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