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; |
| 22 | import android.animation.ObjectAnimator; |
| 23 | import android.animation.TimeInterpolator; |
Winson Chung | bb6f6a5 | 2011-07-25 17:55:44 -0700 | [diff] [blame] | 24 | import android.animation.ValueAnimator; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 25 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 27 | import android.content.res.TypedArray; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 28 | import android.graphics.Canvas; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 29 | import android.graphics.Matrix; |
| 30 | import android.graphics.PointF; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 31 | import android.graphics.Rect; |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 33 | import android.os.Parcel; |
| 34 | import android.os.Parcelable; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 35 | import android.support.v4.view.accessibility.AccessibilityEventCompat; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 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 | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 50 | import android.view.animation.AnimationUtils; |
| 51 | import android.view.animation.DecelerateInterpolator; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 52 | import android.view.animation.Interpolator; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 53 | import android.view.animation.LinearInterpolator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 54 | import android.widget.Scroller; |
| 55 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 56 | import java.util.ArrayList; |
| 57 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 58 | interface Page { |
| 59 | public int getPageChildCount(); |
| 60 | public View getChildOnPageAt(int i); |
| 61 | public void removeAllViewsOnPage(); |
| 62 | public void removeViewOnPageAt(int i); |
| 63 | public int indexOfChildOnPage(View v); |
| 64 | } |
| 65 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 66 | /** |
| 67 | * An abstraction of the original Workspace which supports browsing through a |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 68 | * sequential list of "pages" |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 69 | */ |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 70 | public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 71 | private static final String TAG = "PagedView"; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 72 | private static final boolean DEBUG = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 73 | protected static final int INVALID_PAGE = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 74 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 75 | // 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] | 76 | private static final int MIN_LENGTH_FOR_FLING = 25; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 77 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 78 | protected static final int PAGE_SNAP_ANIMATION_DURATION = 750; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 79 | protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 80 | protected static final float NANOTIME_DIV = 1000000000.0f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 81 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 82 | private static final float OVERSCROLL_ACCELERATE_FACTOR = 2; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 83 | private static final float OVERSCROLL_DAMP_FACTOR = 0.14f; |
Winson Chung | 867ca62 | 2012-02-21 15:48:35 -0800 | [diff] [blame] | 84 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 85 | private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 86 | // The page is moved more than halfway, automatically move to the next page on touch up. |
| 87 | private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 88 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 89 | // The following constants need to be scaled based on density. The scaled versions will be |
| 90 | // assigned to the corresponding member variables below. |
| 91 | private static final int FLING_THRESHOLD_VELOCITY = 500; |
| 92 | private static final int MIN_SNAP_VELOCITY = 1500; |
| 93 | private static final int MIN_FLING_VELOCITY = 250; |
| 94 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 95 | // We are disabling touch interaction of the widget region for factory ROM. |
| 96 | private static final boolean DISABLE_TOUCH_INTERACTION = false; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 97 | private static final boolean DISABLE_TOUCH_SIDE_PAGES = true; |
Adam Cohen | dedbd96 | 2013-07-11 14:21:49 -0700 | [diff] [blame] | 98 | private static final boolean DISABLE_FLING_TO_DELETE = true; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 99 | |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 100 | public static final int INVALID_RESTORE_PAGE = -1001; |
| 101 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 102 | private boolean mFreeScroll = false; |
| 103 | private int mFreeScrollMinScrollX = -1; |
| 104 | private int mFreeScrollMaxScrollX = -1; |
| 105 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 106 | static final int AUTOMATIC_PAGE_SPACING = -1; |
| 107 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 108 | protected int mFlingThresholdVelocity; |
| 109 | protected int mMinFlingVelocity; |
| 110 | protected int mMinSnapVelocity; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 111 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 112 | protected float mDensity; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 113 | protected float mSmoothingTime; |
| 114 | protected float mTouchX; |
| 115 | |
| 116 | protected boolean mFirstLayout = true; |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 117 | private int mNormalChildHeight; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 118 | |
| 119 | protected int mCurrentPage; |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 120 | protected int mRestorePage = INVALID_RESTORE_PAGE; |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 121 | protected int mChildCountOnLastLayout; |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 122 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 123 | protected int mNextPage = INVALID_PAGE; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 124 | protected int mMaxScrollX; |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 125 | protected LauncherScroller mScroller; |
| 126 | private Interpolator mDefaultInterpolator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 127 | private VelocityTracker mVelocityTracker; |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 128 | private int mPageSpacing = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 129 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 130 | private float mParentDownMotionX; |
| 131 | private float mParentDownMotionY; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 132 | private float mDownMotionX; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 133 | private float mDownMotionY; |
| 134 | private float mDownScrollX; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 135 | private float mDragViewBaselineLeft; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 136 | protected float mLastMotionX; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 137 | protected float mLastMotionXRemainder; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 138 | protected float mLastMotionY; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 139 | protected float mTotalMotionX; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 140 | private int mLastScreenCenter = -1; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 141 | |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 142 | private boolean mCancelTap; |
| 143 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 144 | private int[] mPageScrolls; |
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 | protected final static int TOUCH_STATE_REST = 0; |
| 147 | protected final static int TOUCH_STATE_SCROLLING = 1; |
| 148 | protected final static int TOUCH_STATE_PREV_PAGE = 2; |
| 149 | protected final static int TOUCH_STATE_NEXT_PAGE = 3; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 150 | protected final static int TOUCH_STATE_REORDERING = 4; |
| 151 | |
Adam Cohen | e45440e | 2010-10-14 18:33:38 -0700 | [diff] [blame] | 152 | protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 153 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 154 | protected int mTouchState = TOUCH_STATE_REST; |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 155 | protected boolean mForceScreenScrolled = false; |
Adam Cohen | cae7f57 | 2013-11-04 14:42:52 -0800 | [diff] [blame] | 156 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 157 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 158 | protected OnLongClickListener mLongClickListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 159 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 160 | protected int mTouchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 161 | private int mPagingTouchSlop; |
| 162 | private int mMaximumVelocity; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 163 | protected int mPageLayoutPaddingTop; |
| 164 | protected int mPageLayoutPaddingBottom; |
| 165 | protected int mPageLayoutPaddingLeft; |
| 166 | protected int mPageLayoutPaddingRight; |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 167 | protected int mPageLayoutWidthGap; |
| 168 | protected int mPageLayoutHeightGap; |
Michael Jurka | 87b1490 | 2011-05-25 22:13:09 -0700 | [diff] [blame] | 169 | protected int mCellCountX = 0; |
| 170 | protected int mCellCountY = 0; |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 171 | protected boolean mCenterPagesVertically; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 172 | protected boolean mAllowOverScroll = true; |
| 173 | protected int mUnboundedScrollX; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 174 | protected int[] mTempVisiblePagesRange = new int[2]; |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 175 | protected boolean mForceDrawAllChildrenNextFrame; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 176 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 177 | // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 178 | // it is equal to the scaled overscroll position. We use a separate value so as to prevent |
| 179 | // the screens from continuing to translate beyond the normal bounds. |
| 180 | protected int mOverScrollX; |
| 181 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 182 | protected static final int INVALID_POINTER = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 183 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 184 | protected int mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 185 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 186 | private PageSwitchListener mPageSwitchListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 187 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 188 | protected ArrayList<Boolean> mDirtyPageContent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 189 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 190 | // If true, syncPages and syncPageItems will be called to refresh pages |
| 191 | protected boolean mContentIsRefreshable = true; |
| 192 | |
| 193 | // If true, modify alpha of neighboring pages as user scrolls left/right |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 194 | protected boolean mFadeInAdjacentScreens = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 195 | |
| 196 | // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding |
| 197 | // to switch to a new page |
| 198 | protected boolean mUsePagingTouchSlop = true; |
| 199 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 200 | // If true, the subclass should directly update scrollX itself in its computeScroll method |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 201 | // (SmoothPagedView does this) |
| 202 | protected boolean mDeferScrollUpdate = false; |
Winson Chung | 9c0565f | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 203 | protected boolean mDeferLoadAssociatedPagesUntilScrollCompletes = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 204 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 205 | protected boolean mIsPageMoving = false; |
| 206 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 207 | // All syncs and layout passes are deferred until data is ready. |
| 208 | protected boolean mIsDataReady = false; |
| 209 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 210 | protected boolean mAllowLongPress = true; |
| 211 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 212 | // Page Indicator |
| 213 | private int mPageIndicatorViewId; |
| 214 | private PageIndicator mPageIndicator; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 215 | private boolean mAllowPagedViewAnimations = true; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 216 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 217 | // The viewport whether the pages are to be contained (the actual view may be larger than the |
| 218 | // viewport) |
| 219 | private Rect mViewport = new Rect(); |
| 220 | |
| 221 | // Reordering |
| 222 | // We use the min scale to determine how much to expand the actually PagedView measured |
| 223 | // dimensions such that when we are zoomed out, the view is not clipped |
| 224 | private int REORDERING_DROP_REPOSITION_DURATION = 200; |
| 225 | protected int REORDERING_REORDER_REPOSITION_DURATION = 300; |
| 226 | protected int REORDERING_ZOOM_IN_OUT_DURATION = 250; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 227 | private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 228 | private float mMinScale = 1f; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 229 | private boolean mUseMinScale = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 230 | protected View mDragView; |
| 231 | protected AnimatorSet mZoomInOutAnim; |
| 232 | private Runnable mSidePageHoverRunnable; |
| 233 | private int mSidePageHoverIndex = -1; |
| 234 | // This variable's scope is only for the duration of startReordering() and endReordering() |
| 235 | private boolean mReorderingStarted = false; |
| 236 | // This variable's scope is for the duration of startReordering() and after the zoomIn() |
| 237 | // animation after endReordering() |
| 238 | private boolean mIsReordering; |
| 239 | // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition |
| 240 | private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2; |
| 241 | private int mPostReorderingPreZoomInRemainingAnimationCount; |
| 242 | private Runnable mPostReorderingPreZoomInRunnable; |
| 243 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 244 | // Convenience/caching |
| 245 | private Matrix mTmpInvMatrix = new Matrix(); |
| 246 | private float[] mTmpPoint = new float[2]; |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 247 | private int[] mTmpIntPoint = new int[2]; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 248 | private Rect mTmpRect = new Rect(); |
| 249 | private Rect mAltTmpRect = new Rect(); |
| 250 | |
| 251 | // Fling to delete |
| 252 | private int FLING_TO_DELETE_FADE_OUT_DURATION = 350; |
| 253 | private float FLING_TO_DELETE_FRICTION = 0.035f; |
| 254 | // The degrees specifies how much deviation from the up vector to still consider a fling "up" |
| 255 | private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f; |
| 256 | protected int mFlingToDeleteThresholdVelocity = -1400; |
| 257 | // Drag to delete |
| 258 | private boolean mDeferringForDelete = false; |
| 259 | private int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250; |
| 260 | private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350; |
| 261 | |
| 262 | // Drop to delete |
| 263 | private View mDeleteDropTarget; |
| 264 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 265 | // Bouncer |
| 266 | private boolean mTopAlignPageWhenShrinkingForBouncer = false; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 267 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 268 | protected final Rect mInsets = new Rect(); |
| 269 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 270 | public interface PageSwitchListener { |
| 271 | void onPageSwitch(View newPage, int newPageIndex); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 274 | public PagedView(Context context) { |
| 275 | this(context, null); |
| 276 | } |
| 277 | |
| 278 | public PagedView(Context context, AttributeSet attrs) { |
| 279 | this(context, attrs, 0); |
| 280 | } |
| 281 | |
| 282 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 283 | super(context, attrs, defStyle); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 284 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 285 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 286 | R.styleable.PagedView, defStyle, 0); |
Adam Cohen | 0cd0eba | 2013-10-28 14:22:25 -0700 | [diff] [blame] | 287 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 288 | mPageLayoutPaddingTop = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 289 | R.styleable.PagedView_pageLayoutPaddingTop, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 290 | mPageLayoutPaddingBottom = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 291 | R.styleable.PagedView_pageLayoutPaddingBottom, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 292 | mPageLayoutPaddingLeft = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 293 | R.styleable.PagedView_pageLayoutPaddingLeft, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 294 | mPageLayoutPaddingRight = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 295 | R.styleable.PagedView_pageLayoutPaddingRight, 0); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 296 | mPageLayoutWidthGap = a.getDimensionPixelSize( |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 297 | R.styleable.PagedView_pageLayoutWidthGap, 0); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 298 | mPageLayoutHeightGap = a.getDimensionPixelSize( |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 299 | R.styleable.PagedView_pageLayoutHeightGap, 0); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 300 | mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 301 | a.recycle(); |
| 302 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 303 | setHapticFeedbackEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 304 | init(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Initializes various states for this workspace. |
| 309 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 310 | protected void init() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 311 | mDirtyPageContent = new ArrayList<Boolean>(); |
| 312 | mDirtyPageContent.ensureCapacity(32); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 313 | mScroller = new LauncherScroller(getContext()); |
| 314 | setDefaultInterpolator(new ScrollInterpolator()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 315 | mCurrentPage = 0; |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 316 | mCenterPagesVertically = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 317 | |
| 318 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 319 | mTouchSlop = configuration.getScaledPagingTouchSlop(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 320 | mPagingTouchSlop = configuration.getScaledPagingTouchSlop(); |
| 321 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 322 | mDensity = getResources().getDisplayMetrics().density; |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 323 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 324 | // Scale the fling-to-delete threshold by the density |
| 325 | mFlingToDeleteThresholdVelocity = |
| 326 | (int) (mFlingToDeleteThresholdVelocity * mDensity); |
| 327 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 328 | mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity); |
| 329 | mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity); |
| 330 | mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 331 | setOnHierarchyChangeListener(this); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 334 | protected void setDefaultInterpolator(Interpolator interpolator) { |
| 335 | mDefaultInterpolator = interpolator; |
| 336 | mScroller.setInterpolator(mDefaultInterpolator); |
| 337 | } |
| 338 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 339 | protected void onAttachedToWindow() { |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 340 | super.onAttachedToWindow(); |
| 341 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 342 | // Hook up the page indicator |
| 343 | ViewGroup parent = (ViewGroup) getParent(); |
| 344 | if (mPageIndicator == null && mPageIndicatorViewId > -1) { |
| 345 | mPageIndicator = (PageIndicator) parent.findViewById(mPageIndicatorViewId); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 346 | mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations); |
Winson Chung | 82dfe58 | 2013-07-26 15:07:49 -0700 | [diff] [blame] | 347 | |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 348 | ArrayList<PageIndicator.PageMarkerResources> markers = |
| 349 | new ArrayList<PageIndicator.PageMarkerResources>(); |
Winson Chung | 82dfe58 | 2013-07-26 15:07:49 -0700 | [diff] [blame] | 350 | for (int i = 0; i < getChildCount(); ++i) { |
| 351 | markers.add(getPageIndicatorMarker(i)); |
| 352 | } |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 353 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 354 | mPageIndicator.addMarkers(markers, mAllowPagedViewAnimations); |
Adam Cohen | fbdf427 | 2013-10-09 13:02:21 -0700 | [diff] [blame] | 355 | |
| 356 | OnClickListener listener = getPageIndicatorClickListener(); |
| 357 | if (listener != null) { |
| 358 | mPageIndicator.setOnClickListener(listener); |
| 359 | } |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 360 | mPageIndicator.setContentDescription(getPageIndicatorDescription()); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 364 | protected String getPageIndicatorDescription() { |
| 365 | return getCurrentPageDescription(); |
| 366 | } |
| 367 | |
| 368 | protected OnClickListener getPageIndicatorClickListener() { |
| 369 | return null; |
| 370 | } |
| 371 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 372 | protected void onDetachedFromWindow() { |
| 373 | // Unhook the page indicator |
| 374 | mPageIndicator = null; |
| 375 | } |
| 376 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 377 | void setDeleteDropTarget(View v) { |
| 378 | mDeleteDropTarget = v; |
| 379 | } |
| 380 | |
| 381 | // Convenience methods to map points from self to parent and vice versa |
| 382 | float[] mapPointFromViewToParent(View v, float x, float y) { |
| 383 | mTmpPoint[0] = x; |
| 384 | mTmpPoint[1] = y; |
| 385 | v.getMatrix().mapPoints(mTmpPoint); |
| 386 | mTmpPoint[0] += v.getLeft(); |
| 387 | mTmpPoint[1] += v.getTop(); |
| 388 | return mTmpPoint; |
| 389 | } |
| 390 | float[] mapPointFromParentToView(View v, float x, float y) { |
| 391 | mTmpPoint[0] = x - v.getLeft(); |
| 392 | mTmpPoint[1] = y - v.getTop(); |
| 393 | v.getMatrix().invert(mTmpInvMatrix); |
| 394 | mTmpInvMatrix.mapPoints(mTmpPoint); |
| 395 | return mTmpPoint; |
| 396 | } |
| 397 | |
| 398 | void updateDragViewTranslationDuringDrag() { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 399 | if (mDragView != null) { |
| 400 | float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) + |
| 401 | (mDragViewBaselineLeft - mDragView.getLeft()); |
| 402 | float y = mLastMotionY - mDownMotionY; |
| 403 | mDragView.setTranslationX(x); |
| 404 | mDragView.setTranslationY(y); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 405 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 406 | if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): " |
| 407 | + x + ", " + y); |
| 408 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | public void setMinScale(float f) { |
| 412 | mMinScale = f; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 413 | mUseMinScale = true; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 414 | requestLayout(); |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public void setScaleX(float scaleX) { |
| 419 | super.setScaleX(scaleX); |
| 420 | if (isReordering(true)) { |
| 421 | float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY); |
| 422 | mLastMotionX = p[0]; |
| 423 | mLastMotionY = p[1]; |
| 424 | updateDragViewTranslationDuringDrag(); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Convenience methods to get the actual width/height of the PagedView (since it is measured |
| 429 | // to be larger to account for the minimum possible scale) |
| 430 | int getViewportWidth() { |
| 431 | return mViewport.width(); |
| 432 | } |
| 433 | int getViewportHeight() { |
| 434 | return mViewport.height(); |
| 435 | } |
| 436 | |
| 437 | // Convenience methods to get the offset ASSUMING that we are centering the pages in the |
| 438 | // PagedView both horizontally and vertically |
| 439 | int getViewportOffsetX() { |
| 440 | return (getMeasuredWidth() - getViewportWidth()) / 2; |
| 441 | } |
| 442 | |
| 443 | int getViewportOffsetY() { |
| 444 | return (getMeasuredHeight() - getViewportHeight()) / 2; |
| 445 | } |
| 446 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 447 | PageIndicator getPageIndicator() { |
| 448 | return mPageIndicator; |
| 449 | } |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 450 | protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) { |
| 451 | return new PageIndicator.PageMarkerResources(); |
Winson Chung | 82dfe58 | 2013-07-26 15:07:49 -0700 | [diff] [blame] | 452 | } |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 453 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 454 | public void setPageSwitchListener(PageSwitchListener pageSwitchListener) { |
| 455 | mPageSwitchListener = pageSwitchListener; |
| 456 | if (mPageSwitchListener != null) { |
| 457 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | /** |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 462 | * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api. |
| 463 | */ |
| 464 | public boolean isLayoutRtl() { |
| 465 | return (getLayoutDirection() == LAYOUT_DIRECTION_RTL); |
| 466 | } |
| 467 | |
| 468 | /** |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 469 | * Called by subclasses to mark that data is ready, and that we can begin loading and laying |
| 470 | * out pages. |
| 471 | */ |
| 472 | protected void setDataIsReady() { |
| 473 | mIsDataReady = true; |
| 474 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 475 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 476 | protected boolean isDataReady() { |
| 477 | return mIsDataReady; |
| 478 | } |
| 479 | |
| 480 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 481 | * Returns the index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 482 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 483 | * @return The index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 484 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 485 | int getCurrentPage() { |
| 486 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 487 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 488 | |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 489 | int getNextPage() { |
| 490 | return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 491 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 492 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 493 | int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 494 | return getChildCount(); |
| 495 | } |
| 496 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 497 | View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 498 | return getChildAt(index); |
| 499 | } |
| 500 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 501 | protected int indexToPage(int index) { |
| 502 | return index; |
| 503 | } |
| 504 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 505 | /** |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 506 | * Updates the scroll of the current page immediately to its final scroll position. We use this |
| 507 | * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of |
| 508 | * the previous tab page. |
| 509 | */ |
| 510 | protected void updateCurrentPageScroll() { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 511 | // If the current page is invalid, just reset the scroll position to zero |
| 512 | int newX = 0; |
| 513 | if (0 <= mCurrentPage && mCurrentPage < getPageCount()) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 514 | newX = getScrollForPage(mCurrentPage); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 515 | } |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 516 | scrollTo(newX, 0); |
| 517 | mScroller.setFinalX(newX); |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 518 | forceFinishScroller(); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /** |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 522 | * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 523 | * {@link #updateCurrentPageScroll()} should be called, to correctly set the final state and |
| 524 | * re-enable scrolling. |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 525 | */ |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 526 | void stopScrolling() { |
Winson Chung | b88ae41 | 2013-10-29 17:21:56 -0700 | [diff] [blame] | 527 | mCurrentPage = getNextPage(); |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 528 | forceFinishScroller(); |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 531 | private void abortScrollerAnimation(boolean resetNextPage) { |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 532 | mScroller.abortAnimation(); |
| 533 | // We need to clean up the next page here to avoid computeScrollHelper from |
| 534 | // updating current page on the pass. |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 535 | if (resetNextPage) { |
| 536 | mNextPage = INVALID_PAGE; |
| 537 | } |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 538 | } |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 539 | |
| 540 | private void forceFinishScroller() { |
| 541 | mScroller.forceFinished(true); |
| 542 | // We need to clean up the next page here to avoid computeScrollHelper from |
| 543 | // updating current page on the pass. |
| 544 | mNextPage = INVALID_PAGE; |
| 545 | } |
| 546 | |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 547 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 548 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 549 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 550 | void setCurrentPage(int currentPage) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 551 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 552 | abortScrollerAnimation(true); |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 553 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 554 | // don't introduce any checks like mCurrentPage == currentPage here-- if we change the |
| 555 | // the default |
| 556 | if (getChildCount() == 0) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 557 | return; |
| 558 | } |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 559 | mForceScreenScrolled = true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 560 | mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1)); |
Winson Chung | 181c3dc | 2013-07-17 15:36:20 -0700 | [diff] [blame] | 561 | updateCurrentPageScroll(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 562 | notifyPageSwitchListener(); |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 563 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 566 | /** |
| 567 | * The restore page will be set in place of the current page at the next (likely first) |
| 568 | * layout. |
| 569 | */ |
| 570 | void setRestorePage(int restorePage) { |
| 571 | mRestorePage = restorePage; |
| 572 | } |
| 573 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 574 | protected void notifyPageSwitchListener() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 575 | if (mPageSwitchListener != null) { |
| 576 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 577 | } |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 578 | |
| 579 | // Update the page indicator (when we aren't reordering) |
| 580 | if (mPageIndicator != null && !isReordering(false)) { |
| 581 | mPageIndicator.setActiveMarker(getNextPage()); |
| 582 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 583 | } |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 584 | protected void pageBeginMoving() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 585 | if (!mIsPageMoving) { |
| 586 | mIsPageMoving = true; |
| 587 | onPageBeginMoving(); |
| 588 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 591 | protected void pageEndMoving() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 592 | if (mIsPageMoving) { |
| 593 | mIsPageMoving = false; |
| 594 | onPageEndMoving(); |
| 595 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 598 | protected boolean isPageMoving() { |
| 599 | return mIsPageMoving; |
| 600 | } |
| 601 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 602 | // a method that subclasses can override to add behavior |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 603 | protected void onPageBeginMoving() { |
| 604 | } |
| 605 | |
| 606 | // a method that subclasses can override to add behavior |
| 607 | protected void onPageEndMoving() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 610 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 611 | * Registers the specified listener on each page contained in this workspace. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 612 | * |
| 613 | * @param l The listener used to respond to long clicks. |
| 614 | */ |
| 615 | @Override |
| 616 | public void setOnLongClickListener(OnLongClickListener l) { |
| 617 | mLongClickListener = l; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 618 | final int count = getPageCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 619 | for (int i = 0; i < count; i++) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 620 | getPageAt(i).setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 621 | } |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 622 | super.setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | @Override |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 626 | public void scrollBy(int x, int y) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 627 | scrollTo(mUnboundedScrollX + x, getScrollY() + y); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 631 | public void scrollTo(int x, int y) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 632 | // In free scroll mode, we clamp the scrollX |
| 633 | if (mFreeScroll) { |
| 634 | x = Math.min(x, mFreeScrollMaxScrollX); |
| 635 | x = Math.max(x, mFreeScrollMinScrollX); |
| 636 | } |
| 637 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 638 | final boolean isRtl = isLayoutRtl(); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 639 | mUnboundedScrollX = x; |
| 640 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 641 | boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0); |
| 642 | boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX); |
| 643 | if (isXBeforeFirstPage) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 644 | super.scrollTo(0, y); |
| 645 | if (mAllowOverScroll) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 646 | if (isRtl) { |
| 647 | overScroll(x - mMaxScrollX); |
| 648 | } else { |
| 649 | overScroll(x); |
| 650 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 651 | } |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 652 | } else if (isXAfterLastPage) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 653 | super.scrollTo(mMaxScrollX, y); |
| 654 | if (mAllowOverScroll) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 655 | if (isRtl) { |
| 656 | overScroll(x); |
| 657 | } else { |
| 658 | overScroll(x - mMaxScrollX); |
| 659 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 660 | } |
| 661 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 662 | mOverScrollX = x; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 663 | super.scrollTo(x, y); |
| 664 | } |
| 665 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 666 | mTouchX = x; |
| 667 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 668 | |
| 669 | // Update the last motion events when scrolling |
| 670 | if (isReordering(true)) { |
| 671 | float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY); |
| 672 | mLastMotionX = p[0]; |
| 673 | mLastMotionY = p[1]; |
| 674 | updateDragViewTranslationDuringDrag(); |
| 675 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 678 | private void sendScrollAccessibilityEvent() { |
| 679 | AccessibilityManager am = |
| 680 | (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 681 | if (am.isEnabled()) { |
| 682 | AccessibilityEvent ev = |
| 683 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 684 | ev.setItemCount(getChildCount()); |
| 685 | ev.setFromIndex(mCurrentPage); |
Adam Cohen | e4602ae | 2013-10-31 15:46:45 -0700 | [diff] [blame] | 686 | ev.setToIndex(getNextPage()); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 687 | |
Alan Viverette | 254139a | 2013-10-08 13:13:46 -0700 | [diff] [blame] | 688 | final int action; |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 689 | if (getNextPage() >= mCurrentPage) { |
| 690 | action = AccessibilityNodeInfo.ACTION_SCROLL_FORWARD; |
| 691 | } else { |
| 692 | action = AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD; |
| 693 | } |
| 694 | |
| 695 | ev.setAction(action); |
| 696 | sendAccessibilityEventUnchecked(ev); |
| 697 | } |
| 698 | } |
| 699 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 700 | // we moved this functionality to a helper function so SmoothPagedView can reuse it |
| 701 | protected boolean computeScrollHelper() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 702 | if (mScroller.computeScrollOffset()) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 703 | // 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] | 704 | if (getScrollX() != mScroller.getCurrX() |
| 705 | || getScrollY() != mScroller.getCurrY() |
Michael Jurka | b06d95f | 2012-04-02 06:26:53 -0700 | [diff] [blame] | 706 | || mOverScrollX != mScroller.getCurrX()) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 707 | float scaleX = mFreeScroll ? getScaleX() : 1f; |
| 708 | int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX)); |
| 709 | scrollTo(scrollX, mScroller.getCurrY()); |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 710 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 711 | invalidate(); |
| 712 | return true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 713 | } else if (mNextPage != INVALID_PAGE) { |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 714 | sendScrollAccessibilityEvent(); |
| 715 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 716 | mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 717 | mNextPage = INVALID_PAGE; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 718 | notifyPageSwitchListener(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 719 | |
Winson Chung | 9c0565f | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 720 | // Load the associated pages if necessary |
| 721 | if (mDeferLoadAssociatedPagesUntilScrollCompletes) { |
| 722 | loadAssociatedPages(mCurrentPage); |
| 723 | mDeferLoadAssociatedPagesUntilScrollCompletes = false; |
| 724 | } |
| 725 | |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 726 | // We don't want to trigger a page end moving unless the page has settled |
| 727 | // and the user has stopped scrolling |
| 728 | if (mTouchState == TOUCH_STATE_REST) { |
| 729 | pageEndMoving(); |
| 730 | } |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 731 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 732 | onPostReorderingAnimationCompleted(); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 733 | AccessibilityManager am = (AccessibilityManager) |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 734 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 735 | if (am.isEnabled()) { |
| 736 | // Notify the user when the page changes |
| 737 | announceForAccessibility(getCurrentPageDescription()); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 738 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 739 | return true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 740 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | |
| 744 | @Override |
| 745 | public void computeScroll() { |
| 746 | computeScrollHelper(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 749 | protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) { |
| 750 | return mTopAlignPageWhenShrinkingForBouncer; |
| 751 | } |
| 752 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 753 | public static class LayoutParams extends ViewGroup.LayoutParams { |
| 754 | public boolean isFullScreenPage = false; |
| 755 | |
| 756 | /** |
| 757 | * {@inheritDoc} |
| 758 | */ |
| 759 | public LayoutParams(int width, int height) { |
| 760 | super(width, height); |
| 761 | } |
| 762 | |
| 763 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 764 | super(source); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | protected LayoutParams generateDefaultLayoutParams() { |
| 769 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 770 | } |
| 771 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 772 | public void addFullScreenPage(View page) { |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 773 | LayoutParams lp = generateDefaultLayoutParams(); |
| 774 | lp.isFullScreenPage = true; |
| 775 | super.addView(page, 0, lp); |
| 776 | } |
| 777 | |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 778 | public int getNormalChildHeight() { |
| 779 | return mNormalChildHeight; |
| 780 | } |
| 781 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 782 | @Override |
| 783 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 784 | if (!mIsDataReady || getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 785 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 786 | return; |
| 787 | } |
| 788 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 789 | // We measure the dimensions of the PagedView to be larger than the pages so that when we |
| 790 | // 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] | 791 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 792 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 793 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 794 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 795 | // 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] | 796 | // viewport, we can be at most one and a half screens offset once we scale down |
| 797 | DisplayMetrics dm = getResources().getDisplayMetrics(); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 798 | int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right, |
| 799 | dm.heightPixels + mInsets.top + mInsets.bottom); |
Adam Cohen | 410f3cd | 2013-09-22 12:09:32 -0700 | [diff] [blame] | 800 | |
Winson Chung | c82d262 | 2013-11-06 13:23:29 -0800 | [diff] [blame] | 801 | int parentWidthSize = (int) (2f * maxSize); |
| 802 | int parentHeightSize = (int) (2f * maxSize); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 803 | int scaledWidthSize, scaledHeightSize; |
| 804 | if (mUseMinScale) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 805 | scaledWidthSize = (int) (parentWidthSize / mMinScale); |
| 806 | scaledHeightSize = (int) (parentHeightSize / mMinScale); |
| 807 | } else { |
| 808 | scaledWidthSize = widthSize; |
| 809 | scaledHeightSize = heightSize; |
| 810 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 811 | mViewport.set(0, 0, widthSize, heightSize); |
| 812 | |
| 813 | if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) { |
| 814 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 815 | return; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 818 | // Return early if we aren't given a proper dimension |
| 819 | if (widthSize <= 0 || heightSize <= 0) { |
| 820 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 821 | return; |
| 822 | } |
| 823 | |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 824 | /* Allow the height to be set as WRAP_CONTENT. This allows the particular case |
| 825 | * of the All apps view on XLarge displays to not take up more space then it needs. Width |
| 826 | * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect |
| 827 | * each page to have the same width. |
| 828 | */ |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 829 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
| 830 | final int horizontalPadding = getPaddingLeft() + getPaddingRight(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 831 | |
| 832 | // The children are given the same width and height as the workspace |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 833 | // unless they were set to WRAP_CONTENT |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 834 | if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 835 | if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize); |
| 836 | if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize); |
| 837 | if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding); |
| 838 | if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 839 | final int childCount = getChildCount(); |
| 840 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 841 | // disallowing padding in paged view (just pass 0) |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 842 | final View child = getPageAt(i); |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 843 | if (child.getVisibility() != GONE) { |
| 844 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 845 | |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 846 | int childWidthMode; |
| 847 | int childHeightMode; |
| 848 | int childWidth; |
| 849 | int childHeight; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 850 | |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 851 | if (!lp.isFullScreenPage) { |
| 852 | if (lp.width == LayoutParams.WRAP_CONTENT) { |
| 853 | childWidthMode = MeasureSpec.AT_MOST; |
| 854 | } else { |
| 855 | childWidthMode = MeasureSpec.EXACTLY; |
| 856 | } |
| 857 | |
| 858 | if (lp.height == LayoutParams.WRAP_CONTENT) { |
| 859 | childHeightMode = MeasureSpec.AT_MOST; |
| 860 | } else { |
| 861 | childHeightMode = MeasureSpec.EXACTLY; |
| 862 | } |
| 863 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 864 | childWidth = getViewportWidth() - horizontalPadding |
| 865 | - mInsets.left - mInsets.right; |
| 866 | childHeight = getViewportHeight() - verticalPadding |
| 867 | - mInsets.top - mInsets.bottom; |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 868 | mNormalChildHeight = childHeight; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 869 | } else { |
| 870 | childWidthMode = MeasureSpec.EXACTLY; |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 871 | childHeightMode = MeasureSpec.EXACTLY; |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 872 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 873 | childWidth = getViewportWidth() - mInsets.left - mInsets.right; |
| 874 | childHeight = getViewportHeight(); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 877 | final int childWidthMeasureSpec = |
| 878 | MeasureSpec.makeMeasureSpec(childWidth, childWidthMode); |
| 879 | final int childHeightMeasureSpec = |
| 880 | MeasureSpec.makeMeasureSpec(childHeight, childHeightMode); |
| 881 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 882 | } |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 883 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 884 | setMeasuredDimension(scaledWidthSize, scaledHeightSize); |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 885 | } |
| 886 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 887 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 888 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 889 | if (!mIsDataReady || getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 890 | return; |
| 891 | } |
| 892 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 893 | if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 894 | final int childCount = getChildCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 895 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 896 | int offsetX = getViewportOffsetX(); |
| 897 | int offsetY = getViewportOffsetY(); |
| 898 | |
| 899 | // Update the viewport offsets |
| 900 | mViewport.offset(offsetX, offsetY); |
| 901 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 902 | final boolean isRtl = isLayoutRtl(); |
| 903 | |
| 904 | final int startIndex = isRtl ? childCount - 1 : 0; |
| 905 | final int endIndex = isRtl ? -1 : childCount; |
| 906 | final int delta = isRtl ? -1 : 1; |
| 907 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 908 | int verticalPadding = getPaddingTop() + getPaddingBottom(); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 909 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 910 | LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams(); |
Adam Cohen | 84a465a | 2013-11-11 18:49:56 +0000 | [diff] [blame] | 911 | LayoutParams nextLp; |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 912 | |
| 913 | int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft()); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 914 | if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) { |
| 915 | mPageScrolls = new int[getChildCount()]; |
| 916 | } |
| 917 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 918 | for (int i = startIndex; i != endIndex; i += delta) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 919 | final View child = getPageAt(i); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 920 | if (child.getVisibility() != View.GONE) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 921 | lp = (LayoutParams) child.getLayoutParams(); |
Vladimir Marko | 2824b07 | 2013-10-04 16:42:17 +0100 | [diff] [blame] | 922 | int childTop; |
| 923 | if (lp.isFullScreenPage) { |
| 924 | childTop = offsetY; |
| 925 | } else { |
| 926 | childTop = offsetY + getPaddingTop() + mInsets.top; |
| 927 | if (mCenterPagesVertically) { |
| 928 | childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2; |
| 929 | } |
| 930 | } |
| 931 | |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 932 | final int childWidth = child.getMeasuredWidth(); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 933 | final int childHeight = child.getMeasuredHeight(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 934 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 935 | if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 936 | child.layout(childLeft, childTop, |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 937 | childLeft + child.getMeasuredWidth(), childTop + childHeight); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 938 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 939 | int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft(); |
| 940 | mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX; |
Adam Cohen | 84a465a | 2013-11-11 18:49:56 +0000 | [diff] [blame] | 941 | |
| 942 | int pageGap = mPageSpacing; |
| 943 | int next = i + delta; |
| 944 | if (next != endIndex) { |
| 945 | nextLp = (LayoutParams) getPageAt(next).getLayoutParams(); |
| 946 | } else { |
| 947 | nextLp = null; |
| 948 | } |
| 949 | |
| 950 | // Prevent full screen pages from showing in the viewport |
| 951 | // when they are not the current page. |
| 952 | if (lp.isFullScreenPage) { |
| 953 | pageGap = getPaddingLeft(); |
| 954 | } else if (nextLp != null && nextLp.isFullScreenPage) { |
| 955 | pageGap = getPaddingRight(); |
| 956 | } |
| 957 | |
| 958 | childLeft += childWidth + pageGap; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 959 | } |
| 960 | } |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 961 | |
| 962 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
| 963 | setHorizontalScrollBarEnabled(false); |
Michael Jurka | dd6c091 | 2012-01-16 06:46:20 -0800 | [diff] [blame] | 964 | updateCurrentPageScroll(); |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 965 | setHorizontalScrollBarEnabled(true); |
| 966 | mFirstLayout = false; |
| 967 | } |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 968 | |
| 969 | if (childCount > 0) { |
| 970 | final int index = isLayoutRtl() ? 0 : childCount - 1; |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 971 | mMaxScrollX = getScrollForPage(index); |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 972 | } else { |
| 973 | mMaxScrollX = 0; |
| 974 | } |
| 975 | |
| 976 | if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() && |
| 977 | !mDeferringForDelete) { |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 978 | if (mRestorePage != INVALID_RESTORE_PAGE) { |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 979 | setCurrentPage(mRestorePage); |
Adam Cohen | 21cd002 | 2013-10-09 18:57:02 -0700 | [diff] [blame] | 980 | mRestorePage = INVALID_RESTORE_PAGE; |
Winson Chung | 8c87cd8 | 2013-07-23 16:20:10 -0700 | [diff] [blame] | 981 | } else { |
| 982 | setCurrentPage(getNextPage()); |
| 983 | } |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 984 | } |
| 985 | mChildCountOnLastLayout = getChildCount(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 986 | |
| 987 | if (isReordering(true)) { |
| 988 | updateDragViewTranslationDuringDrag(); |
| 989 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 992 | public void setPageSpacing(int pageSpacing) { |
| 993 | mPageSpacing = pageSpacing; |
| 994 | requestLayout(); |
| 995 | } |
| 996 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 997 | protected void screenScrolled(int screenCenter) { |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 998 | boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX; |
| 999 | |
| 1000 | if (mFadeInAdjacentScreens && !isInOverscroll) { |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1001 | for (int i = 0; i < getChildCount(); i++) { |
| 1002 | View child = getChildAt(i); |
| 1003 | if (child != null) { |
| 1004 | float scrollProgress = getScrollProgress(screenCenter, child, i); |
| 1005 | float alpha = 1 - Math.abs(scrollProgress); |
Michael Jurka | 7372c59 | 2012-01-16 04:21:35 -0800 | [diff] [blame] | 1006 | child.setAlpha(alpha); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | invalidate(); |
| 1010 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1013 | protected void enablePagedViewAnimations() { |
| 1014 | mAllowPagedViewAnimations = true; |
| 1015 | |
| 1016 | } |
| 1017 | protected void disablePagedViewAnimations() { |
| 1018 | mAllowPagedViewAnimations = false; |
| 1019 | } |
| 1020 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1021 | @Override |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1022 | public void onChildViewAdded(View parent, View child) { |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 1023 | // Update the page indicator, we don't update the page indicator as we |
| 1024 | // add/remove pages |
| 1025 | if (mPageIndicator != null && !isReordering(false)) { |
Winson Chung | 82dfe58 | 2013-07-26 15:07:49 -0700 | [diff] [blame] | 1026 | int pageIndex = indexOfChild(child); |
Winson Chung | 7819a56 | 2013-09-19 15:55:45 -0700 | [diff] [blame] | 1027 | mPageIndicator.addMarker(pageIndex, |
| 1028 | getPageIndicatorMarker(pageIndex), |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1029 | mAllowPagedViewAnimations); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 1032 | // This ensures that when children are added, they get the correct transforms / alphas |
| 1033 | // in accordance with any scroll effects. |
| 1034 | mForceScreenScrolled = true; |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 1035 | updateFreescrollBounds(); |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 1036 | invalidate(); |
| 1037 | } |
| 1038 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1039 | @Override |
| 1040 | public void onChildViewRemoved(View parent, View child) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1041 | mForceScreenScrolled = true; |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 1042 | updateFreescrollBounds(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1043 | invalidate(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 1046 | private void removeMarkerForView(int index) { |
| 1047 | // Update the page indicator, we don't update the page indicator as we |
| 1048 | // add/remove pages |
| 1049 | if (mPageIndicator != null && !isReordering(false)) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1050 | mPageIndicator.removeMarker(index, mAllowPagedViewAnimations); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | @Override |
| 1055 | public void removeView(View v) { |
| 1056 | // XXX: We should find a better way to hook into this before the view |
| 1057 | // gets removed form its parent... |
| 1058 | removeMarkerForView(indexOfChild(v)); |
| 1059 | super.removeView(v); |
| 1060 | } |
| 1061 | @Override |
| 1062 | public void removeViewInLayout(View v) { |
| 1063 | // XXX: We should find a better way to hook into this before the view |
| 1064 | // gets removed form its parent... |
| 1065 | removeMarkerForView(indexOfChild(v)); |
| 1066 | super.removeViewInLayout(v); |
| 1067 | } |
| 1068 | @Override |
| 1069 | public void removeViewAt(int index) { |
| 1070 | // XXX: We should find a better way to hook into this before the view |
| 1071 | // gets removed form its parent... |
| 1072 | removeViewAt(index); |
| 1073 | super.removeViewAt(index); |
| 1074 | } |
| 1075 | @Override |
| 1076 | public void removeAllViewsInLayout() { |
| 1077 | // Update the page indicator, we don't update the page indicator as we |
| 1078 | // add/remove pages |
| 1079 | if (mPageIndicator != null) { |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 1080 | mPageIndicator.removeAllMarkers(mAllowPagedViewAnimations); |
Winson Chung | d2be381 | 2013-07-16 11:11:32 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | super.removeAllViewsInLayout(); |
| 1084 | } |
| 1085 | |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1086 | protected int getChildOffset(int index) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1087 | if (index < 0 || index > getChildCount() - 1) return 0; |
| 1088 | |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 1089 | int offset = getPageAt(index).getLeft() - getViewportOffsetX(); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1090 | |
Adam Cohen | f698c6e | 2013-07-17 18:44:25 -0700 | [diff] [blame] | 1091 | return offset; |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1094 | protected void getOverviewModePages(int[] range) { |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1095 | range[0] = 0; |
Adam Cohen | 1f1f45d | 2013-10-02 09:40:18 -0700 | [diff] [blame] | 1096 | range[1] = Math.max(0, getChildCount() - 1); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1099 | protected void getVisiblePages(int[] range) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1100 | final int pageCount = getChildCount(); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1101 | mTmpIntPoint[0] = mTmpIntPoint[1] = 0; |
Michael Jurka | 4ff7d79 | 2012-04-02 03:46:50 -0700 | [diff] [blame] | 1102 | |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1103 | range[0] = -1; |
| 1104 | range[1] = -1; |
| 1105 | |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 1106 | if (pageCount > 0) { |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1107 | int viewportWidth = getViewportWidth(); |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1108 | int curScreen = 0; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1109 | |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1110 | int count = getChildCount(); |
| 1111 | for (int i = 0; i < count; i++) { |
| 1112 | View currPage = getPageAt(i); |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1113 | |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1114 | mTmpIntPoint[0] = 0; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 1115 | Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false); |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1116 | if (mTmpIntPoint[0] > viewportWidth) { |
| 1117 | if (range[0] == -1) { |
| 1118 | continue; |
| 1119 | } else { |
| 1120 | break; |
| 1121 | } |
| 1122 | } |
| 1123 | |
Adam Cohen | 6a678da | 2013-09-24 10:58:01 -0700 | [diff] [blame] | 1124 | mTmpIntPoint[0] = currPage.getMeasuredWidth(); |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1125 | Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false); |
| 1126 | if (mTmpIntPoint[0] < 0) { |
| 1127 | if (range[0] == -1) { |
| 1128 | continue; |
| 1129 | } else { |
| 1130 | break; |
| 1131 | } |
| 1132 | } |
| 1133 | curScreen = i; |
| 1134 | if (range[0] < 0) { |
| 1135 | range[0] = curScreen; |
Winson Chung | c9ca298 | 2013-07-19 12:07:38 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
Adam Cohen | af9b0e5 | 2013-09-23 19:27:38 -0700 | [diff] [blame] | 1138 | |
| 1139 | range[1] = curScreen; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1140 | } else { |
| 1141 | range[0] = -1; |
| 1142 | range[1] = -1; |
| 1143 | } |
| 1144 | } |
| 1145 | |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 1146 | protected boolean shouldDrawChild(View child) { |
Adam Cohen | f343499 | 2013-09-16 16:52:59 -0700 | [diff] [blame] | 1147 | return child.getAlpha() > 0 && child.getVisibility() == VISIBLE; |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1150 | @Override |
| 1151 | protected void dispatchDraw(Canvas canvas) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1152 | int halfScreenSize = getViewportWidth() / 2; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1153 | // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. |
| 1154 | // Otherwise it is equal to the scaled overscroll position. |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1155 | int screenCenter = mOverScrollX + halfScreenSize; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1156 | |
| 1157 | if (screenCenter != mLastScreenCenter || mForceScreenScrolled) { |
Michael Jurka | b06d95f | 2012-04-02 06:26:53 -0700 | [diff] [blame] | 1158 | // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can |
| 1159 | // set it for the next frame |
| 1160 | mForceScreenScrolled = false; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1161 | screenScrolled(screenCenter); |
| 1162 | mLastScreenCenter = screenCenter; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | // Find out which screens are visible; as an optimization we only call draw on them |
| 1166 | final int pageCount = getChildCount(); |
| 1167 | if (pageCount > 0) { |
| 1168 | getVisiblePages(mTempVisiblePagesRange); |
| 1169 | final int leftScreen = mTempVisiblePagesRange[0]; |
| 1170 | final int rightScreen = mTempVisiblePagesRange[1]; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1171 | if (leftScreen != -1 && rightScreen != -1) { |
| 1172 | final long drawingTime = getDrawingTime(); |
| 1173 | // Clip to the bounds |
| 1174 | canvas.save(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1175 | canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(), |
| 1176 | getScrollY() + getBottom() - getTop()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1177 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1178 | // Draw all the children, leaving the drag view for last |
| 1179 | for (int i = pageCount - 1; i >= 0; i--) { |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 1180 | final View v = getPageAt(i); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1181 | if (v == mDragView) continue; |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 1182 | if (mForceDrawAllChildrenNextFrame || |
| 1183 | (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) { |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 1184 | drawChild(canvas, v, drawingTime); |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 1185 | } |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1186 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1187 | // Draw the drag view on top (if there is one) |
| 1188 | if (mDragView != null) { |
| 1189 | drawChild(canvas, mDragView, drawingTime); |
| 1190 | } |
| 1191 | |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 1192 | mForceDrawAllChildrenNextFrame = false; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 1193 | canvas.restore(); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 1194 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1195 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | @Override |
| 1199 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1200 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1201 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 1202 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1203 | return true; |
| 1204 | } |
| 1205 | return false; |
| 1206 | } |
| 1207 | |
| 1208 | @Override |
| 1209 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1210 | int focusablePage; |
| 1211 | if (mNextPage != INVALID_PAGE) { |
| 1212 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1213 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1214 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1215 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1216 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1217 | if (v != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1218 | return v.requestFocus(direction, previouslyFocusedRect); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1219 | } |
| 1220 | return false; |
| 1221 | } |
| 1222 | |
| 1223 | @Override |
| 1224 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1225 | // XXX-RTL: This will be fixed in a future CL |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1226 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1227 | if (getCurrentPage() > 0) { |
| 1228 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1229 | return true; |
| 1230 | } |
| 1231 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1232 | if (getCurrentPage() < getPageCount() - 1) { |
| 1233 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1234 | return true; |
| 1235 | } |
| 1236 | } |
| 1237 | return super.dispatchUnhandledMove(focused, direction); |
| 1238 | } |
| 1239 | |
| 1240 | @Override |
| 1241 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1242 | // XXX-RTL: This will be fixed in a future CL |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1243 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1244 | getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1245 | } |
| 1246 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1247 | if (mCurrentPage > 0) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1248 | getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1249 | } |
| 1250 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1251 | if (mCurrentPage < getPageCount() - 1) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 1252 | getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | /** |
| 1258 | * 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] | 1259 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1260 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1261 | * This happens when live folders requery, and if they're off page, they |
| 1262 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1263 | */ |
| 1264 | @Override |
| 1265 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1266 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1267 | View v = focused; |
| 1268 | while (true) { |
| 1269 | if (v == current) { |
| 1270 | super.focusableViewAvailable(focused); |
| 1271 | return; |
| 1272 | } |
| 1273 | if (v == this) { |
| 1274 | return; |
| 1275 | } |
| 1276 | ViewParent parent = v.getParent(); |
| 1277 | if (parent instanceof View) { |
| 1278 | v = (View)v.getParent(); |
| 1279 | } else { |
| 1280 | return; |
| 1281 | } |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | /** |
| 1286 | * {@inheritDoc} |
| 1287 | */ |
| 1288 | @Override |
| 1289 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 1290 | if (disallowIntercept) { |
| 1291 | // We need to make sure to cancel our long press if |
| 1292 | // a scrollable widget takes over touch events |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1293 | final View currentPage = getPageAt(mCurrentPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1294 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1295 | } |
| 1296 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 1297 | } |
| 1298 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1299 | /** |
| 1300 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 1301 | */ |
| 1302 | protected boolean hitsPreviousPage(float x, float y) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1303 | if (isLayoutRtl()) { |
| 1304 | return (x > (getViewportOffsetX() + getViewportWidth() - |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1305 | getPaddingRight() - mPageSpacing)); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1306 | } |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1307 | return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing); |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | /** |
| 1311 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 1312 | */ |
| 1313 | protected boolean hitsNextPage(float x, float y) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1314 | if (isLayoutRtl()) { |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1315 | return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing); |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 1316 | } |
| 1317 | return (x > (getViewportOffsetX() + getViewportWidth() - |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1318 | getPaddingRight() - mPageSpacing)); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1319 | } |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1320 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1321 | /** Returns whether x and y originated within the buffered viewport */ |
| 1322 | private boolean isTouchPointInViewportWithBuffer(int x, int y) { |
| 1323 | mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top, |
| 1324 | mViewport.right + mViewport.width() / 2, mViewport.bottom); |
| 1325 | return mTmpRect.contains(x, y); |
| 1326 | } |
| 1327 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1328 | @Override |
| 1329 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1330 | if (DISABLE_TOUCH_INTERACTION) { |
| 1331 | return false; |
| 1332 | } |
| 1333 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1334 | /* |
| 1335 | * This method JUST determines whether we want to intercept the motion. |
| 1336 | * If we return true, onTouchEvent will be called and we do the actual |
| 1337 | * scrolling there. |
| 1338 | */ |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1339 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1340 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1341 | // Skip touch handling if there are no pages to swipe |
| 1342 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 1343 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1344 | /* |
| 1345 | * Shortcut the most recurring case: the user is in the dragging |
| 1346 | * state and he is moving his finger. We want to intercept this |
| 1347 | * motion. |
| 1348 | */ |
| 1349 | final int action = ev.getAction(); |
| 1350 | if ((action == MotionEvent.ACTION_MOVE) && |
| 1351 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 1352 | return true; |
| 1353 | } |
| 1354 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1355 | switch (action & MotionEvent.ACTION_MASK) { |
| 1356 | case MotionEvent.ACTION_MOVE: { |
| 1357 | /* |
| 1358 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 1359 | * whether the user has moved far enough from his original down touch. |
| 1360 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1361 | if (mActivePointerId != INVALID_POINTER) { |
| 1362 | determineScrollingStart(ev); |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1363 | } |
| 1364 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 1365 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 1366 | // i.e. fall through to the next case (don't break) |
| 1367 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 1368 | // 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] | 1369 | break; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | case MotionEvent.ACTION_DOWN: { |
| 1373 | final float x = ev.getX(); |
| 1374 | final float y = ev.getY(); |
| 1375 | // Remember location of down touch |
| 1376 | mDownMotionX = x; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1377 | mDownMotionY = y; |
| 1378 | mDownScrollX = getScrollX(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1379 | mLastMotionX = x; |
| 1380 | mLastMotionY = y; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1381 | float[] p = mapPointFromViewToParent(this, x, y); |
| 1382 | mParentDownMotionX = p[0]; |
| 1383 | mParentDownMotionY = p[1]; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1384 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1385 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1386 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1387 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1388 | /* |
| 1389 | * If being flinged and user touches the screen, initiate drag; |
| 1390 | * otherwise don't. mScroller.isFinished should be false when |
| 1391 | * being flinged. |
| 1392 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame] | 1393 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Adam Cohen | a765215 | 2013-11-18 20:06:55 +0000 | [diff] [blame^] | 1394 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3); |
Adam Cohen | 2da0a05 | 2013-11-08 06:28:17 -0800 | [diff] [blame] | 1395 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1396 | if (finishedScrolling) { |
| 1397 | mTouchState = TOUCH_STATE_REST; |
Adam Cohen | 2da0a05 | 2013-11-08 06:28:17 -0800 | [diff] [blame] | 1398 | if (!mScroller.isFinished()) { |
Adam Cohen | a765215 | 2013-11-18 20:06:55 +0000 | [diff] [blame^] | 1399 | setCurrentPage(getNextPage()); |
| 1400 | pageEndMoving(); |
Adam Cohen | 2da0a05 | 2013-11-08 06:28:17 -0800 | [diff] [blame] | 1401 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1402 | } else { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1403 | if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) { |
| 1404 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1405 | } else { |
| 1406 | mTouchState = TOUCH_STATE_REST; |
| 1407 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1408 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1409 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1410 | // check if this can be the beginning of a tap on the side of the pages |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1411 | // to scroll the current page |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1412 | if (!DISABLE_TOUCH_SIDE_PAGES) { |
| 1413 | if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) { |
| 1414 | if (getChildCount() > 0) { |
| 1415 | if (hitsPreviousPage(x, y)) { |
| 1416 | mTouchState = TOUCH_STATE_PREV_PAGE; |
| 1417 | } else if (hitsNextPage(x, y)) { |
| 1418 | mTouchState = TOUCH_STATE_NEXT_PAGE; |
| 1419 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | break; |
| 1424 | } |
| 1425 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1426 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1427 | case MotionEvent.ACTION_CANCEL: |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1428 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1429 | break; |
| 1430 | |
| 1431 | case MotionEvent.ACTION_POINTER_UP: |
| 1432 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1433 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1434 | break; |
| 1435 | } |
| 1436 | |
| 1437 | /* |
| 1438 | * The only time we want to intercept motion events is if we are in the |
| 1439 | * drag mode. |
| 1440 | */ |
| 1441 | return mTouchState != TOUCH_STATE_REST; |
| 1442 | } |
| 1443 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1444 | protected void determineScrollingStart(MotionEvent ev) { |
| 1445 | determineScrollingStart(ev, 1.0f); |
| 1446 | } |
| 1447 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1448 | /* |
| 1449 | * Determines if we should change the touch state to start scrolling after the |
| 1450 | * user moves their touch point too far. |
| 1451 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1452 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1453 | // Disallow scrolling if we don't have a valid pointer index |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1454 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1455 | if (pointerIndex == -1) return; |
| 1456 | |
| 1457 | // Disallow scrolling if we started the gesture from outside the viewport |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1458 | final float x = ev.getX(pointerIndex); |
| 1459 | final float y = ev.getY(pointerIndex); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1460 | if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return; |
| 1461 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1462 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 1463 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 1464 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1465 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1466 | boolean xPaged = xDiff > mPagingTouchSlop; |
| 1467 | boolean xMoved = xDiff > touchSlop; |
| 1468 | boolean yMoved = yDiff > touchSlop; |
| 1469 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1470 | if (xMoved || xPaged || yMoved) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1471 | if (mUsePagingTouchSlop ? xPaged : xMoved) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1472 | // Scroll if the user moved far enough along the X axis |
| 1473 | mTouchState = TOUCH_STATE_SCROLLING; |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1474 | mTotalMotionX += Math.abs(mLastMotionX - x); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1475 | mLastMotionX = x; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1476 | mLastMotionXRemainder = 0; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1477 | mTouchX = getViewportOffsetX() + getScrollX(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1478 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1479 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1480 | } |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1484 | protected float getMaxScrollProgress() { |
| 1485 | return 1.0f; |
| 1486 | } |
| 1487 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1488 | protected void cancelCurrentPageLongPress() { |
| 1489 | if (mAllowLongPress) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1490 | //mAllowLongPress = false; |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1491 | // Try canceling the long press. It could also have been scheduled |
| 1492 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 1493 | // everything |
| 1494 | final View currentPage = getPageAt(mCurrentPage); |
| 1495 | if (currentPage != null) { |
| 1496 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1497 | } |
| 1498 | } |
| 1499 | } |
| 1500 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1501 | protected float getBoundedScrollProgress(int screenCenter, View v, int page) { |
| 1502 | final int halfScreenSize = getViewportWidth() / 2; |
| 1503 | |
| 1504 | screenCenter = Math.min(getScrollX() + halfScreenSize, screenCenter); |
| 1505 | screenCenter = Math.max(halfScreenSize, screenCenter); |
| 1506 | |
| 1507 | return getScrollProgress(screenCenter, v, page); |
| 1508 | } |
| 1509 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1510 | protected float getScrollProgress(int screenCenter, View v, int page) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1511 | final int halfScreenSize = getViewportWidth() / 2; |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1512 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1513 | int delta = screenCenter - (getScrollForPage(page) + halfScreenSize); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1514 | int count = getChildCount(); |
| 1515 | |
| 1516 | final int totalDistance; |
| 1517 | |
| 1518 | int adjacentPage = page + 1; |
| 1519 | if ((delta < 0 && !isLayoutRtl()) || (delta > 0 && isLayoutRtl())) { |
| 1520 | adjacentPage = page - 1; |
| 1521 | } |
| 1522 | |
| 1523 | if (adjacentPage < 0 || adjacentPage > count - 1) { |
| 1524 | totalDistance = v.getMeasuredWidth() + mPageSpacing; |
| 1525 | } else { |
| 1526 | totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page)); |
| 1527 | } |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1528 | |
| 1529 | float scrollProgress = delta / (totalDistance * 1.0f); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1530 | scrollProgress = Math.min(scrollProgress, getMaxScrollProgress()); |
| 1531 | scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress()); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1532 | return scrollProgress; |
| 1533 | } |
| 1534 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1535 | public int getScrollForPage(int index) { |
Adam Cohen | 1f1f45d | 2013-10-02 09:40:18 -0700 | [diff] [blame] | 1536 | if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) { |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 1537 | return 0; |
| 1538 | } else { |
| 1539 | return mPageScrolls[index]; |
| 1540 | } |
| 1541 | } |
| 1542 | |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1543 | // While layout transitions are occurring, a child's position may stray from its baseline |
| 1544 | // position. This method returns the magnitude of this stray at any given time. |
| 1545 | public int getLayoutTransitionOffsetForPage(int index) { |
| 1546 | if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) { |
| 1547 | return 0; |
| 1548 | } else { |
| 1549 | View child = getChildAt(index); |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 1550 | |
| 1551 | int scrollOffset = 0; |
| 1552 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 1553 | if (!lp.isFullScreenPage) { |
| 1554 | scrollOffset = isLayoutRtl() ? getPaddingRight() : getPaddingLeft(); |
| 1555 | } |
| 1556 | |
Adam Cohen | 564a2e7 | 2013-10-09 14:47:32 -0700 | [diff] [blame] | 1557 | int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX(); |
| 1558 | return (int) (child.getX() - baselineX); |
| 1559 | } |
| 1560 | } |
| 1561 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1562 | // This curve determines how the effect of scrolling over the limits of the page dimishes |
| 1563 | // as the user pulls further and further from the bounds |
| 1564 | private float overScrollInfluenceCurve(float f) { |
| 1565 | f -= 1.0f; |
| 1566 | return f * f * f + 1.0f; |
| 1567 | } |
| 1568 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1569 | protected void acceleratedOverScroll(float amount) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1570 | int screenSize = getViewportWidth(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1571 | |
| 1572 | // We want to reach the max over scroll effect when the user has |
| 1573 | // over scrolled half the size of the screen |
| 1574 | float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize); |
| 1575 | |
| 1576 | if (f == 0) return; |
| 1577 | |
| 1578 | // Clamp this factor, f, to -1 < f < 1 |
| 1579 | if (Math.abs(f) >= 1) { |
| 1580 | f /= Math.abs(f); |
| 1581 | } |
| 1582 | |
| 1583 | int overScrollAmount = (int) Math.round(f * screenSize); |
| 1584 | if (amount < 0) { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1585 | mOverScrollX = overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1586 | super.scrollTo(0, getScrollY()); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1587 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1588 | mOverScrollX = mMaxScrollX + overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1589 | super.scrollTo(mMaxScrollX, getScrollY()); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1590 | } |
| 1591 | invalidate(); |
| 1592 | } |
| 1593 | |
| 1594 | protected void dampedOverScroll(float amount) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1595 | int screenSize = getViewportWidth(); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1596 | |
| 1597 | float f = (amount / screenSize); |
| 1598 | |
| 1599 | if (f == 0) return; |
| 1600 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 1601 | |
Adam Cohen | 7bfc979 | 2011-01-28 13:52:37 -0800 | [diff] [blame] | 1602 | // Clamp this factor, f, to -1 < f < 1 |
| 1603 | if (Math.abs(f) >= 1) { |
| 1604 | f /= Math.abs(f); |
| 1605 | } |
| 1606 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1607 | int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1608 | if (amount < 0) { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1609 | mOverScrollX = overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1610 | super.scrollTo(0, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1611 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1612 | mOverScrollX = mMaxScrollX + overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1613 | super.scrollTo(mMaxScrollX, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1614 | } |
| 1615 | invalidate(); |
| 1616 | } |
| 1617 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1618 | protected void overScroll(float amount) { |
| 1619 | dampedOverScroll(amount); |
| 1620 | } |
| 1621 | |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1622 | protected float maxOverScroll() { |
| 1623 | // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1624 | // exceed). Used to find out how much extra wallpaper we need for the over scroll effect |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1625 | float f = 1.0f; |
| 1626 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 1627 | return OVERSCROLL_DAMP_FACTOR * f; |
| 1628 | } |
| 1629 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1630 | protected void enableFreeScroll() { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1631 | setEnableFreeScroll(true); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1634 | protected void disableFreeScroll() { |
| 1635 | setEnableFreeScroll(false); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 1638 | void updateFreescrollBounds() { |
| 1639 | getOverviewModePages(mTempVisiblePagesRange); |
| 1640 | if (isLayoutRtl()) { |
| 1641 | mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]); |
| 1642 | mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]); |
| 1643 | } else { |
| 1644 | mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]); |
| 1645 | mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]); |
| 1646 | } |
| 1647 | } |
| 1648 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1649 | private void setEnableFreeScroll(boolean freeScroll) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1650 | mFreeScroll = freeScroll; |
| 1651 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1652 | if (mFreeScroll) { |
Adam Cohen | 7a9e58a | 2013-10-01 18:02:13 -0700 | [diff] [blame] | 1653 | updateFreescrollBounds(); |
| 1654 | getOverviewModePages(mTempVisiblePagesRange); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1655 | if (getCurrentPage() < mTempVisiblePagesRange[0]) { |
| 1656 | setCurrentPage(mTempVisiblePagesRange[0]); |
| 1657 | } else if (getCurrentPage() > mTempVisiblePagesRange[1]) { |
| 1658 | setCurrentPage(mTempVisiblePagesRange[1]); |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | setEnableOverscroll(!freeScroll); |
| 1663 | } |
| 1664 | |
| 1665 | private void setEnableOverscroll(boolean enable) { |
| 1666 | mAllowOverScroll = enable; |
| 1667 | } |
| 1668 | |
| 1669 | int getNearestHoverOverPageIndex() { |
| 1670 | if (mDragView != null) { |
| 1671 | int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2) |
| 1672 | + mDragView.getTranslationX()); |
| 1673 | getOverviewModePages(mTempVisiblePagesRange); |
| 1674 | int minDistance = Integer.MAX_VALUE; |
| 1675 | int minIndex = indexOfChild(mDragView); |
| 1676 | for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) { |
| 1677 | View page = getPageAt(i); |
| 1678 | int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2); |
| 1679 | int d = Math.abs(dragX - pageX); |
| 1680 | if (d < minDistance) { |
| 1681 | minIndex = i; |
| 1682 | minDistance = d; |
| 1683 | } |
| 1684 | } |
| 1685 | return minIndex; |
| 1686 | } |
| 1687 | return -1; |
| 1688 | } |
| 1689 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1690 | @Override |
| 1691 | public boolean onTouchEvent(MotionEvent ev) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1692 | if (DISABLE_TOUCH_INTERACTION) { |
| 1693 | return false; |
| 1694 | } |
| 1695 | |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 1696 | super.onTouchEvent(ev); |
| 1697 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1698 | // Skip touch handling if there are no pages to swipe |
| 1699 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 1700 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1701 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1702 | |
| 1703 | final int action = ev.getAction(); |
| 1704 | |
| 1705 | switch (action & MotionEvent.ACTION_MASK) { |
| 1706 | case MotionEvent.ACTION_DOWN: |
| 1707 | /* |
| 1708 | * If being flinged and user touches, stop the fling. isFinished |
| 1709 | * will be false if being flinged. |
| 1710 | */ |
| 1711 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 1712 | abortScrollerAnimation(false); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | // Remember where the motion event started |
| 1716 | mDownMotionX = mLastMotionX = ev.getX(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1717 | mDownMotionY = mLastMotionY = ev.getY(); |
| 1718 | mDownScrollX = getScrollX(); |
| 1719 | float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1720 | mParentDownMotionX = p[0]; |
| 1721 | mParentDownMotionY = p[1]; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1722 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1723 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1724 | mActivePointerId = ev.getPointerId(0); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1725 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1726 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1727 | pageBeginMoving(); |
| 1728 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1729 | break; |
| 1730 | |
| 1731 | case MotionEvent.ACTION_MOVE: |
| 1732 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1733 | // Scroll to follow the motion event |
| 1734 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1735 | |
| 1736 | if (pointerIndex == -1) return true; |
| 1737 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1738 | final float x = ev.getX(pointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1739 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1740 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1741 | mTotalMotionX += Math.abs(deltaX); |
| 1742 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1743 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1744 | // keep the remainder because we are actually testing if we've moved from the last |
| 1745 | // scrolled position (which is discrete). |
| 1746 | if (Math.abs(deltaX) >= 1.0f) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1747 | mTouchX += deltaX; |
| 1748 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1749 | if (!mDeferScrollUpdate) { |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1750 | scrollBy((int) deltaX, 0); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1751 | if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1752 | } else { |
| 1753 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1754 | } |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1755 | mLastMotionX = x; |
| 1756 | mLastMotionXRemainder = deltaX - (int) deltaX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1757 | } else { |
| 1758 | awakenScrollBars(); |
| 1759 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1760 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1761 | // Update the last motion position |
| 1762 | mLastMotionX = ev.getX(); |
| 1763 | mLastMotionY = ev.getY(); |
| 1764 | |
| 1765 | // Update the parent down so that our zoom animations take this new movement into |
| 1766 | // account |
| 1767 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1768 | mParentDownMotionX = pt[0]; |
| 1769 | mParentDownMotionY = pt[1]; |
| 1770 | updateDragViewTranslationDuringDrag(); |
| 1771 | |
| 1772 | // Find the closest page to the touch point |
| 1773 | final int dragViewIndex = indexOfChild(mDragView); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1774 | |
| 1775 | // Change the drag view if we are hovering over the drop target |
| 1776 | boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget( |
| 1777 | (int) mParentDownMotionX, (int) mParentDownMotionY); |
| 1778 | setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete); |
| 1779 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1780 | if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX); |
| 1781 | if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY); |
| 1782 | if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX); |
| 1783 | if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY); |
| 1784 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1785 | final int pageUnderPointIndex = getNearestHoverOverPageIndex(); |
| 1786 | if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView) && |
| 1787 | !isHoveringOverDelete) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1788 | mTempVisiblePagesRange[0] = 0; |
| 1789 | mTempVisiblePagesRange[1] = getPageCount() - 1; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1790 | getOverviewModePages(mTempVisiblePagesRange); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1791 | if (mTempVisiblePagesRange[0] <= pageUnderPointIndex && |
| 1792 | pageUnderPointIndex <= mTempVisiblePagesRange[1] && |
| 1793 | pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) { |
| 1794 | mSidePageHoverIndex = pageUnderPointIndex; |
| 1795 | mSidePageHoverRunnable = new Runnable() { |
| 1796 | @Override |
| 1797 | public void run() { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1798 | // Setup the scroll to the correct page before we swap the views |
| 1799 | snapToPage(pageUnderPointIndex); |
| 1800 | |
| 1801 | // For each of the pages between the paged view and the drag view, |
| 1802 | // animate them from the previous position to the new position in |
| 1803 | // the layout (as a result of the drag view moving in the layout) |
| 1804 | int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1; |
| 1805 | int lowerIndex = (dragViewIndex < pageUnderPointIndex) ? |
| 1806 | dragViewIndex + 1 : pageUnderPointIndex; |
| 1807 | int upperIndex = (dragViewIndex > pageUnderPointIndex) ? |
| 1808 | dragViewIndex - 1 : pageUnderPointIndex; |
| 1809 | for (int i = lowerIndex; i <= upperIndex; ++i) { |
| 1810 | View v = getChildAt(i); |
| 1811 | // dragViewIndex < pageUnderPointIndex, so after we remove the |
| 1812 | // drag view all subsequent views to pageUnderPointIndex will |
| 1813 | // shift down. |
| 1814 | int oldX = getViewportOffsetX() + getChildOffset(i); |
| 1815 | int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta); |
| 1816 | |
| 1817 | // Animate the view translation from its old position to its new |
| 1818 | // position |
| 1819 | AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY); |
| 1820 | if (anim != null) { |
| 1821 | anim.cancel(); |
| 1822 | } |
| 1823 | |
| 1824 | v.setTranslationX(oldX - newX); |
| 1825 | anim = new AnimatorSet(); |
| 1826 | anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION); |
| 1827 | anim.playTogether( |
| 1828 | ObjectAnimator.ofFloat(v, "translationX", 0f)); |
| 1829 | anim.start(); |
| 1830 | v.setTag(anim); |
| 1831 | } |
| 1832 | |
| 1833 | removeView(mDragView); |
| 1834 | onRemoveView(mDragView, false); |
| 1835 | addView(mDragView, pageUnderPointIndex); |
| 1836 | onAddView(mDragView, pageUnderPointIndex); |
| 1837 | mSidePageHoverIndex = -1; |
Winson Chung | 876a619 | 2013-11-06 14:49:50 -0800 | [diff] [blame] | 1838 | if (mPageIndicator != null) { |
| 1839 | mPageIndicator.setActiveMarker(getNextPage()); |
| 1840 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1841 | } |
| 1842 | }; |
| 1843 | postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT); |
| 1844 | } |
| 1845 | } else { |
| 1846 | removeCallbacks(mSidePageHoverRunnable); |
| 1847 | mSidePageHoverIndex = -1; |
| 1848 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1849 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1850 | determineScrollingStart(ev); |
| 1851 | } |
| 1852 | break; |
| 1853 | |
| 1854 | case MotionEvent.ACTION_UP: |
| 1855 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1856 | final int activePointerId = mActivePointerId; |
| 1857 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 1858 | final float x = ev.getX(pointerIndex); |
| 1859 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1860 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1861 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 1862 | final int deltaX = (int) (x - mDownMotionX); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 1863 | final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth(); |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1864 | boolean isSignificantMove = Math.abs(deltaX) > pageWidth * |
| 1865 | SIGNIFICANT_MOVE_THRESHOLD; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1866 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1867 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1868 | |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1869 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1870 | Math.abs(velocityX) > mFlingThresholdVelocity; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1871 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1872 | if (!mFreeScroll) { |
| 1873 | // In the case that the page is moved far to one direction and then is flung |
| 1874 | // in the opposite direction, we use a threshold to determine whether we should |
| 1875 | // just return to the starting page, or if we should skip one further. |
| 1876 | boolean returnToOriginalPage = false; |
| 1877 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
| 1878 | Math.signum(velocityX) != Math.signum(deltaX) && isFling) { |
| 1879 | returnToOriginalPage = true; |
| 1880 | } |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1881 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1882 | int finalPage; |
| 1883 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1884 | // test for a large move if a fling has been registered. That is, a large |
| 1885 | // move to the left and fling to the right will register as a fling to the right. |
| 1886 | final boolean isRtl = isLayoutRtl(); |
| 1887 | boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0; |
| 1888 | boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0; |
| 1889 | if (((isSignificantMove && !isDeltaXLeft && !isFling) || |
| 1890 | (isFling && !isVelocityXLeft)) && mCurrentPage > 0) { |
| 1891 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1892 | snapToPageWithVelocity(finalPage, velocityX); |
| 1893 | } else if (((isSignificantMove && isDeltaXLeft && !isFling) || |
| 1894 | (isFling && isVelocityXLeft)) && |
| 1895 | mCurrentPage < getChildCount() - 1) { |
| 1896 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1897 | snapToPageWithVelocity(finalPage, velocityX); |
| 1898 | } else { |
| 1899 | snapToDestination(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1900 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1901 | } else { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1902 | if (!mScroller.isFinished()) { |
Adam Cohen | 8bdbaab | 2013-10-29 15:25:02 -0700 | [diff] [blame] | 1903 | abortScrollerAnimation(true); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | float scaleX = getScaleX(); |
| 1907 | int vX = (int) (-velocityX * scaleX); |
| 1908 | int initialScrollX = (int) (getScrollX() * scaleX); |
| 1909 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 1910 | mScroller.setInterpolator(mDefaultInterpolator); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1911 | mScroller.fling(initialScrollX, |
| 1912 | getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0); |
| 1913 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1914 | } |
Adam Cohen | cae7f57 | 2013-11-04 14:42:52 -0800 | [diff] [blame] | 1915 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
| 1916 | // at this point we have not moved beyond the touch slop |
| 1917 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1918 | // we can just page |
| 1919 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1920 | if (nextPage != mCurrentPage) { |
| 1921 | snapToPage(nextPage); |
| 1922 | } else { |
| 1923 | snapToDestination(); |
| 1924 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1925 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1926 | // at this point we have not moved beyond the touch slop |
| 1927 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1928 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1929 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1930 | if (nextPage != mCurrentPage) { |
| 1931 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1932 | } else { |
| 1933 | snapToDestination(); |
| 1934 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1935 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1936 | // Update the last motion position |
| 1937 | mLastMotionX = ev.getX(); |
| 1938 | mLastMotionY = ev.getY(); |
| 1939 | |
| 1940 | // Update the parent down so that our zoom animations take this new movement into |
| 1941 | // account |
| 1942 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1943 | mParentDownMotionX = pt[0]; |
| 1944 | mParentDownMotionY = pt[1]; |
| 1945 | updateDragViewTranslationDuringDrag(); |
| 1946 | boolean handledFling = false; |
| 1947 | if (!DISABLE_FLING_TO_DELETE) { |
| 1948 | // Check the velocity and see if we are flinging-to-delete |
| 1949 | PointF flingToDeleteVector = isFlingingToDelete(); |
| 1950 | if (flingToDeleteVector != null) { |
| 1951 | onFlingToDelete(flingToDeleteVector); |
| 1952 | handledFling = true; |
| 1953 | } |
| 1954 | } |
| 1955 | if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX, |
| 1956 | (int) mParentDownMotionY)) { |
| 1957 | onDropToDelete(); |
| 1958 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1959 | } else { |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1960 | if (!mCancelTap) { |
| 1961 | onUnhandledTap(ev); |
| 1962 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1963 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1964 | |
| 1965 | // Remove the callback to wait for the side page hover timeout |
| 1966 | removeCallbacks(mSidePageHoverRunnable); |
| 1967 | // End any intermediate reordering states |
| 1968 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1969 | break; |
| 1970 | |
| 1971 | case MotionEvent.ACTION_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1972 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1973 | snapToDestination(); |
| 1974 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1975 | resetTouchState(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1976 | break; |
| 1977 | |
| 1978 | case MotionEvent.ACTION_POINTER_UP: |
| 1979 | onSecondaryPointerUp(ev); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 1980 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1981 | break; |
| 1982 | } |
| 1983 | |
| 1984 | return true; |
| 1985 | } |
| 1986 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1987 | public void onFlingToDelete(View v) {} |
| 1988 | public void onRemoveView(View v, boolean deletePermanently) {} |
| 1989 | public void onRemoveViewAnimationCompleted() {} |
| 1990 | public void onAddView(View v, int index) {} |
| 1991 | |
| 1992 | private void resetTouchState() { |
| 1993 | releaseVelocityTracker(); |
| 1994 | endReordering(); |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 1995 | mCancelTap = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1996 | mTouchState = TOUCH_STATE_REST; |
| 1997 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1998 | } |
| 1999 | |
Adam Cohen | 1697b79 | 2013-09-17 19:08:21 -0700 | [diff] [blame] | 2000 | protected void onUnhandledTap(MotionEvent ev) { |
| 2001 | ((Launcher) getContext()).onClick(this); |
| 2002 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2003 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 2004 | @Override |
| 2005 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 2006 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 2007 | switch (event.getAction()) { |
| 2008 | case MotionEvent.ACTION_SCROLL: { |
| 2009 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 2010 | final float vscroll; |
| 2011 | final float hscroll; |
| 2012 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 2013 | vscroll = 0; |
| 2014 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 2015 | } else { |
| 2016 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 2017 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 2018 | } |
| 2019 | if (hscroll != 0 || vscroll != 0) { |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 2020 | boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0) |
| 2021 | : (hscroll > 0 || vscroll > 0); |
| 2022 | if (isForwardScroll) { |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 2023 | scrollRight(); |
| 2024 | } else { |
| 2025 | scrollLeft(); |
| 2026 | } |
| 2027 | return true; |
| 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | } |
| 2032 | return super.onGenericMotionEvent(event); |
| 2033 | } |
| 2034 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 2035 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 2036 | if (mVelocityTracker == null) { |
| 2037 | mVelocityTracker = VelocityTracker.obtain(); |
| 2038 | } |
| 2039 | mVelocityTracker.addMovement(ev); |
| 2040 | } |
| 2041 | |
| 2042 | private void releaseVelocityTracker() { |
| 2043 | if (mVelocityTracker != null) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2044 | mVelocityTracker.clear(); |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 2045 | mVelocityTracker.recycle(); |
| 2046 | mVelocityTracker = null; |
| 2047 | } |
| 2048 | } |
| 2049 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2050 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 2051 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 2052 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 2053 | final int pointerId = ev.getPointerId(pointerIndex); |
| 2054 | if (pointerId == mActivePointerId) { |
| 2055 | // This was our active pointer going up. Choose a new |
| 2056 | // active pointer and adjust accordingly. |
| 2057 | // TODO: Make this decision more intelligent. |
| 2058 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 2059 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 2060 | mLastMotionY = ev.getY(newPointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 2061 | mLastMotionXRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2062 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 2063 | if (mVelocityTracker != null) { |
| 2064 | mVelocityTracker.clear(); |
| 2065 | } |
| 2066 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 2067 | } |
| 2068 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2069 | @Override |
| 2070 | public void requestChildFocus(View child, View focused) { |
| 2071 | super.requestChildFocus(child, focused); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 2072 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 2073 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2074 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2075 | } |
| 2076 | } |
| 2077 | |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 2078 | protected int getChildWidth(int index) { |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 2079 | return getPageAt(index).getMeasuredWidth(); |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 2080 | } |
| 2081 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2082 | int getPageNearestToPoint(float x) { |
| 2083 | int index = 0; |
| 2084 | for (int i = 0; i < getChildCount(); ++i) { |
| 2085 | if (x < getChildAt(i).getRight() - getScrollX()) { |
| 2086 | return index; |
| 2087 | } else { |
| 2088 | index++; |
| 2089 | } |
| 2090 | } |
| 2091 | return Math.min(index, getChildCount() - 1); |
| 2092 | } |
| 2093 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 2094 | int getPageNearestToCenterOfScreen() { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 2095 | int minDistanceFromScreenCenter = Integer.MAX_VALUE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2096 | int minDistanceFromScreenCenterIndex = -1; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2097 | int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2098 | final int childCount = getChildCount(); |
| 2099 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 2100 | View layout = (View) getPageAt(i); |
Adam Cohen | 96d30a1 | 2013-07-16 18:13:21 -0700 | [diff] [blame] | 2101 | int childWidth = layout.getMeasuredWidth(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2102 | int halfChildWidth = (childWidth / 2); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2103 | int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2104 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 2105 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 2106 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 2107 | minDistanceFromScreenCenterIndex = i; |
| 2108 | } |
| 2109 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 2110 | return minDistanceFromScreenCenterIndex; |
| 2111 | } |
| 2112 | |
| 2113 | protected void snapToDestination() { |
| 2114 | snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2115 | } |
| 2116 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2117 | private static class ScrollInterpolator implements Interpolator { |
| 2118 | public ScrollInterpolator() { |
| 2119 | } |
| 2120 | |
| 2121 | public float getInterpolation(float t) { |
| 2122 | t -= 1.0f; |
| 2123 | return t*t*t*t*t + 1; |
| 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | // We want the duration of the page snap animation to be influenced by the distance that |
| 2128 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 2129 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 2130 | // of travel has on the overall snap duration. |
| 2131 | float distanceInfluenceForSnapDuration(float f) { |
| 2132 | f -= 0.5f; // center the values about 0. |
| 2133 | f *= 0.3f * Math.PI / 2.0f; |
| 2134 | return (float) Math.sin(f); |
| 2135 | } |
| 2136 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2137 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2138 | whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1)); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2139 | int halfScreenSize = getViewportWidth() / 2; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2140 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 2141 | final int newX = getScrollForPage(whichPage); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2142 | int delta = newX - mUnboundedScrollX; |
| 2143 | int duration = 0; |
| 2144 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 2145 | if (Math.abs(velocity) < mMinFlingVelocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2146 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 2147 | // as opposed to an apparent physical response to flinging |
| 2148 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
| 2149 | return; |
| 2150 | } |
| 2151 | |
| 2152 | // Here we compute a "distance" that will be used in the computation of the overall |
| 2153 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 2154 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 2155 | // duration as a function of the distance the page needs to travel. |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 2156 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2157 | float distance = halfScreenSize + halfScreenSize * |
| 2158 | distanceInfluenceForSnapDuration(distanceRatio); |
| 2159 | |
| 2160 | velocity = Math.abs(velocity); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 2161 | velocity = Math.max(mMinSnapVelocity, velocity); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2162 | |
| 2163 | // we want the page's snap velocity to approximately match the velocity at which the |
| 2164 | // 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] | 2165 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 2166 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 2167 | |
| 2168 | snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | protected void snapToPage(int whichPage) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 2172 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2175 | protected void snapToPageImmediately(int whichPage) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2176 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2179 | protected void snapToPage(int whichPage, int duration) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2180 | snapToPage(whichPage, duration, false, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2181 | } |
| 2182 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2183 | protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) { |
| 2184 | snapToPage(whichPage, duration, false, interpolator); |
| 2185 | } |
| 2186 | |
| 2187 | protected void snapToPage(int whichPage, int duration, boolean immediate, |
| 2188 | TimeInterpolator interpolator) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2189 | whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2190 | |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 2191 | int newX = getScrollForPage(whichPage); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 2192 | final int sX = mUnboundedScrollX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2193 | final int delta = newX - sX; |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2194 | snapToPage(whichPage, delta, duration, immediate, interpolator); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | protected void snapToPage(int whichPage, int delta, int duration) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2198 | snapToPage(whichPage, delta, duration, false, null); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2199 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2200 | |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2201 | protected void snapToPage(int whichPage, int delta, int duration, boolean immediate, |
| 2202 | TimeInterpolator interpolator) { |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2203 | mNextPage = whichPage; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2204 | View focusedChild = getFocusedChild(); |
| 2205 | if (focusedChild != null && whichPage != mCurrentPage && |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 2206 | focusedChild == getPageAt(mCurrentPage)) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2207 | focusedChild.clearFocus(); |
| 2208 | } |
| 2209 | |
Adam Cohen | 5380521 | 2013-10-01 10:39:23 -0700 | [diff] [blame] | 2210 | sendScrollAccessibilityEvent(); |
| 2211 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2212 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2213 | awakenScrollBars(duration); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2214 | if (immediate) { |
| 2215 | duration = 0; |
| 2216 | } else if (duration == 0) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2217 | duration = Math.abs(delta); |
| 2218 | } |
| 2219 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2220 | if (!mScroller.isFinished()) { |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2221 | abortScrollerAnimation(false); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2222 | } |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2223 | |
| 2224 | if (interpolator != null) { |
| 2225 | mScroller.setInterpolator(interpolator); |
| 2226 | } else { |
| 2227 | mScroller.setInterpolator(mDefaultInterpolator); |
| 2228 | } |
| 2229 | |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 2230 | mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 2231 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2232 | notifyPageSwitchListener(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2233 | |
| 2234 | // Trigger a compute() to finish switching pages if necessary |
| 2235 | if (immediate) { |
| 2236 | computeScroll(); |
| 2237 | } |
| 2238 | |
Winson Chung | 9c0565f | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 2239 | // Defer loading associated pages until the scroll settles |
| 2240 | mDeferLoadAssociatedPagesUntilScrollCompletes = true; |
| 2241 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2242 | mForceScreenScrolled = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2243 | invalidate(); |
| 2244 | } |
| 2245 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2246 | public void scrollLeft() { |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 2247 | if (getNextPage() > 0) snapToPage(getNextPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | public void scrollRight() { |
Adam Cohen | 2bf63d5 | 2013-09-29 17:46:49 -0700 | [diff] [blame] | 2251 | if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2254 | public int getPageForView(View v) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2255 | int result = -1; |
| 2256 | if (v != null) { |
| 2257 | ViewParent vp = v.getParent(); |
| 2258 | int count = getChildCount(); |
| 2259 | for (int i = 0; i < count; i++) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 2260 | if (vp == getPageAt(i)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2261 | return i; |
| 2262 | } |
| 2263 | } |
| 2264 | } |
| 2265 | return result; |
| 2266 | } |
| 2267 | |
| 2268 | /** |
| 2269 | * @return True is long presses are still allowed for the current touch |
| 2270 | */ |
| 2271 | public boolean allowLongPress() { |
| 2272 | return mAllowLongPress; |
| 2273 | } |
| 2274 | |
Adam Cohen | dbdff6b | 2013-09-18 19:09:15 -0700 | [diff] [blame] | 2275 | @Override |
| 2276 | public boolean performLongClick() { |
| 2277 | mCancelTap = true; |
| 2278 | return super.performLongClick(); |
| 2279 | } |
| 2280 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2281 | /** |
| 2282 | * Set true to allow long-press events to be triggered, usually checked by |
| 2283 | * {@link Launcher} to accept or block dpad-initiated long-presses. |
| 2284 | */ |
| 2285 | public void setAllowLongPress(boolean allowLongPress) { |
| 2286 | mAllowLongPress = allowLongPress; |
| 2287 | } |
| 2288 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2289 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2290 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2291 | |
| 2292 | SavedState(Parcelable superState) { |
| 2293 | super(superState); |
| 2294 | } |
| 2295 | |
| 2296 | private SavedState(Parcel in) { |
| 2297 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2298 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | @Override |
| 2302 | public void writeToParcel(Parcel out, int flags) { |
| 2303 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2304 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 2308 | new Parcelable.Creator<SavedState>() { |
| 2309 | public SavedState createFromParcel(Parcel in) { |
| 2310 | return new SavedState(in); |
| 2311 | } |
| 2312 | |
| 2313 | public SavedState[] newArray(int size) { |
| 2314 | return new SavedState[size]; |
| 2315 | } |
| 2316 | }; |
| 2317 | } |
| 2318 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2319 | protected void loadAssociatedPages(int page) { |
| 2320 | loadAssociatedPages(page, false); |
| 2321 | } |
| 2322 | protected void loadAssociatedPages(int page, boolean immediateAndOnly) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2323 | if (mContentIsRefreshable) { |
| 2324 | final int count = getChildCount(); |
| 2325 | if (page < count) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 2326 | int lowerPageBound = getAssociatedLowerPageBound(page); |
| 2327 | int upperPageBound = getAssociatedUpperPageBound(page); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 2328 | if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/" |
| 2329 | + upperPageBound); |
Michael Jurka | 0cad111 | 2011-11-16 20:43:29 -0800 | [diff] [blame] | 2330 | // First, clear any pages that should no longer be loaded |
| 2331 | for (int i = 0; i < count; ++i) { |
| 2332 | Page layout = (Page) getPageAt(i); |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 2333 | if ((i < lowerPageBound) || (i > upperPageBound)) { |
Michael Jurka | 0cad111 | 2011-11-16 20:43:29 -0800 | [diff] [blame] | 2334 | if (layout.getPageChildCount() > 0) { |
| 2335 | layout.removeAllViewsOnPage(); |
| 2336 | } |
| 2337 | mDirtyPageContent.set(i, true); |
| 2338 | } |
| 2339 | } |
| 2340 | // Next, load any new pages |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2341 | for (int i = 0; i < count; ++i) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2342 | if ((i != page) && immediateAndOnly) { |
| 2343 | continue; |
| 2344 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2345 | if (lowerPageBound <= i && i <= upperPageBound) { |
| 2346 | if (mDirtyPageContent.get(i)) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2347 | syncPageItems(i, (i == page) && immediateAndOnly); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2348 | mDirtyPageContent.set(i, false); |
| 2349 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2350 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 2351 | } |
| 2352 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 2353 | } |
| 2354 | } |
| 2355 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 2356 | protected int getAssociatedLowerPageBound(int page) { |
| 2357 | return Math.max(0, page - 1); |
| 2358 | } |
| 2359 | protected int getAssociatedUpperPageBound(int page) { |
| 2360 | final int count = getChildCount(); |
| 2361 | return Math.min(page + 1, count - 1); |
| 2362 | } |
| 2363 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2364 | /** |
| 2365 | * This method is called ONLY to synchronize the number of pages that the paged view has. |
| 2366 | * To actually fill the pages with information, implement syncPageItems() below. It is |
| 2367 | * guaranteed that syncPageItems() will be called for a particular page before it is shown, |
| 2368 | * and therefore, individual page items do not need to be updated in this method. |
| 2369 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2370 | public abstract void syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2371 | |
| 2372 | /** |
| 2373 | * This method is called to synchronize the items that are on a particular page. If views on |
| 2374 | * the page can be reused, then they should be updated within this method. |
| 2375 | */ |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2376 | public abstract void syncPageItems(int page, boolean immediate); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2377 | |
Patrick Dubroy | 244d74c | 2011-05-19 16:48:48 -0700 | [diff] [blame] | 2378 | protected void invalidatePageData() { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2379 | invalidatePageData(-1, false); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 2380 | } |
| 2381 | protected void invalidatePageData(int currentPage) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2382 | invalidatePageData(currentPage, false); |
| 2383 | } |
| 2384 | protected void invalidatePageData(int currentPage, boolean immediateAndOnly) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 2385 | if (!mIsDataReady) { |
| 2386 | return; |
| 2387 | } |
| 2388 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2389 | if (mContentIsRefreshable) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 2390 | // Force all scrolling-related behavior to end |
Adam Cohen | 4fe4c93 | 2013-10-28 16:02:34 -0700 | [diff] [blame] | 2391 | forceFinishScroller(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 2392 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2393 | // Update all the pages |
| 2394 | syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2395 | |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 2396 | // We must force a measure after we've loaded the pages to update the content width and |
| 2397 | // to determine the full scroll width |
| 2398 | measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), |
| 2399 | MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)); |
| 2400 | |
| 2401 | // Set a new page as the current page if necessary |
| 2402 | if (currentPage > -1) { |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 2403 | setCurrentPage(Math.min(getPageCount() - 1, currentPage)); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2406 | // Mark each of the pages as dirty |
| 2407 | final int count = getChildCount(); |
| 2408 | mDirtyPageContent.clear(); |
| 2409 | for (int i = 0; i < count; ++i) { |
| 2410 | mDirtyPageContent.add(true); |
| 2411 | } |
| 2412 | |
| 2413 | // Load any pages that are necessary for the current window of views |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 2414 | loadAssociatedPages(mCurrentPage, immediateAndOnly); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 2415 | requestLayout(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 2416 | } |
Adam Cohen | 0655904 | 2013-09-29 18:30:56 -0700 | [diff] [blame] | 2417 | if (isPageMoving()) { |
| 2418 | // If the page is moving, then snap it to the final position to ensure we don't get |
| 2419 | // stuck between pages |
| 2420 | snapToDestination(); |
| 2421 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 2422 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 2423 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2424 | // Animate the drag view back to the original position |
| 2425 | void animateDragViewToOriginalPosition() { |
| 2426 | if (mDragView != null) { |
| 2427 | AnimatorSet anim = new AnimatorSet(); |
| 2428 | anim.setDuration(REORDERING_DROP_REPOSITION_DURATION); |
| 2429 | anim.playTogether( |
| 2430 | ObjectAnimator.ofFloat(mDragView, "translationX", 0f), |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2431 | ObjectAnimator.ofFloat(mDragView, "translationY", 0f), |
| 2432 | ObjectAnimator.ofFloat(mDragView, "scaleX", 1f), |
| 2433 | ObjectAnimator.ofFloat(mDragView, "scaleY", 1f)); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2434 | anim.addListener(new AnimatorListenerAdapter() { |
| 2435 | @Override |
| 2436 | public void onAnimationEnd(Animator animation) { |
| 2437 | onPostReorderingAnimationCompleted(); |
| 2438 | } |
| 2439 | }); |
| 2440 | anim.start(); |
| 2441 | } |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 2442 | } |
| 2443 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2444 | protected void onStartReordering() { |
| 2445 | // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.) |
| 2446 | mTouchState = TOUCH_STATE_REORDERING; |
| 2447 | mIsReordering = true; |
| 2448 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2449 | // We must invalidate to trigger a redraw to update the layers such that the drag view |
| 2450 | // is always drawn on top |
| 2451 | invalidate(); |
| 2452 | } |
| 2453 | |
| 2454 | private void onPostReorderingAnimationCompleted() { |
| 2455 | // Trigger the callback when reordering has settled |
| 2456 | --mPostReorderingPreZoomInRemainingAnimationCount; |
| 2457 | if (mPostReorderingPreZoomInRunnable != null && |
| 2458 | mPostReorderingPreZoomInRemainingAnimationCount == 0) { |
| 2459 | mPostReorderingPreZoomInRunnable.run(); |
| 2460 | mPostReorderingPreZoomInRunnable = null; |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | protected void onEndReordering() { |
| 2465 | mIsReordering = false; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2466 | } |
| 2467 | |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2468 | public boolean startReordering(View v) { |
Adam Cohen | 93c9756 | 2013-09-26 13:48:01 -0700 | [diff] [blame] | 2469 | int dragViewIndex = indexOfChild(v); |
| 2470 | |
| 2471 | if (mTouchState != TOUCH_STATE_REST) return false; |
| 2472 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2473 | mTempVisiblePagesRange[0] = 0; |
| 2474 | mTempVisiblePagesRange[1] = getPageCount() - 1; |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2475 | getOverviewModePages(mTempVisiblePagesRange); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2476 | mReorderingStarted = true; |
| 2477 | |
| 2478 | // Check if we are within the reordering range |
| 2479 | if (mTempVisiblePagesRange[0] <= dragViewIndex && |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2480 | dragViewIndex <= mTempVisiblePagesRange[1]) { |
| 2481 | // Find the drag view under the pointer |
| 2482 | mDragView = getChildAt(dragViewIndex); |
| 2483 | mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start(); |
| 2484 | mDragViewBaselineLeft = mDragView.getLeft(); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 2485 | snapToPage(getPageNearestToCenterOfScreen()); |
| 2486 | disableFreeScroll(); |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2487 | onStartReordering(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2488 | return true; |
| 2489 | } |
| 2490 | return false; |
| 2491 | } |
| 2492 | |
| 2493 | boolean isReordering(boolean testTouchState) { |
| 2494 | boolean state = mIsReordering; |
| 2495 | if (testTouchState) { |
| 2496 | state &= (mTouchState == TOUCH_STATE_REORDERING); |
| 2497 | } |
| 2498 | return state; |
| 2499 | } |
| 2500 | void endReordering() { |
| 2501 | // For simplicity, we call endReordering sometimes even if reordering was never started. |
| 2502 | // In that case, we don't want to do anything. |
| 2503 | if (!mReorderingStarted) return; |
| 2504 | mReorderingStarted = false; |
| 2505 | |
| 2506 | // If we haven't flung-to-delete the current child, then we just animate the drag view |
| 2507 | // back into position |
| 2508 | final Runnable onCompleteRunnable = new Runnable() { |
| 2509 | @Override |
| 2510 | public void run() { |
| 2511 | onEndReordering(); |
| 2512 | } |
| 2513 | }; |
| 2514 | if (!mDeferringForDelete) { |
| 2515 | mPostReorderingPreZoomInRunnable = new Runnable() { |
| 2516 | public void run() { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2517 | onCompleteRunnable.run(); |
| 2518 | enableFreeScroll(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2519 | }; |
| 2520 | }; |
| 2521 | |
| 2522 | mPostReorderingPreZoomInRemainingAnimationCount = |
| 2523 | NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT; |
| 2524 | // Snap to the current page |
| 2525 | snapToPage(indexOfChild(mDragView), 0); |
| 2526 | // Animate the drag view back to the front position |
| 2527 | animateDragViewToOriginalPosition(); |
| 2528 | } else { |
| 2529 | // Handled in post-delete-animation-callbacks |
| 2530 | } |
| 2531 | } |
| 2532 | |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2533 | /* |
| 2534 | * Flinging to delete - IN PROGRESS |
| 2535 | */ |
| 2536 | private PointF isFlingingToDelete() { |
| 2537 | ViewConfiguration config = ViewConfiguration.get(getContext()); |
| 2538 | mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity()); |
| 2539 | |
| 2540 | if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) { |
| 2541 | // Do a quick dot product test to ensure that we are flinging upwards |
| 2542 | PointF vel = new PointF(mVelocityTracker.getXVelocity(), |
| 2543 | mVelocityTracker.getYVelocity()); |
| 2544 | PointF upVec = new PointF(0f, -1f); |
| 2545 | float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) / |
| 2546 | (vel.length() * upVec.length())); |
| 2547 | if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) { |
| 2548 | return vel; |
| 2549 | } |
| 2550 | } |
| 2551 | return null; |
| 2552 | } |
| 2553 | |
| 2554 | /** |
| 2555 | * Creates an animation from the current drag view along its current velocity vector. |
| 2556 | * For this animation, the alpha runs for a fixed duration and we update the position |
| 2557 | * progressively. |
| 2558 | */ |
| 2559 | private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener { |
| 2560 | private View mDragView; |
| 2561 | private PointF mVelocity; |
| 2562 | private Rect mFrom; |
| 2563 | private long mPrevTime; |
| 2564 | private float mFriction; |
| 2565 | |
| 2566 | private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f); |
| 2567 | |
| 2568 | public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from, |
| 2569 | long startTime, float friction) { |
| 2570 | mDragView = dragView; |
| 2571 | mVelocity = vel; |
| 2572 | mFrom = from; |
| 2573 | mPrevTime = startTime; |
| 2574 | mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction); |
| 2575 | } |
| 2576 | |
| 2577 | @Override |
| 2578 | public void onAnimationUpdate(ValueAnimator animation) { |
| 2579 | float t = ((Float) animation.getAnimatedValue()).floatValue(); |
| 2580 | long curTime = AnimationUtils.currentAnimationTimeMillis(); |
| 2581 | |
| 2582 | mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f); |
| 2583 | mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f); |
| 2584 | |
| 2585 | mDragView.setTranslationX(mFrom.left); |
| 2586 | mDragView.setTranslationY(mFrom.top); |
| 2587 | mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t)); |
| 2588 | |
| 2589 | mVelocity.x *= mFriction; |
| 2590 | mVelocity.y *= mFriction; |
| 2591 | mPrevTime = curTime; |
| 2592 | } |
| 2593 | }; |
| 2594 | |
| 2595 | private static final int ANIM_TAG_KEY = 100; |
| 2596 | |
| 2597 | private Runnable createPostDeleteAnimationRunnable(final View dragView) { |
| 2598 | return new Runnable() { |
| 2599 | @Override |
| 2600 | public void run() { |
| 2601 | int dragViewIndex = indexOfChild(dragView); |
| 2602 | |
| 2603 | // For each of the pages around the drag view, animate them from the previous |
| 2604 | // position to the new position in the layout (as a result of the drag view moving |
| 2605 | // in the layout) |
| 2606 | // NOTE: We can make an assumption here because we have side-bound pages that we |
| 2607 | // will always have pages to animate in from the left |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2608 | getOverviewModePages(mTempVisiblePagesRange); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2609 | boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]); |
| 2610 | boolean slideFromLeft = (isLastWidgetPage || |
| 2611 | dragViewIndex > mTempVisiblePagesRange[0]); |
| 2612 | |
| 2613 | // Setup the scroll to the correct page before we swap the views |
| 2614 | if (slideFromLeft) { |
| 2615 | snapToPageImmediately(dragViewIndex - 1); |
| 2616 | } |
| 2617 | |
| 2618 | int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]); |
| 2619 | int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1); |
| 2620 | int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 ); |
| 2621 | int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex); |
| 2622 | ArrayList<Animator> animations = new ArrayList<Animator>(); |
| 2623 | for (int i = lowerIndex; i <= upperIndex; ++i) { |
| 2624 | View v = getChildAt(i); |
| 2625 | // dragViewIndex < pageUnderPointIndex, so after we remove the |
| 2626 | // drag view all subsequent views to pageUnderPointIndex will |
| 2627 | // shift down. |
| 2628 | int oldX = 0; |
| 2629 | int newX = 0; |
| 2630 | if (slideFromLeft) { |
| 2631 | if (i == 0) { |
| 2632 | // Simulate the page being offscreen with the page spacing |
| 2633 | oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i) |
Adam Cohen | 3b185e2 | 2013-10-29 14:45:58 -0700 | [diff] [blame] | 2634 | - mPageSpacing; |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2635 | } else { |
| 2636 | oldX = getViewportOffsetX() + getChildOffset(i - 1); |
| 2637 | } |
| 2638 | newX = getViewportOffsetX() + getChildOffset(i); |
| 2639 | } else { |
| 2640 | oldX = getChildOffset(i) - getChildOffset(i - 1); |
| 2641 | newX = 0; |
| 2642 | } |
| 2643 | |
| 2644 | // Animate the view translation from its old position to its new |
| 2645 | // position |
| 2646 | AnimatorSet anim = (AnimatorSet) v.getTag(); |
| 2647 | if (anim != null) { |
| 2648 | anim.cancel(); |
| 2649 | } |
| 2650 | |
| 2651 | // Note: Hacky, but we want to skip any optimizations to not draw completely |
| 2652 | // hidden views |
| 2653 | v.setAlpha(Math.max(v.getAlpha(), 0.01f)); |
| 2654 | v.setTranslationX(oldX - newX); |
| 2655 | anim = new AnimatorSet(); |
| 2656 | anim.playTogether( |
| 2657 | ObjectAnimator.ofFloat(v, "translationX", 0f), |
| 2658 | ObjectAnimator.ofFloat(v, "alpha", 1f)); |
| 2659 | animations.add(anim); |
| 2660 | v.setTag(ANIM_TAG_KEY, anim); |
| 2661 | } |
| 2662 | |
| 2663 | AnimatorSet slideAnimations = new AnimatorSet(); |
| 2664 | slideAnimations.playTogether(animations); |
| 2665 | slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION); |
| 2666 | slideAnimations.addListener(new AnimatorListenerAdapter() { |
| 2667 | @Override |
| 2668 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | f358a4b | 2013-07-23 16:47:31 -0700 | [diff] [blame] | 2669 | mDeferringForDelete = false; |
| 2670 | onEndReordering(); |
| 2671 | onRemoveViewAnimationCompleted(); |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 2672 | } |
| 2673 | }); |
| 2674 | slideAnimations.start(); |
| 2675 | |
| 2676 | removeView(dragView); |
| 2677 | onRemoveView(dragView, true); |
| 2678 | } |
| 2679 | }; |
| 2680 | } |
| 2681 | |
| 2682 | public void onFlingToDelete(PointF vel) { |
| 2683 | final long startTime = AnimationUtils.currentAnimationTimeMillis(); |
| 2684 | |
| 2685 | // NOTE: Because it takes time for the first frame of animation to actually be |
| 2686 | // called and we expect the animation to be a continuation of the fling, we have |
| 2687 | // to account for the time that has elapsed since the fling finished. And since |
| 2688 | // we don't have a startDelay, we will always get call to update when we call |
| 2689 | // start() (which we want to ignore). |
| 2690 | final TimeInterpolator tInterpolator = new TimeInterpolator() { |
| 2691 | private int mCount = -1; |
| 2692 | private long mStartTime; |
| 2693 | private float mOffset; |
| 2694 | /* Anonymous inner class ctor */ { |
| 2695 | mStartTime = startTime; |
| 2696 | } |
| 2697 | |
| 2698 | @Override |
| 2699 | public float getInterpolation(float t) { |
| 2700 | if (mCount < 0) { |
| 2701 | mCount++; |
| 2702 | } else if (mCount == 0) { |
| 2703 | mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() - |
| 2704 | mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION); |
| 2705 | mCount++; |
| 2706 | } |
| 2707 | return Math.min(1f, mOffset + t); |
| 2708 | } |
| 2709 | }; |
| 2710 | |
| 2711 | final Rect from = new Rect(); |
| 2712 | final View dragView = mDragView; |
| 2713 | from.left = (int) dragView.getTranslationX(); |
| 2714 | from.top = (int) dragView.getTranslationY(); |
| 2715 | AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel, |
| 2716 | from, startTime, FLING_TO_DELETE_FRICTION); |
| 2717 | |
| 2718 | final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView); |
| 2719 | |
| 2720 | // Create and start the animation |
| 2721 | ValueAnimator mDropAnim = new ValueAnimator(); |
| 2722 | mDropAnim.setInterpolator(tInterpolator); |
| 2723 | mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION); |
| 2724 | mDropAnim.setFloatValues(0f, 1f); |
| 2725 | mDropAnim.addUpdateListener(updateCb); |
| 2726 | mDropAnim.addListener(new AnimatorListenerAdapter() { |
| 2727 | public void onAnimationEnd(Animator animation) { |
| 2728 | onAnimationEndRunnable.run(); |
| 2729 | } |
| 2730 | }); |
| 2731 | mDropAnim.start(); |
| 2732 | mDeferringForDelete = true; |
| 2733 | } |
| 2734 | |
| 2735 | /* Drag to delete */ |
| 2736 | private boolean isHoveringOverDeleteDropTarget(int x, int y) { |
| 2737 | if (mDeleteDropTarget != null) { |
| 2738 | mAltTmpRect.set(0, 0, 0, 0); |
| 2739 | View parent = (View) mDeleteDropTarget.getParent(); |
| 2740 | if (parent != null) { |
| 2741 | parent.getGlobalVisibleRect(mAltTmpRect); |
| 2742 | } |
| 2743 | mDeleteDropTarget.getGlobalVisibleRect(mTmpRect); |
| 2744 | mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top); |
| 2745 | return mTmpRect.contains(x, y); |
| 2746 | } |
| 2747 | return false; |
| 2748 | } |
| 2749 | |
| 2750 | protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {} |
| 2751 | |
| 2752 | private void onDropToDelete() { |
| 2753 | final View dragView = mDragView; |
| 2754 | |
| 2755 | final float toScale = 0f; |
| 2756 | final float toAlpha = 0f; |
| 2757 | |
| 2758 | // Create and start the complex animation |
| 2759 | ArrayList<Animator> animations = new ArrayList<Animator>(); |
| 2760 | AnimatorSet motionAnim = new AnimatorSet(); |
| 2761 | motionAnim.setInterpolator(new DecelerateInterpolator(2)); |
| 2762 | motionAnim.playTogether( |
| 2763 | ObjectAnimator.ofFloat(dragView, "scaleX", toScale), |
| 2764 | ObjectAnimator.ofFloat(dragView, "scaleY", toScale)); |
| 2765 | animations.add(motionAnim); |
| 2766 | |
| 2767 | AnimatorSet alphaAnim = new AnimatorSet(); |
| 2768 | alphaAnim.setInterpolator(new LinearInterpolator()); |
| 2769 | alphaAnim.playTogether( |
| 2770 | ObjectAnimator.ofFloat(dragView, "alpha", toAlpha)); |
| 2771 | animations.add(alphaAnim); |
| 2772 | |
| 2773 | final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView); |
| 2774 | |
| 2775 | AnimatorSet anim = new AnimatorSet(); |
| 2776 | anim.playTogether(animations); |
| 2777 | anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION); |
| 2778 | anim.addListener(new AnimatorListenerAdapter() { |
| 2779 | public void onAnimationEnd(Animator animation) { |
| 2780 | onAnimationEndRunnable.run(); |
| 2781 | } |
| 2782 | }); |
| 2783 | anim.start(); |
| 2784 | |
| 2785 | mDeferringForDelete = true; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 2786 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2787 | |
| 2788 | /* Accessibility */ |
| 2789 | @Override |
| 2790 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 2791 | super.onInitializeAccessibilityNodeInfo(info); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 2792 | info.setScrollable(getPageCount() > 1); |
| 2793 | if (getCurrentPage() < getPageCount() - 1) { |
| 2794 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); |
| 2795 | } |
| 2796 | if (getCurrentPage() > 0) { |
| 2797 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); |
| 2798 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | @Override |
Alan Viverette | 254139a | 2013-10-08 13:13:46 -0700 | [diff] [blame] | 2802 | public void sendAccessibilityEvent(int eventType) { |
| 2803 | // Don't let the view send real scroll events. |
| 2804 | if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 2805 | super.sendAccessibilityEvent(eventType); |
| 2806 | } |
| 2807 | } |
| 2808 | |
| 2809 | @Override |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2810 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 2811 | super.onInitializeAccessibilityEvent(event); |
| 2812 | event.setScrollable(true); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 2813 | } |
| 2814 | |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 2815 | @Override |
| 2816 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 2817 | if (super.performAccessibilityAction(action, arguments)) { |
| 2818 | return true; |
| 2819 | } |
| 2820 | switch (action) { |
| 2821 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
| 2822 | if (getCurrentPage() < getPageCount() - 1) { |
| 2823 | scrollRight(); |
| 2824 | return true; |
| 2825 | } |
| 2826 | } break; |
| 2827 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
| 2828 | if (getCurrentPage() > 0) { |
| 2829 | scrollLeft(); |
| 2830 | return true; |
| 2831 | } |
| 2832 | } break; |
| 2833 | } |
| 2834 | return false; |
| 2835 | } |
| 2836 | |
Adam Cohen | 0ffac43 | 2013-07-10 11:19:26 -0700 | [diff] [blame] | 2837 | protected String getCurrentPageDescription() { |
| 2838 | return String.format(getContext().getString(R.string.default_scroll_format), |
| 2839 | getNextPage() + 1, getChildCount()); |
| 2840 | } |
| 2841 | |
Winson Chung | d11265e | 2011-08-30 13:37:23 -0700 | [diff] [blame] | 2842 | @Override |
| 2843 | public boolean onHoverEvent(android.view.MotionEvent event) { |
| 2844 | return true; |
| 2845 | } |
Adam Cohen | 5084cba | 2013-09-03 12:01:16 -0700 | [diff] [blame] | 2846 | } |