Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 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 | |
| 17 | package com.android.launcher2; |
| 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; |
| 21 | import android.animation.ObjectAnimator; |
Winson Chung | bb6f6a5 | 2011-07-25 17:55:44 -0700 | [diff] [blame] | 22 | import android.animation.ValueAnimator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 25 | import android.graphics.Canvas; |
| 26 | import android.graphics.Rect; |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 27 | import android.os.Bundle; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 28 | import android.os.Parcel; |
| 29 | import android.os.Parcelable; |
| 30 | import android.util.AttributeSet; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 31 | import android.util.Log; |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 32 | import android.view.InputDevice; |
| 33 | import android.view.KeyEvent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 34 | import android.view.MotionEvent; |
| 35 | import android.view.VelocityTracker; |
| 36 | import android.view.View; |
| 37 | import android.view.ViewConfiguration; |
| 38 | import android.view.ViewGroup; |
| 39 | import android.view.ViewParent; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 40 | import android.view.accessibility.AccessibilityEvent; |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 41 | import android.view.accessibility.AccessibilityManager; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 42 | import android.view.accessibility.AccessibilityNodeInfo; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 43 | import android.view.animation.Interpolator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 44 | import android.widget.Scroller; |
| 45 | |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 46 | import com.android.launcher.R; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 47 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 48 | import java.util.ArrayList; |
| 49 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 50 | /** |
| 51 | * An abstraction of the original Workspace which supports browsing through a |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 52 | * sequential list of "pages" |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 53 | */ |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 54 | public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 55 | private static final String TAG = "PagedView"; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 56 | private static final boolean DEBUG = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 57 | protected static final int INVALID_PAGE = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 58 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 59 | // 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] | 60 | private static final int MIN_LENGTH_FOR_FLING = 25; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 61 | |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 62 | protected static final int PAGE_SNAP_ANIMATION_DURATION = 550; |
| 63 | protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 64 | protected static final float NANOTIME_DIV = 1000000000.0f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 65 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 66 | private static final float OVERSCROLL_ACCELERATE_FACTOR = 2; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 67 | private static final float OVERSCROLL_DAMP_FACTOR = 0.14f; |
Winson Chung | 867ca62 | 2012-02-21 15:48:35 -0800 | [diff] [blame] | 68 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 69 | private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 70 | // The page is moved more than halfway, automatically move to the next page on touch up. |
| 71 | private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 72 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 73 | // The following constants need to be scaled based on density. The scaled versions will be |
| 74 | // assigned to the corresponding member variables below. |
| 75 | private static final int FLING_THRESHOLD_VELOCITY = 500; |
| 76 | private static final int MIN_SNAP_VELOCITY = 1500; |
| 77 | private static final int MIN_FLING_VELOCITY = 250; |
| 78 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 79 | static final int AUTOMATIC_PAGE_SPACING = -1; |
| 80 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 81 | protected int mFlingThresholdVelocity; |
| 82 | protected int mMinFlingVelocity; |
| 83 | protected int mMinSnapVelocity; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 84 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 85 | protected float mDensity; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 86 | protected float mSmoothingTime; |
| 87 | protected float mTouchX; |
| 88 | |
| 89 | protected boolean mFirstLayout = true; |
| 90 | |
| 91 | protected int mCurrentPage; |
| 92 | protected int mNextPage = INVALID_PAGE; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 93 | protected int mMaxScrollX; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 94 | protected Scroller mScroller; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 95 | private VelocityTracker mVelocityTracker; |
| 96 | |
| 97 | private float mDownMotionX; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 98 | protected float mLastMotionX; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 99 | protected float mLastMotionXRemainder; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 100 | protected float mLastMotionY; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 101 | protected float mTotalMotionX; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 102 | private int mLastScreenCenter = -1; |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 103 | private int[] mChildOffsets; |
| 104 | private int[] mChildRelativeOffsets; |
| 105 | private int[] mChildOffsetsWithLayoutScale; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 106 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 107 | protected final static int TOUCH_STATE_REST = 0; |
| 108 | protected final static int TOUCH_STATE_SCROLLING = 1; |
| 109 | protected final static int TOUCH_STATE_PREV_PAGE = 2; |
| 110 | protected final static int TOUCH_STATE_NEXT_PAGE = 3; |
Adam Cohen | e45440e | 2010-10-14 18:33:38 -0700 | [diff] [blame] | 111 | protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 112 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 113 | protected int mTouchState = TOUCH_STATE_REST; |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 114 | protected boolean mForceScreenScrolled = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 115 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 116 | protected OnLongClickListener mLongClickListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 117 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 118 | protected boolean mAllowLongPress = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 119 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 120 | protected int mTouchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 121 | private int mPagingTouchSlop; |
| 122 | private int mMaximumVelocity; |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 123 | private int mMinimumWidth; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 124 | protected int mPageSpacing; |
| 125 | protected int mPageLayoutPaddingTop; |
| 126 | protected int mPageLayoutPaddingBottom; |
| 127 | protected int mPageLayoutPaddingLeft; |
| 128 | protected int mPageLayoutPaddingRight; |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 129 | protected int mPageLayoutWidthGap; |
| 130 | protected int mPageLayoutHeightGap; |
Michael Jurka | 87b1490 | 2011-05-25 22:13:09 -0700 | [diff] [blame] | 131 | protected int mCellCountX = 0; |
| 132 | protected int mCellCountY = 0; |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 133 | protected boolean mCenterPagesVertically; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 134 | protected boolean mAllowOverScroll = true; |
| 135 | protected int mUnboundedScrollX; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 136 | protected int[] mTempVisiblePagesRange = new int[2]; |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 137 | protected boolean mForceDrawAllChildrenNextFrame; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 138 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 139 | // 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] | 140 | // it is equal to the scaled overscroll position. We use a separate value so as to prevent |
| 141 | // the screens from continuing to translate beyond the normal bounds. |
| 142 | protected int mOverScrollX; |
| 143 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 144 | // parameter that adjusts the layout to be optimized for pages with that scale factor |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 145 | protected float mLayoutScale = 1.0f; |
| 146 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 147 | protected static final int INVALID_POINTER = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 148 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 149 | protected int mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 150 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 151 | private PageSwitchListener mPageSwitchListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 152 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 153 | protected ArrayList<Boolean> mDirtyPageContent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 154 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 155 | // If true, syncPages and syncPageItems will be called to refresh pages |
| 156 | protected boolean mContentIsRefreshable = true; |
| 157 | |
| 158 | // If true, modify alpha of neighboring pages as user scrolls left/right |
| 159 | protected boolean mFadeInAdjacentScreens = true; |
| 160 | |
| 161 | // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding |
| 162 | // to switch to a new page |
| 163 | protected boolean mUsePagingTouchSlop = true; |
| 164 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 165 | // 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] | 166 | // (SmoothPagedView does this) |
| 167 | protected boolean mDeferScrollUpdate = false; |
| 168 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 169 | protected boolean mIsPageMoving = false; |
| 170 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 171 | // All syncs and layout passes are deferred until data is ready. |
| 172 | protected boolean mIsDataReady = false; |
| 173 | |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 174 | // Scrolling indicator |
Winson Chung | bb6f6a5 | 2011-07-25 17:55:44 -0700 | [diff] [blame] | 175 | private ValueAnimator mScrollIndicatorAnimator; |
Michael Jurka | afaa050 | 2011-12-13 18:22:50 -0800 | [diff] [blame] | 176 | private View mScrollIndicator; |
Winson Chung | f5f8cef | 2011-07-22 11:16:13 -0700 | [diff] [blame] | 177 | private int mScrollIndicatorPaddingLeft; |
| 178 | private int mScrollIndicatorPaddingRight; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 179 | private boolean mHasScrollIndicator = true; |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 180 | private boolean mShouldShowScrollIndicator = false; |
| 181 | private boolean mShouldShowScrollIndicatorImmediately = false; |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 182 | protected static final int sScrollIndicatorFadeInDuration = 150; |
| 183 | protected static final int sScrollIndicatorFadeOutDuration = 650; |
| 184 | protected static final int sScrollIndicatorFlashDuration = 650; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 185 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 186 | // If set, will defer loading associated pages until the scrolling settles |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 187 | private boolean mDeferLoadAssociatedPagesUntilScrollCompletes; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 188 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 189 | public interface PageSwitchListener { |
| 190 | void onPageSwitch(View newPage, int newPageIndex); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 193 | public PagedView(Context context) { |
| 194 | this(context, null); |
| 195 | } |
| 196 | |
| 197 | public PagedView(Context context, AttributeSet attrs) { |
| 198 | this(context, attrs, 0); |
| 199 | } |
| 200 | |
| 201 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 202 | super(context, attrs, defStyle); |
| 203 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 204 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 205 | R.styleable.PagedView, defStyle, 0); |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 206 | setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0)); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 207 | mPageLayoutPaddingTop = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 208 | R.styleable.PagedView_pageLayoutPaddingTop, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 209 | mPageLayoutPaddingBottom = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 210 | R.styleable.PagedView_pageLayoutPaddingBottom, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 211 | mPageLayoutPaddingLeft = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 212 | R.styleable.PagedView_pageLayoutPaddingLeft, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 213 | mPageLayoutPaddingRight = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 214 | R.styleable.PagedView_pageLayoutPaddingRight, 0); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 215 | mPageLayoutWidthGap = a.getDimensionPixelSize( |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 216 | R.styleable.PagedView_pageLayoutWidthGap, 0); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 217 | mPageLayoutHeightGap = a.getDimensionPixelSize( |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 218 | R.styleable.PagedView_pageLayoutHeightGap, 0); |
Winson Chung | f5f8cef | 2011-07-22 11:16:13 -0700 | [diff] [blame] | 219 | mScrollIndicatorPaddingLeft = |
| 220 | a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0); |
| 221 | mScrollIndicatorPaddingRight = |
| 222 | a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 223 | a.recycle(); |
| 224 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 225 | setHapticFeedbackEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 226 | init(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Initializes various states for this workspace. |
| 231 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 232 | protected void init() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 233 | mDirtyPageContent = new ArrayList<Boolean>(); |
| 234 | mDirtyPageContent.ensureCapacity(32); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 235 | mScroller = new Scroller(getContext(), new ScrollInterpolator()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 236 | mCurrentPage = 0; |
Winson Chung | bfeac06 | 2012-06-06 15:56:08 -0700 | [diff] [blame^] | 237 | if (this instanceof AppsCustomizePagedView) Log.d(TAG, "6549598 init() mCurrentPage: " + mCurrentPage); |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 238 | mCenterPagesVertically = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 239 | |
| 240 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
| 241 | mTouchSlop = configuration.getScaledTouchSlop(); |
| 242 | mPagingTouchSlop = configuration.getScaledPagingTouchSlop(); |
| 243 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 244 | mDensity = getResources().getDisplayMetrics().density; |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 245 | |
| 246 | mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity); |
| 247 | mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity); |
| 248 | mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 249 | setOnHierarchyChangeListener(this); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 252 | public void setPageSwitchListener(PageSwitchListener pageSwitchListener) { |
| 253 | mPageSwitchListener = pageSwitchListener; |
| 254 | if (mPageSwitchListener != null) { |
| 255 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 260 | * Called by subclasses to mark that data is ready, and that we can begin loading and laying |
| 261 | * out pages. |
| 262 | */ |
| 263 | protected void setDataIsReady() { |
| 264 | mIsDataReady = true; |
| 265 | } |
| 266 | protected boolean isDataReady() { |
| 267 | return mIsDataReady; |
| 268 | } |
| 269 | |
| 270 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 271 | * Returns the index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 272 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 273 | * @return The index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 274 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 275 | int getCurrentPage() { |
| 276 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 277 | } |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 278 | int getNextPage() { |
| 279 | return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 280 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 281 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 282 | int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 283 | return getChildCount(); |
| 284 | } |
| 285 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 286 | View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 287 | return getChildAt(index); |
| 288 | } |
| 289 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 290 | protected int indexToPage(int index) { |
| 291 | return index; |
| 292 | } |
| 293 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 294 | /** |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 295 | * Updates the scroll of the current page immediately to its final scroll position. We use this |
| 296 | * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of |
| 297 | * the previous tab page. |
| 298 | */ |
| 299 | protected void updateCurrentPageScroll() { |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 300 | int offset = getChildOffset(mCurrentPage); |
| 301 | int relOffset = getRelativeChildOffset(mCurrentPage); |
| 302 | int newX = offset - relOffset; |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 303 | scrollTo(newX, 0); |
| 304 | mScroller.setFinalX(newX); |
Michael Jurka | dd6c091 | 2012-01-16 06:46:20 -0800 | [diff] [blame] | 305 | mScroller.forceFinished(true); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 309 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 310 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 311 | void setCurrentPage(int currentPage) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 312 | if (!mScroller.isFinished()) { |
| 313 | mScroller.abortAnimation(); |
| 314 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 315 | // don't introduce any checks like mCurrentPage == currentPage here-- if we change the |
| 316 | // the default |
| 317 | if (getChildCount() == 0) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 318 | return; |
| 319 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 320 | |
Winson Chung | bfeac06 | 2012-06-06 15:56:08 -0700 | [diff] [blame^] | 321 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 322 | mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1)); |
Winson Chung | bfeac06 | 2012-06-06 15:56:08 -0700 | [diff] [blame^] | 323 | if (this instanceof AppsCustomizePagedView) Log.d(TAG, "6549598 setCurrentPage mCurrentPage: " + mCurrentPage); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 324 | updateCurrentPageScroll(); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 325 | updateScrollingIndicator(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 326 | notifyPageSwitchListener(); |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 327 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 330 | protected void notifyPageSwitchListener() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 331 | if (mPageSwitchListener != null) { |
| 332 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 336 | protected void pageBeginMoving() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 337 | if (!mIsPageMoving) { |
| 338 | mIsPageMoving = true; |
| 339 | onPageBeginMoving(); |
| 340 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 343 | protected void pageEndMoving() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 344 | if (mIsPageMoving) { |
| 345 | mIsPageMoving = false; |
| 346 | onPageEndMoving(); |
| 347 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 350 | protected boolean isPageMoving() { |
| 351 | return mIsPageMoving; |
| 352 | } |
| 353 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 354 | // a method that subclasses can override to add behavior |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 355 | protected void onPageBeginMoving() { |
| 356 | } |
| 357 | |
| 358 | // a method that subclasses can override to add behavior |
| 359 | protected void onPageEndMoving() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 362 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 363 | * Registers the specified listener on each page contained in this workspace. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 364 | * |
| 365 | * @param l The listener used to respond to long clicks. |
| 366 | */ |
| 367 | @Override |
| 368 | public void setOnLongClickListener(OnLongClickListener l) { |
| 369 | mLongClickListener = l; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 370 | final int count = getPageCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 371 | for (int i = 0; i < count; i++) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 372 | getPageAt(i).setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | @Override |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 377 | public void scrollBy(int x, int y) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 378 | scrollTo(mUnboundedScrollX + x, getScrollY() + y); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 382 | public void scrollTo(int x, int y) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 383 | mUnboundedScrollX = x; |
| 384 | |
| 385 | if (x < 0) { |
| 386 | super.scrollTo(0, y); |
| 387 | if (mAllowOverScroll) { |
| 388 | overScroll(x); |
| 389 | } |
| 390 | } else if (x > mMaxScrollX) { |
| 391 | super.scrollTo(mMaxScrollX, y); |
| 392 | if (mAllowOverScroll) { |
| 393 | overScroll(x - mMaxScrollX); |
| 394 | } |
| 395 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 396 | mOverScrollX = x; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 397 | super.scrollTo(x, y); |
| 398 | } |
| 399 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 400 | mTouchX = x; |
| 401 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 402 | } |
| 403 | |
| 404 | // we moved this functionality to a helper function so SmoothPagedView can reuse it |
| 405 | protected boolean computeScrollHelper() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 406 | if (mScroller.computeScrollOffset()) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 407 | // 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] | 408 | if (getScrollX() != mScroller.getCurrX() |
| 409 | || getScrollY() != mScroller.getCurrY() |
Michael Jurka | b06d95f | 2012-04-02 06:26:53 -0700 | [diff] [blame] | 410 | || mOverScrollX != mScroller.getCurrX()) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 411 | scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); |
| 412 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 413 | invalidate(); |
| 414 | return true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 415 | } else if (mNextPage != INVALID_PAGE) { |
| 416 | mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 417 | mNextPage = INVALID_PAGE; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 418 | notifyPageSwitchListener(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 419 | |
| 420 | // Load the associated pages if necessary |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 421 | if (mDeferLoadAssociatedPagesUntilScrollCompletes) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 422 | loadAssociatedPages(mCurrentPage); |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 423 | mDeferLoadAssociatedPagesUntilScrollCompletes = false; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 426 | // We don't want to trigger a page end moving unless the page has settled |
| 427 | // and the user has stopped scrolling |
| 428 | if (mTouchState == TOUCH_STATE_REST) { |
| 429 | pageEndMoving(); |
| 430 | } |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 431 | |
| 432 | // Notify the user when the page changes |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 433 | AccessibilityManager accessibilityManager = (AccessibilityManager) |
| 434 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 435 | if (accessibilityManager.isEnabled()) { |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 436 | AccessibilityEvent ev = |
| 437 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED); |
| 438 | ev.getText().add(getCurrentPageDescription()); |
| 439 | sendAccessibilityEventUnchecked(ev); |
| 440 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 441 | return true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 442 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 443 | return false; |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | public void computeScroll() { |
| 448 | computeScrollHelper(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | @Override |
| 452 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 453 | if (!mIsDataReady) { |
| 454 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 455 | return; |
| 456 | } |
| 457 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 458 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 459 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 460 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 461 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 462 | if (widthMode != MeasureSpec.EXACTLY) { |
| 463 | throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); |
| 464 | } |
| 465 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 466 | // Return early if we aren't given a proper dimension |
| 467 | if (widthSize <= 0 || heightSize <= 0) { |
| 468 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 469 | return; |
| 470 | } |
| 471 | |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 472 | /* Allow the height to be set as WRAP_CONTENT. This allows the particular case |
| 473 | * of the All apps view on XLarge displays to not take up more space then it needs. Width |
| 474 | * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect |
| 475 | * each page to have the same width. |
| 476 | */ |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 477 | int maxChildHeight = 0; |
| 478 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 479 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
| 480 | final int horizontalPadding = getPaddingLeft() + getPaddingRight(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 481 | |
Michael Jurka | 36fcb74 | 2011-04-06 17:08:58 -0700 | [diff] [blame] | 482 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 483 | // The children are given the same width and height as the workspace |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 484 | // unless they were set to WRAP_CONTENT |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 485 | if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 486 | final int childCount = getChildCount(); |
| 487 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 488 | // disallowing padding in paged view (just pass 0) |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 489 | final View child = getPageAt(i); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 490 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 491 | |
| 492 | int childWidthMode; |
| 493 | if (lp.width == LayoutParams.WRAP_CONTENT) { |
| 494 | childWidthMode = MeasureSpec.AT_MOST; |
| 495 | } else { |
| 496 | childWidthMode = MeasureSpec.EXACTLY; |
| 497 | } |
| 498 | |
| 499 | int childHeightMode; |
| 500 | if (lp.height == LayoutParams.WRAP_CONTENT) { |
| 501 | childHeightMode = MeasureSpec.AT_MOST; |
| 502 | } else { |
| 503 | childHeightMode = MeasureSpec.EXACTLY; |
| 504 | } |
| 505 | |
| 506 | final int childWidthMeasureSpec = |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame] | 507 | MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 508 | final int childHeightMeasureSpec = |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 509 | MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 510 | |
| 511 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 512 | maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 513 | if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", " |
| 514 | + child.getMeasuredHeight()); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | if (heightMode == MeasureSpec.AT_MOST) { |
| 518 | heightSize = maxChildHeight + verticalPadding; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 519 | } |
Winson Chung | ae890b8 | 2011-09-13 18:08:54 -0700 | [diff] [blame] | 520 | |
Winson Chung | ae890b8 | 2011-09-13 18:08:54 -0700 | [diff] [blame] | 521 | setMeasuredDimension(widthSize, heightSize); |
| 522 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 523 | // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions. |
| 524 | // We also wait until we set the measured dimensions before flushing the cache as well, to |
| 525 | // ensure that the cache is filled with good values. |
| 526 | invalidateCachedOffsets(); |
| 527 | |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 528 | if (childCount > 0) { |
| 529 | if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", " |
| 530 | + getChildWidth(0)); |
| 531 | |
| 532 | // Calculate the variable page spacing if necessary |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 533 | if (mPageSpacing == AUTOMATIC_PAGE_SPACING) { |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 534 | // The gap between pages in the PagedView should be equal to the gap from the page |
| 535 | // to the edge of the screen (so it is not visible in the current screen). To |
| 536 | // account for unequal padding on each side of the paged view, we take the maximum |
| 537 | // of the left/right gap and use that as the gap between each page. |
| 538 | int offset = getRelativeChildOffset(0); |
| 539 | int spacing = Math.max(offset, widthSize - offset - |
| 540 | getChildAt(0).getMeasuredWidth()); |
| 541 | setPageSpacing(spacing); |
| 542 | } |
| 543 | } |
| 544 | |
Adam Cohen | 25b2995 | 2011-11-02 14:49:06 -0700 | [diff] [blame] | 545 | updateScrollingIndicatorPosition(); |
| 546 | |
Adam Cohen | faa2830 | 2010-11-19 12:02:18 -0800 | [diff] [blame] | 547 | if (childCount > 0) { |
| 548 | mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1); |
| 549 | } else { |
| 550 | mMaxScrollX = 0; |
| 551 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 554 | protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) { |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 555 | int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 556 | int delta = newX - getScrollX(); |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 557 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 558 | final int pageCount = getChildCount(); |
| 559 | for (int i = 0; i < pageCount; i++) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 560 | View page = (View) getPageAt(i); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 561 | page.setX(page.getX() + delta); |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 562 | } |
| 563 | setCurrentPage(newCurrentPage); |
| 564 | } |
| 565 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 566 | // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a |
| 567 | // scale of 1.0f. A layout scale of 0.8f assumes the pages have a scale of 0.8f, and |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 568 | // tightens the layout accordingly |
| 569 | public void setLayoutScale(float childrenScale) { |
| 570 | mLayoutScale = childrenScale; |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 571 | invalidateCachedOffsets(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 572 | |
| 573 | // Now we need to do a re-layout, but preserving absolute X and Y coordinates |
| 574 | int childCount = getChildCount(); |
| 575 | float childrenX[] = new float[childCount]; |
| 576 | float childrenY[] = new float[childCount]; |
| 577 | for (int i = 0; i < childCount; i++) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 578 | final View child = getPageAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 579 | childrenX[i] = child.getX(); |
| 580 | childrenY[i] = child.getY(); |
| 581 | } |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 582 | // Trigger a full re-layout (never just call onLayout directly!) |
| 583 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY); |
| 584 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY); |
| 585 | requestLayout(); |
| 586 | measure(widthSpec, heightSpec); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 587 | layout(getLeft(), getTop(), getRight(), getBottom()); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 588 | for (int i = 0; i < childCount; i++) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 589 | final View child = getPageAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 590 | child.setX(childrenX[i]); |
| 591 | child.setY(childrenY[i]); |
| 592 | } |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 593 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 594 | // Also, the page offset has changed (since the pages are now smaller); |
| 595 | // update the page offset, but again preserving absolute X and Y coordinates |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 596 | scrollToNewPageWithoutMovingPages(mCurrentPage); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 599 | public void setPageSpacing(int pageSpacing) { |
| 600 | mPageSpacing = pageSpacing; |
| 601 | invalidateCachedOffsets(); |
| 602 | } |
| 603 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 604 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 605 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 606 | if (!mIsDataReady) { |
| 607 | return; |
| 608 | } |
| 609 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 610 | if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 611 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 612 | final int childCount = getChildCount(); |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 613 | int childLeft = getRelativeChildOffset(0); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 614 | |
| 615 | for (int i = 0; i < childCount; i++) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 616 | final View child = getPageAt(i); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 617 | if (child.getVisibility() != View.GONE) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 618 | final int childWidth = getScaledMeasuredWidth(child); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 619 | final int childHeight = child.getMeasuredHeight(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 620 | int childTop = getPaddingTop(); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 621 | if (mCenterPagesVertically) { |
| 622 | childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2; |
| 623 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 624 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 625 | if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 626 | child.layout(childLeft, childTop, |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 627 | childLeft + child.getMeasuredWidth(), childTop + childHeight); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 628 | childLeft += childWidth + mPageSpacing; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 629 | } |
| 630 | } |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 631 | |
| 632 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
| 633 | setHorizontalScrollBarEnabled(false); |
Michael Jurka | dd6c091 | 2012-01-16 06:46:20 -0800 | [diff] [blame] | 634 | updateCurrentPageScroll(); |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 635 | setHorizontalScrollBarEnabled(true); |
| 636 | mFirstLayout = false; |
| 637 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 640 | protected void screenScrolled(int screenCenter) { |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 641 | if (isScrollingIndicatorEnabled()) { |
| 642 | updateScrollingIndicator(); |
| 643 | } |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 644 | boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX; |
| 645 | |
| 646 | if (mFadeInAdjacentScreens && !isInOverscroll) { |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 647 | for (int i = 0; i < getChildCount(); i++) { |
| 648 | View child = getChildAt(i); |
| 649 | if (child != null) { |
| 650 | float scrollProgress = getScrollProgress(screenCenter, child, i); |
| 651 | float alpha = 1 - Math.abs(scrollProgress); |
Michael Jurka | 7372c59 | 2012-01-16 04:21:35 -0800 | [diff] [blame] | 652 | child.setAlpha(alpha); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | invalidate(); |
| 656 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 659 | @Override |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 660 | public void onChildViewAdded(View parent, View child) { |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 661 | // This ensures that when children are added, they get the correct transforms / alphas |
| 662 | // in accordance with any scroll effects. |
| 663 | mForceScreenScrolled = true; |
| 664 | invalidate(); |
Adam Cohen | 25b2995 | 2011-11-02 14:49:06 -0700 | [diff] [blame] | 665 | invalidateCachedOffsets(); |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 668 | @Override |
| 669 | public void onChildViewRemoved(View parent, View child) { |
| 670 | } |
| 671 | |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 672 | protected void invalidateCachedOffsets() { |
| 673 | int count = getChildCount(); |
Adam Cohen | 25b2995 | 2011-11-02 14:49:06 -0700 | [diff] [blame] | 674 | if (count == 0) { |
| 675 | mChildOffsets = null; |
| 676 | mChildRelativeOffsets = null; |
| 677 | mChildOffsetsWithLayoutScale = null; |
| 678 | return; |
| 679 | } |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 680 | |
| 681 | mChildOffsets = new int[count]; |
| 682 | mChildRelativeOffsets = new int[count]; |
| 683 | mChildOffsetsWithLayoutScale = new int[count]; |
| 684 | for (int i = 0; i < count; i++) { |
| 685 | mChildOffsets[i] = -1; |
| 686 | mChildRelativeOffsets[i] = -1; |
| 687 | mChildOffsetsWithLayoutScale[i] = -1; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | protected int getChildOffset(int index) { |
| 692 | int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ? |
| 693 | mChildOffsets : mChildOffsetsWithLayoutScale; |
| 694 | |
| 695 | if (childOffsets != null && childOffsets[index] != -1) { |
| 696 | return childOffsets[index]; |
| 697 | } else { |
| 698 | if (getChildCount() == 0) |
| 699 | return 0; |
| 700 | |
| 701 | int offset = getRelativeChildOffset(0); |
| 702 | for (int i = 0; i < index; ++i) { |
| 703 | offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing; |
| 704 | } |
| 705 | if (childOffsets != null) { |
| 706 | childOffsets[index] = offset; |
| 707 | } |
| 708 | return offset; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | protected int getRelativeChildOffset(int index) { |
| 713 | if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) { |
| 714 | return mChildRelativeOffsets[index]; |
| 715 | } else { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 716 | final int padding = getPaddingLeft() + getPaddingRight(); |
| 717 | final int offset = getPaddingLeft() + |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 718 | (getMeasuredWidth() - padding - getChildWidth(index)) / 2; |
| 719 | if (mChildRelativeOffsets != null) { |
| 720 | mChildRelativeOffsets[index] = offset; |
| 721 | } |
| 722 | return offset; |
| 723 | } |
| 724 | } |
| 725 | |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 726 | protected int getScaledMeasuredWidth(View child) { |
| 727 | // This functions are called enough times that it actually makes a difference in the |
| 728 | // profiler -- so just inline the max() here |
| 729 | final int measuredWidth = child.getMeasuredWidth(); |
| 730 | final int minWidth = mMinimumWidth; |
| 731 | final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth; |
| 732 | return (int) (maxWidth * mLayoutScale + 0.5f); |
| 733 | } |
| 734 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 735 | protected void getVisiblePages(int[] range) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 736 | final int pageCount = getChildCount(); |
Michael Jurka | 4ff7d79 | 2012-04-02 03:46:50 -0700 | [diff] [blame] | 737 | |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 738 | if (pageCount > 0) { |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 739 | final int screenWidth = getMeasuredWidth(); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 740 | int leftScreen = 0; |
| 741 | int rightScreen = 0; |
Michael Jurka | 47f7474 | 2012-03-02 13:39:13 -0800 | [diff] [blame] | 742 | View currPage = getPageAt(leftScreen); |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 743 | while (leftScreen < pageCount - 1 && |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 744 | currPage.getX() + currPage.getWidth() - |
| 745 | currPage.getPaddingRight() < getScrollX()) { |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 746 | leftScreen++; |
Michael Jurka | 47f7474 | 2012-03-02 13:39:13 -0800 | [diff] [blame] | 747 | currPage = getPageAt(leftScreen); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 748 | } |
| 749 | rightScreen = leftScreen; |
Michael Jurka | 47f7474 | 2012-03-02 13:39:13 -0800 | [diff] [blame] | 750 | currPage = getPageAt(rightScreen + 1); |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 751 | while (rightScreen < pageCount - 1 && |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 752 | currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) { |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 753 | rightScreen++; |
Michael Jurka | 47f7474 | 2012-03-02 13:39:13 -0800 | [diff] [blame] | 754 | currPage = getPageAt(rightScreen + 1); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 755 | } |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 756 | range[0] = leftScreen; |
| 757 | range[1] = rightScreen; |
| 758 | } else { |
| 759 | range[0] = -1; |
| 760 | range[1] = -1; |
| 761 | } |
| 762 | } |
| 763 | |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 764 | protected boolean shouldDrawChild(View child) { |
| 765 | return child.getAlpha() > 0; |
| 766 | } |
| 767 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 768 | @Override |
| 769 | protected void dispatchDraw(Canvas canvas) { |
| 770 | int halfScreenSize = getMeasuredWidth() / 2; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 771 | // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. |
| 772 | // Otherwise it is equal to the scaled overscroll position. |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 773 | int screenCenter = mOverScrollX + halfScreenSize; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 774 | |
| 775 | if (screenCenter != mLastScreenCenter || mForceScreenScrolled) { |
Michael Jurka | b06d95f | 2012-04-02 06:26:53 -0700 | [diff] [blame] | 776 | // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can |
| 777 | // set it for the next frame |
| 778 | mForceScreenScrolled = false; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 779 | screenScrolled(screenCenter); |
| 780 | mLastScreenCenter = screenCenter; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | // Find out which screens are visible; as an optimization we only call draw on them |
| 784 | final int pageCount = getChildCount(); |
| 785 | if (pageCount > 0) { |
| 786 | getVisiblePages(mTempVisiblePagesRange); |
| 787 | final int leftScreen = mTempVisiblePagesRange[0]; |
| 788 | final int rightScreen = mTempVisiblePagesRange[1]; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 789 | if (leftScreen != -1 && rightScreen != -1) { |
| 790 | final long drawingTime = getDrawingTime(); |
| 791 | // Clip to the bounds |
| 792 | canvas.save(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 793 | canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(), |
| 794 | getScrollY() + getBottom() - getTop()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 795 | |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 796 | // On certain graphics drivers, if you draw to a off-screen buffer that's not |
| 797 | // used, it can lead to poor performance. We were running into this when |
| 798 | // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw |
| 799 | // of that CellLayout's hardware layer, even if that CellLayout wasn't visible. |
| 800 | // As a fix, below we set pages that aren't going to be rendered are to be |
| 801 | // View.INVISIBLE, preventing re-drawing of their hardware layer |
| 802 | for (int i = getChildCount() - 1; i >= 0; i--) { |
| 803 | final View v = getPageAt(i); |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 804 | if (mForceDrawAllChildrenNextFrame || |
| 805 | (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) { |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 806 | v.setVisibility(VISIBLE); |
| 807 | drawChild(canvas, v, drawingTime); |
| 808 | } else { |
| 809 | v.setVisibility(INVISIBLE); |
| 810 | } |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 811 | } |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 812 | mForceDrawAllChildrenNextFrame = false; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 813 | canvas.restore(); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 814 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 815 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | @Override |
| 819 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 820 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 821 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 822 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 823 | return true; |
| 824 | } |
| 825 | return false; |
| 826 | } |
| 827 | |
| 828 | @Override |
| 829 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 830 | int focusablePage; |
| 831 | if (mNextPage != INVALID_PAGE) { |
| 832 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 833 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 834 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 835 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 836 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 837 | if (v != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 838 | return v.requestFocus(direction, previouslyFocusedRect); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 839 | } |
| 840 | return false; |
| 841 | } |
| 842 | |
| 843 | @Override |
| 844 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 845 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 846 | if (getCurrentPage() > 0) { |
| 847 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 848 | return true; |
| 849 | } |
| 850 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 851 | if (getCurrentPage() < getPageCount() - 1) { |
| 852 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 853 | return true; |
| 854 | } |
| 855 | } |
| 856 | return super.dispatchUnhandledMove(focused, direction); |
| 857 | } |
| 858 | |
| 859 | @Override |
| 860 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 861 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 862 | getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 863 | } |
| 864 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 865 | if (mCurrentPage > 0) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 866 | getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 867 | } |
| 868 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 869 | if (mCurrentPage < getPageCount() - 1) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 870 | getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * 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] | 877 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 878 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 879 | * This happens when live folders requery, and if they're off page, they |
| 880 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 881 | */ |
| 882 | @Override |
| 883 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 884 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 885 | View v = focused; |
| 886 | while (true) { |
| 887 | if (v == current) { |
| 888 | super.focusableViewAvailable(focused); |
| 889 | return; |
| 890 | } |
| 891 | if (v == this) { |
| 892 | return; |
| 893 | } |
| 894 | ViewParent parent = v.getParent(); |
| 895 | if (parent instanceof View) { |
| 896 | v = (View)v.getParent(); |
| 897 | } else { |
| 898 | return; |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | /** |
| 904 | * {@inheritDoc} |
| 905 | */ |
| 906 | @Override |
| 907 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 908 | if (disallowIntercept) { |
| 909 | // We need to make sure to cancel our long press if |
| 910 | // a scrollable widget takes over touch events |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 911 | final View currentPage = getPageAt(mCurrentPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 912 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 913 | } |
| 914 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 915 | } |
| 916 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 917 | /** |
| 918 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 919 | */ |
| 920 | protected boolean hitsPreviousPage(float x, float y) { |
| 921 | return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing); |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 926 | */ |
| 927 | protected boolean hitsNextPage(float x, float y) { |
| 928 | return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing)); |
| 929 | } |
| 930 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 931 | @Override |
| 932 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 933 | /* |
| 934 | * This method JUST determines whether we want to intercept the motion. |
| 935 | * If we return true, onTouchEvent will be called and we do the actual |
| 936 | * scrolling there. |
| 937 | */ |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 938 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 939 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 940 | // Skip touch handling if there are no pages to swipe |
| 941 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 942 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 943 | /* |
| 944 | * Shortcut the most recurring case: the user is in the dragging |
| 945 | * state and he is moving his finger. We want to intercept this |
| 946 | * motion. |
| 947 | */ |
| 948 | final int action = ev.getAction(); |
| 949 | if ((action == MotionEvent.ACTION_MOVE) && |
| 950 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 951 | return true; |
| 952 | } |
| 953 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 954 | switch (action & MotionEvent.ACTION_MASK) { |
| 955 | case MotionEvent.ACTION_MOVE: { |
| 956 | /* |
| 957 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 958 | * whether the user has moved far enough from his original down touch. |
| 959 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 960 | if (mActivePointerId != INVALID_POINTER) { |
| 961 | determineScrollingStart(ev); |
| 962 | break; |
| 963 | } |
| 964 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 965 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 966 | // i.e. fall through to the next case (don't break) |
| 967 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 968 | // while it's small- this was causing a crash before we checked for INVALID_POINTER) |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | case MotionEvent.ACTION_DOWN: { |
| 972 | final float x = ev.getX(); |
| 973 | final float y = ev.getY(); |
| 974 | // Remember location of down touch |
| 975 | mDownMotionX = x; |
| 976 | mLastMotionX = x; |
| 977 | mLastMotionY = y; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 978 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 979 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 980 | mActivePointerId = ev.getPointerId(0); |
| 981 | mAllowLongPress = true; |
| 982 | |
| 983 | /* |
| 984 | * If being flinged and user touches the screen, initiate drag; |
| 985 | * otherwise don't. mScroller.isFinished should be false when |
| 986 | * being flinged. |
| 987 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame] | 988 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 989 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop); |
| 990 | if (finishedScrolling) { |
| 991 | mTouchState = TOUCH_STATE_REST; |
| 992 | mScroller.abortAnimation(); |
| 993 | } else { |
| 994 | mTouchState = TOUCH_STATE_SCROLLING; |
| 995 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 996 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 997 | // 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] | 998 | // to scroll the current page |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 999 | if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1000 | if (getChildCount() > 0) { |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1001 | if (hitsPreviousPage(x, y)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1002 | mTouchState = TOUCH_STATE_PREV_PAGE; |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1003 | } else if (hitsNextPage(x, y)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1004 | mTouchState = TOUCH_STATE_NEXT_PAGE; |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | break; |
| 1009 | } |
| 1010 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1011 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1012 | case MotionEvent.ACTION_CANCEL: |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1013 | mTouchState = TOUCH_STATE_REST; |
| 1014 | mAllowLongPress = false; |
| 1015 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1016 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1017 | break; |
| 1018 | |
| 1019 | case MotionEvent.ACTION_POINTER_UP: |
| 1020 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1021 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1022 | break; |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | * The only time we want to intercept motion events is if we are in the |
| 1027 | * drag mode. |
| 1028 | */ |
| 1029 | return mTouchState != TOUCH_STATE_REST; |
| 1030 | } |
| 1031 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1032 | protected void determineScrollingStart(MotionEvent ev) { |
| 1033 | determineScrollingStart(ev, 1.0f); |
| 1034 | } |
| 1035 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1036 | /* |
| 1037 | * Determines if we should change the touch state to start scrolling after the |
| 1038 | * user moves their touch point too far. |
| 1039 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1040 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1041 | /* |
| 1042 | * Locally do absolute value. mLastMotionX is set to the y value |
| 1043 | * of the down event. |
| 1044 | */ |
| 1045 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Michael Jurka | 2698db4 | 2011-07-13 18:25:14 -0700 | [diff] [blame] | 1046 | if (pointerIndex == -1) { |
| 1047 | return; |
| 1048 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1049 | final float x = ev.getX(pointerIndex); |
| 1050 | final float y = ev.getY(pointerIndex); |
| 1051 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 1052 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 1053 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1054 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1055 | boolean xPaged = xDiff > mPagingTouchSlop; |
| 1056 | boolean xMoved = xDiff > touchSlop; |
| 1057 | boolean yMoved = yDiff > touchSlop; |
| 1058 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1059 | if (xMoved || xPaged || yMoved) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1060 | if (mUsePagingTouchSlop ? xPaged : xMoved) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1061 | // Scroll if the user moved far enough along the X axis |
| 1062 | mTouchState = TOUCH_STATE_SCROLLING; |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1063 | mTotalMotionX += Math.abs(mLastMotionX - x); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1064 | mLastMotionX = x; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1065 | mLastMotionXRemainder = 0; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1066 | mTouchX = getScrollX(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1067 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1068 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1069 | } |
| 1070 | // Either way, cancel any pending longpress |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1071 | cancelCurrentPageLongPress(); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | protected void cancelCurrentPageLongPress() { |
| 1076 | if (mAllowLongPress) { |
| 1077 | mAllowLongPress = false; |
| 1078 | // Try canceling the long press. It could also have been scheduled |
| 1079 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 1080 | // everything |
| 1081 | final View currentPage = getPageAt(mCurrentPage); |
| 1082 | if (currentPage != null) { |
| 1083 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1088 | protected float getScrollProgress(int screenCenter, View v, int page) { |
| 1089 | final int halfScreenSize = getMeasuredWidth() / 2; |
| 1090 | |
| 1091 | int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing; |
| 1092 | int delta = screenCenter - (getChildOffset(page) - |
| 1093 | getRelativeChildOffset(page) + halfScreenSize); |
| 1094 | |
| 1095 | float scrollProgress = delta / (totalDistance * 1.0f); |
| 1096 | scrollProgress = Math.min(scrollProgress, 1.0f); |
| 1097 | scrollProgress = Math.max(scrollProgress, -1.0f); |
| 1098 | return scrollProgress; |
| 1099 | } |
| 1100 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1101 | // This curve determines how the effect of scrolling over the limits of the page dimishes |
| 1102 | // as the user pulls further and further from the bounds |
| 1103 | private float overScrollInfluenceCurve(float f) { |
| 1104 | f -= 1.0f; |
| 1105 | return f * f * f + 1.0f; |
| 1106 | } |
| 1107 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1108 | protected void acceleratedOverScroll(float amount) { |
| 1109 | int screenSize = getMeasuredWidth(); |
| 1110 | |
| 1111 | // We want to reach the max over scroll effect when the user has |
| 1112 | // over scrolled half the size of the screen |
| 1113 | float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize); |
| 1114 | |
| 1115 | if (f == 0) return; |
| 1116 | |
| 1117 | // Clamp this factor, f, to -1 < f < 1 |
| 1118 | if (Math.abs(f) >= 1) { |
| 1119 | f /= Math.abs(f); |
| 1120 | } |
| 1121 | |
| 1122 | int overScrollAmount = (int) Math.round(f * screenSize); |
| 1123 | if (amount < 0) { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1124 | mOverScrollX = overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1125 | super.scrollTo(0, getScrollY()); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1126 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1127 | mOverScrollX = mMaxScrollX + overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1128 | super.scrollTo(mMaxScrollX, getScrollY()); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1129 | } |
| 1130 | invalidate(); |
| 1131 | } |
| 1132 | |
| 1133 | protected void dampedOverScroll(float amount) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1134 | int screenSize = getMeasuredWidth(); |
| 1135 | |
| 1136 | float f = (amount / screenSize); |
| 1137 | |
| 1138 | if (f == 0) return; |
| 1139 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 1140 | |
Adam Cohen | 7bfc979 | 2011-01-28 13:52:37 -0800 | [diff] [blame] | 1141 | // Clamp this factor, f, to -1 < f < 1 |
| 1142 | if (Math.abs(f) >= 1) { |
| 1143 | f /= Math.abs(f); |
| 1144 | } |
| 1145 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1146 | int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1147 | if (amount < 0) { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1148 | mOverScrollX = overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1149 | super.scrollTo(0, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1150 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1151 | mOverScrollX = mMaxScrollX + overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1152 | super.scrollTo(mMaxScrollX, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1153 | } |
| 1154 | invalidate(); |
| 1155 | } |
| 1156 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1157 | protected void overScroll(float amount) { |
| 1158 | dampedOverScroll(amount); |
| 1159 | } |
| 1160 | |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1161 | protected float maxOverScroll() { |
| 1162 | // 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] | 1163 | // 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] | 1164 | float f = 1.0f; |
| 1165 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 1166 | return OVERSCROLL_DAMP_FACTOR * f; |
| 1167 | } |
| 1168 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1169 | @Override |
| 1170 | public boolean onTouchEvent(MotionEvent ev) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1171 | // Skip touch handling if there are no pages to swipe |
| 1172 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 1173 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1174 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1175 | |
| 1176 | final int action = ev.getAction(); |
| 1177 | |
| 1178 | switch (action & MotionEvent.ACTION_MASK) { |
| 1179 | case MotionEvent.ACTION_DOWN: |
| 1180 | /* |
| 1181 | * If being flinged and user touches, stop the fling. isFinished |
| 1182 | * will be false if being flinged. |
| 1183 | */ |
| 1184 | if (!mScroller.isFinished()) { |
| 1185 | mScroller.abortAnimation(); |
| 1186 | } |
| 1187 | |
| 1188 | // Remember where the motion event started |
| 1189 | mDownMotionX = mLastMotionX = ev.getX(); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1190 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1191 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1192 | mActivePointerId = ev.getPointerId(0); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1193 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1194 | pageBeginMoving(); |
| 1195 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1196 | break; |
| 1197 | |
| 1198 | case MotionEvent.ACTION_MOVE: |
| 1199 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1200 | // Scroll to follow the motion event |
| 1201 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 1202 | final float x = ev.getX(pointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1203 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1204 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1205 | mTotalMotionX += Math.abs(deltaX); |
| 1206 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1207 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1208 | // keep the remainder because we are actually testing if we've moved from the last |
| 1209 | // scrolled position (which is discrete). |
| 1210 | if (Math.abs(deltaX) >= 1.0f) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1211 | mTouchX += deltaX; |
| 1212 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1213 | if (!mDeferScrollUpdate) { |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1214 | scrollBy((int) deltaX, 0); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1215 | if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1216 | } else { |
| 1217 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1218 | } |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1219 | mLastMotionX = x; |
| 1220 | mLastMotionXRemainder = deltaX - (int) deltaX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1221 | } else { |
| 1222 | awakenScrollBars(); |
| 1223 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1224 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1225 | determineScrollingStart(ev); |
| 1226 | } |
| 1227 | break; |
| 1228 | |
| 1229 | case MotionEvent.ACTION_UP: |
| 1230 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1231 | final int activePointerId = mActivePointerId; |
| 1232 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 1233 | final float x = ev.getX(pointerIndex); |
| 1234 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1235 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1236 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 1237 | final int deltaX = (int) (x - mDownMotionX); |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1238 | final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage)); |
| 1239 | boolean isSignificantMove = Math.abs(deltaX) > pageWidth * |
| 1240 | SIGNIFICANT_MOVE_THRESHOLD; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1241 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1242 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1243 | |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1244 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1245 | Math.abs(velocityX) > mFlingThresholdVelocity; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1246 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1247 | // In the case that the page is moved far to one direction and then is flung |
| 1248 | // in the opposite direction, we use a threshold to determine whether we should |
| 1249 | // just return to the starting page, or if we should skip one further. |
| 1250 | boolean returnToOriginalPage = false; |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1251 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1252 | Math.signum(velocityX) != Math.signum(deltaX) && isFling) { |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1253 | returnToOriginalPage = true; |
| 1254 | } |
| 1255 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1256 | int finalPage; |
| 1257 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1258 | // test for a large move if a fling has been registered. That is, a large |
| 1259 | // move to the left and fling to the right will register as a fling to the right. |
| 1260 | if (((isSignificantMove && deltaX > 0 && !isFling) || |
| 1261 | (isFling && velocityX > 0)) && mCurrentPage > 0) { |
| 1262 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1263 | snapToPageWithVelocity(finalPage, velocityX); |
| 1264 | } else if (((isSignificantMove && deltaX < 0 && !isFling) || |
| 1265 | (isFling && velocityX < 0)) && |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1266 | mCurrentPage < getChildCount() - 1) { |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1267 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1268 | snapToPageWithVelocity(finalPage, velocityX); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1269 | } else { |
| 1270 | snapToDestination(); |
| 1271 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1272 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1273 | // at this point we have not moved beyond the touch slop |
| 1274 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1275 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1276 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1277 | if (nextPage != mCurrentPage) { |
| 1278 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1279 | } else { |
| 1280 | snapToDestination(); |
| 1281 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1282 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1283 | // at this point we have not moved beyond the touch slop |
| 1284 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1285 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1286 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1287 | if (nextPage != mCurrentPage) { |
| 1288 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1289 | } else { |
| 1290 | snapToDestination(); |
| 1291 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1292 | } else { |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 1293 | onUnhandledTap(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1294 | } |
| 1295 | mTouchState = TOUCH_STATE_REST; |
| 1296 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1297 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1298 | break; |
| 1299 | |
| 1300 | case MotionEvent.ACTION_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1301 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1302 | snapToDestination(); |
| 1303 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1304 | mTouchState = TOUCH_STATE_REST; |
| 1305 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1306 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1307 | break; |
| 1308 | |
| 1309 | case MotionEvent.ACTION_POINTER_UP: |
| 1310 | onSecondaryPointerUp(ev); |
| 1311 | break; |
| 1312 | } |
| 1313 | |
| 1314 | return true; |
| 1315 | } |
| 1316 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1317 | @Override |
| 1318 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1319 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1320 | switch (event.getAction()) { |
| 1321 | case MotionEvent.ACTION_SCROLL: { |
| 1322 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1323 | final float vscroll; |
| 1324 | final float hscroll; |
| 1325 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1326 | vscroll = 0; |
| 1327 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1328 | } else { |
| 1329 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1330 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1331 | } |
| 1332 | if (hscroll != 0 || vscroll != 0) { |
| 1333 | if (hscroll > 0 || vscroll > 0) { |
| 1334 | scrollRight(); |
| 1335 | } else { |
| 1336 | scrollLeft(); |
| 1337 | } |
| 1338 | return true; |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | return super.onGenericMotionEvent(event); |
| 1344 | } |
| 1345 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1346 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1347 | if (mVelocityTracker == null) { |
| 1348 | mVelocityTracker = VelocityTracker.obtain(); |
| 1349 | } |
| 1350 | mVelocityTracker.addMovement(ev); |
| 1351 | } |
| 1352 | |
| 1353 | private void releaseVelocityTracker() { |
| 1354 | if (mVelocityTracker != null) { |
| 1355 | mVelocityTracker.recycle(); |
| 1356 | mVelocityTracker = null; |
| 1357 | } |
| 1358 | } |
| 1359 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1360 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 1361 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 1362 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 1363 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1364 | if (pointerId == mActivePointerId) { |
| 1365 | // This was our active pointer going up. Choose a new |
| 1366 | // active pointer and adjust accordingly. |
| 1367 | // TODO: Make this decision more intelligent. |
| 1368 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 1369 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 1370 | mLastMotionY = ev.getY(newPointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1371 | mLastMotionXRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1372 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1373 | if (mVelocityTracker != null) { |
| 1374 | mVelocityTracker.clear(); |
| 1375 | } |
| 1376 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 1379 | protected void onUnhandledTap(MotionEvent ev) {} |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1380 | |
| 1381 | @Override |
| 1382 | public void requestChildFocus(View child, View focused) { |
| 1383 | super.requestChildFocus(child, focused); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1384 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1385 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1386 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1390 | protected int getChildIndexForRelativeOffset(int relativeOffset) { |
| 1391 | final int childCount = getChildCount(); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 1392 | int left; |
| 1393 | int right; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1394 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 1395 | left = getRelativeChildOffset(i); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1396 | right = (left + getScaledMeasuredWidth(getPageAt(i))); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1397 | if (left <= relativeOffset && relativeOffset <= right) { |
| 1398 | return i; |
| 1399 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1400 | } |
| 1401 | return -1; |
| 1402 | } |
| 1403 | |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1404 | protected int getChildWidth(int index) { |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1405 | // This functions are called enough times that it actually makes a difference in the |
| 1406 | // profiler -- so just inline the max() here |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1407 | final int measuredWidth = getPageAt(index).getMeasuredWidth(); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1408 | final int minWidth = mMinimumWidth; |
| 1409 | return (minWidth > measuredWidth) ? minWidth : measuredWidth; |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1410 | } |
| 1411 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1412 | int getPageNearestToCenterOfScreen() { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1413 | int minDistanceFromScreenCenter = Integer.MAX_VALUE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1414 | int minDistanceFromScreenCenterIndex = -1; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1415 | int screenCenter = getScrollX() + (getMeasuredWidth() / 2); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1416 | final int childCount = getChildCount(); |
| 1417 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1418 | View layout = (View) getPageAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1419 | int childWidth = getScaledMeasuredWidth(layout); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1420 | int halfChildWidth = (childWidth / 2); |
| 1421 | int childCenter = getChildOffset(i) + halfChildWidth; |
| 1422 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 1423 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1424 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1425 | minDistanceFromScreenCenterIndex = i; |
| 1426 | } |
| 1427 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1428 | return minDistanceFromScreenCenterIndex; |
| 1429 | } |
| 1430 | |
| 1431 | protected void snapToDestination() { |
| 1432 | snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1435 | private static class ScrollInterpolator implements Interpolator { |
| 1436 | public ScrollInterpolator() { |
| 1437 | } |
| 1438 | |
| 1439 | public float getInterpolation(float t) { |
| 1440 | t -= 1.0f; |
| 1441 | return t*t*t*t*t + 1; |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1446 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1447 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1448 | // of travel has on the overall snap duration. |
| 1449 | float distanceInfluenceForSnapDuration(float f) { |
| 1450 | f -= 0.5f; // center the values about 0. |
| 1451 | f *= 0.3f * Math.PI / 2.0f; |
| 1452 | return (float) Math.sin(f); |
| 1453 | } |
| 1454 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1455 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1456 | whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1)); |
| 1457 | int halfScreenSize = getMeasuredWidth() / 2; |
| 1458 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1459 | if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage)); |
| 1460 | if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): " |
| 1461 | + getMeasuredWidth() + ", " + getChildWidth(whichPage)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1462 | final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
| 1463 | int delta = newX - mUnboundedScrollX; |
| 1464 | int duration = 0; |
| 1465 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1466 | if (Math.abs(velocity) < mMinFlingVelocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1467 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 1468 | // as opposed to an apparent physical response to flinging |
| 1469 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
| 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | // Here we compute a "distance" that will be used in the computation of the overall |
| 1474 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 1475 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 1476 | // duration as a function of the distance the page needs to travel. |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 1477 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1478 | float distance = halfScreenSize + halfScreenSize * |
| 1479 | distanceInfluenceForSnapDuration(distanceRatio); |
| 1480 | |
| 1481 | velocity = Math.abs(velocity); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1482 | velocity = Math.max(mMinSnapVelocity, velocity); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1483 | |
| 1484 | // we want the page's snap velocity to approximately match the velocity at which the |
| 1485 | // 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] | 1486 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 1487 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1488 | |
| 1489 | snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | protected void snapToPage(int whichPage) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1493 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1496 | protected void snapToPage(int whichPage, int duration) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1497 | whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1498 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1499 | if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage)); |
| 1500 | if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", " |
| 1501 | + getChildWidth(whichPage)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1502 | int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1503 | final int sX = mUnboundedScrollX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1504 | final int delta = newX - sX; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1505 | snapToPage(whichPage, delta, duration); |
| 1506 | } |
| 1507 | |
| 1508 | protected void snapToPage(int whichPage, int delta, int duration) { |
| 1509 | mNextPage = whichPage; |
| 1510 | |
| 1511 | View focusedChild = getFocusedChild(); |
| 1512 | if (focusedChild != null && whichPage != mCurrentPage && |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1513 | focusedChild == getPageAt(mCurrentPage)) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1514 | focusedChild.clearFocus(); |
| 1515 | } |
| 1516 | |
| 1517 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1518 | awakenScrollBars(duration); |
| 1519 | if (duration == 0) { |
| 1520 | duration = Math.abs(delta); |
| 1521 | } |
| 1522 | |
| 1523 | if (!mScroller.isFinished()) mScroller.abortAnimation(); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1524 | mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1525 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1526 | // Load associated pages immediately if someone else is handling the scroll, otherwise defer |
| 1527 | // loading associated pages until the scroll settles |
| 1528 | if (mDeferScrollUpdate) { |
| 1529 | loadAssociatedPages(mNextPage); |
| 1530 | } else { |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 1531 | mDeferLoadAssociatedPagesUntilScrollCompletes = true; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1532 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1533 | notifyPageSwitchListener(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1534 | invalidate(); |
| 1535 | } |
| 1536 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1537 | public void scrollLeft() { |
| 1538 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1539 | if (mCurrentPage > 0) snapToPage(mCurrentPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1540 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1541 | if (mNextPage > 0) snapToPage(mNextPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | public void scrollRight() { |
| 1546 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1547 | if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1548 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1549 | if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1550 | } |
| 1551 | } |
| 1552 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1553 | public int getPageForView(View v) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1554 | int result = -1; |
| 1555 | if (v != null) { |
| 1556 | ViewParent vp = v.getParent(); |
| 1557 | int count = getChildCount(); |
| 1558 | for (int i = 0; i < count; i++) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1559 | if (vp == getPageAt(i)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1560 | return i; |
| 1561 | } |
| 1562 | } |
| 1563 | } |
| 1564 | return result; |
| 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * @return True is long presses are still allowed for the current touch |
| 1569 | */ |
| 1570 | public boolean allowLongPress() { |
| 1571 | return mAllowLongPress; |
| 1572 | } |
| 1573 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1574 | /** |
| 1575 | * Set true to allow long-press events to be triggered, usually checked by |
| 1576 | * {@link Launcher} to accept or block dpad-initiated long-presses. |
| 1577 | */ |
| 1578 | public void setAllowLongPress(boolean allowLongPress) { |
| 1579 | mAllowLongPress = allowLongPress; |
| 1580 | } |
| 1581 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1582 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1583 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1584 | |
| 1585 | SavedState(Parcelable superState) { |
| 1586 | super(superState); |
| 1587 | } |
| 1588 | |
| 1589 | private SavedState(Parcel in) { |
| 1590 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1591 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | @Override |
| 1595 | public void writeToParcel(Parcel out, int flags) { |
| 1596 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1597 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 1601 | new Parcelable.Creator<SavedState>() { |
| 1602 | public SavedState createFromParcel(Parcel in) { |
| 1603 | return new SavedState(in); |
| 1604 | } |
| 1605 | |
| 1606 | public SavedState[] newArray(int size) { |
| 1607 | return new SavedState[size]; |
| 1608 | } |
| 1609 | }; |
| 1610 | } |
| 1611 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1612 | protected void loadAssociatedPages(int page) { |
| 1613 | loadAssociatedPages(page, false); |
| 1614 | } |
| 1615 | protected void loadAssociatedPages(int page, boolean immediateAndOnly) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1616 | if (mContentIsRefreshable) { |
| 1617 | final int count = getChildCount(); |
| 1618 | if (page < count) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1619 | int lowerPageBound = getAssociatedLowerPageBound(page); |
| 1620 | int upperPageBound = getAssociatedUpperPageBound(page); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1621 | if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/" |
| 1622 | + upperPageBound); |
Michael Jurka | 0cad111 | 2011-11-16 20:43:29 -0800 | [diff] [blame] | 1623 | // First, clear any pages that should no longer be loaded |
| 1624 | for (int i = 0; i < count; ++i) { |
| 1625 | Page layout = (Page) getPageAt(i); |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 1626 | if ((i < lowerPageBound) || (i > upperPageBound)) { |
Michael Jurka | 0cad111 | 2011-11-16 20:43:29 -0800 | [diff] [blame] | 1627 | if (layout.getPageChildCount() > 0) { |
| 1628 | layout.removeAllViewsOnPage(); |
| 1629 | } |
| 1630 | mDirtyPageContent.set(i, true); |
| 1631 | } |
| 1632 | } |
| 1633 | // Next, load any new pages |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1634 | for (int i = 0; i < count; ++i) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1635 | if ((i != page) && immediateAndOnly) { |
| 1636 | continue; |
| 1637 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1638 | if (lowerPageBound <= i && i <= upperPageBound) { |
| 1639 | if (mDirtyPageContent.get(i)) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1640 | syncPageItems(i, (i == page) && immediateAndOnly); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1641 | mDirtyPageContent.set(i, false); |
| 1642 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1643 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1644 | } |
| 1645 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1646 | } |
| 1647 | } |
| 1648 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1649 | protected int getAssociatedLowerPageBound(int page) { |
| 1650 | return Math.max(0, page - 1); |
| 1651 | } |
| 1652 | protected int getAssociatedUpperPageBound(int page) { |
| 1653 | final int count = getChildCount(); |
| 1654 | return Math.min(page + 1, count - 1); |
| 1655 | } |
| 1656 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1657 | /** |
| 1658 | * This method is called ONLY to synchronize the number of pages that the paged view has. |
| 1659 | * To actually fill the pages with information, implement syncPageItems() below. It is |
| 1660 | * guaranteed that syncPageItems() will be called for a particular page before it is shown, |
| 1661 | * and therefore, individual page items do not need to be updated in this method. |
| 1662 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1663 | public abstract void syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1664 | |
| 1665 | /** |
| 1666 | * This method is called to synchronize the items that are on a particular page. If views on |
| 1667 | * the page can be reused, then they should be updated within this method. |
| 1668 | */ |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1669 | public abstract void syncPageItems(int page, boolean immediate); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1670 | |
Patrick Dubroy | 244d74c | 2011-05-19 16:48:48 -0700 | [diff] [blame] | 1671 | protected void invalidatePageData() { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1672 | invalidatePageData(-1, false); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1673 | } |
| 1674 | protected void invalidatePageData(int currentPage) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1675 | invalidatePageData(currentPage, false); |
| 1676 | } |
| 1677 | protected void invalidatePageData(int currentPage, boolean immediateAndOnly) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1678 | if (!mIsDataReady) { |
Winson Chung | 7c7a22d | 2012-06-01 13:46:48 -0700 | [diff] [blame] | 1679 | if (this instanceof AppsCustomizePagedView) Log.d(TAG, "6549598 invalidatePageData page: " + currentPage + " not data ready"); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1680 | return; |
| 1681 | } |
| 1682 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1683 | if (mContentIsRefreshable) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1684 | // Force all scrolling-related behavior to end |
| 1685 | mScroller.forceFinished(true); |
| 1686 | mNextPage = INVALID_PAGE; |
| 1687 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1688 | // Update all the pages |
| 1689 | syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1690 | |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1691 | // We must force a measure after we've loaded the pages to update the content width and |
| 1692 | // to determine the full scroll width |
| 1693 | measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), |
| 1694 | MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)); |
| 1695 | |
| 1696 | // Set a new page as the current page if necessary |
| 1697 | if (currentPage > -1) { |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 1698 | setCurrentPage(Math.min(getPageCount() - 1, currentPage)); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1699 | } |
| 1700 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1701 | // Mark each of the pages as dirty |
| 1702 | final int count = getChildCount(); |
| 1703 | mDirtyPageContent.clear(); |
| 1704 | for (int i = 0; i < count; ++i) { |
| 1705 | mDirtyPageContent.add(true); |
| 1706 | } |
| 1707 | |
| 1708 | // Load any pages that are necessary for the current window of views |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1709 | loadAssociatedPages(mCurrentPage, immediateAndOnly); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1710 | requestLayout(); |
Winson Chung | 7c7a22d | 2012-06-01 13:46:48 -0700 | [diff] [blame] | 1711 | |
| 1712 | if (this instanceof AppsCustomizePagedView) Log.d(TAG, "6549598 invalidatePageData page: " + currentPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1713 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1714 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1715 | |
Michael Jurka | afaa050 | 2011-12-13 18:22:50 -0800 | [diff] [blame] | 1716 | protected View getScrollingIndicator() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1717 | // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator |
| 1718 | // found |
| 1719 | if (mHasScrollIndicator && mScrollIndicator == null) { |
| 1720 | ViewGroup parent = (ViewGroup) getParent(); |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 1721 | if (parent != null) { |
| 1722 | mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator)); |
| 1723 | mHasScrollIndicator = mScrollIndicator != null; |
| 1724 | if (mHasScrollIndicator) { |
| 1725 | mScrollIndicator.setVisibility(View.VISIBLE); |
| 1726 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1727 | } |
| 1728 | } |
| 1729 | return mScrollIndicator; |
| 1730 | } |
| 1731 | |
| 1732 | protected boolean isScrollingIndicatorEnabled() { |
Winson Chung | 649723c | 2011-07-06 20:41:23 -0700 | [diff] [blame] | 1733 | return !LauncherApplication.isScreenLarge(); |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1734 | } |
| 1735 | |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1736 | Runnable hideScrollingIndicatorRunnable = new Runnable() { |
| 1737 | @Override |
| 1738 | public void run() { |
| 1739 | hideScrollingIndicator(false); |
| 1740 | } |
| 1741 | }; |
Michael Jurka | b737ee6 | 2011-11-15 15:57:22 -0800 | [diff] [blame] | 1742 | protected void flashScrollingIndicator(boolean animated) { |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1743 | removeCallbacks(hideScrollingIndicatorRunnable); |
Michael Jurka | b737ee6 | 2011-11-15 15:57:22 -0800 | [diff] [blame] | 1744 | showScrollingIndicator(!animated); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1745 | postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration); |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 1748 | protected void showScrollingIndicator(boolean immediately) { |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 1749 | mShouldShowScrollIndicator = true; |
| 1750 | mShouldShowScrollIndicatorImmediately = true; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1751 | if (getChildCount() <= 1) return; |
| 1752 | if (!isScrollingIndicatorEnabled()) return; |
| 1753 | |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 1754 | mShouldShowScrollIndicator = false; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1755 | getScrollingIndicator(); |
| 1756 | if (mScrollIndicator != null) { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1757 | // Fade the indicator in |
| 1758 | updateScrollingIndicatorPosition(); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1759 | mScrollIndicator.setVisibility(View.VISIBLE); |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 1760 | cancelScrollingIndicatorAnimations(); |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 1761 | if (immediately) { |
| 1762 | mScrollIndicator.setAlpha(1f); |
| 1763 | } else { |
| 1764 | mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f); |
| 1765 | mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration); |
| 1766 | mScrollIndicatorAnimator.start(); |
| 1767 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1768 | } |
| 1769 | } |
| 1770 | |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 1771 | protected void cancelScrollingIndicatorAnimations() { |
| 1772 | if (mScrollIndicatorAnimator != null) { |
| 1773 | mScrollIndicatorAnimator.cancel(); |
| 1774 | } |
| 1775 | } |
| 1776 | |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1777 | protected void hideScrollingIndicator(boolean immediately) { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1778 | if (getChildCount() <= 1) return; |
| 1779 | if (!isScrollingIndicatorEnabled()) return; |
| 1780 | |
| 1781 | getScrollingIndicator(); |
| 1782 | if (mScrollIndicator != null) { |
| 1783 | // Fade the indicator out |
| 1784 | updateScrollingIndicatorPosition(); |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 1785 | cancelScrollingIndicatorAnimations(); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1786 | if (immediately) { |
Michael Jurka | 81efbad | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1787 | mScrollIndicator.setVisibility(View.INVISIBLE); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1788 | mScrollIndicator.setAlpha(0f); |
| 1789 | } else { |
| 1790 | mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f); |
| 1791 | mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration); |
| 1792 | mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() { |
| 1793 | private boolean cancelled = false; |
| 1794 | @Override |
| 1795 | public void onAnimationCancel(android.animation.Animator animation) { |
| 1796 | cancelled = true; |
| 1797 | } |
| 1798 | @Override |
| 1799 | public void onAnimationEnd(Animator animation) { |
| 1800 | if (!cancelled) { |
Michael Jurka | 81efbad | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1801 | mScrollIndicator.setVisibility(View.INVISIBLE); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1802 | } |
| 1803 | } |
| 1804 | }); |
| 1805 | mScrollIndicatorAnimator.start(); |
| 1806 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1807 | } |
| 1808 | } |
| 1809 | |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1810 | /** |
| 1811 | * To be overridden by subclasses to determine whether the scroll indicator should stretch to |
| 1812 | * fill its space on the track or not. |
| 1813 | */ |
| 1814 | protected boolean hasElasticScrollIndicator() { |
Winson Chung | dea74b7 | 2011-09-13 18:06:43 -0700 | [diff] [blame] | 1815 | return true; |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1818 | private void updateScrollingIndicator() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1819 | if (getChildCount() <= 1) return; |
| 1820 | if (!isScrollingIndicatorEnabled()) return; |
| 1821 | |
| 1822 | getScrollingIndicator(); |
| 1823 | if (mScrollIndicator != null) { |
| 1824 | updateScrollingIndicatorPosition(); |
| 1825 | } |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 1826 | if (mShouldShowScrollIndicator) { |
| 1827 | showScrollingIndicator(mShouldShowScrollIndicatorImmediately); |
| 1828 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1829 | } |
| 1830 | |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1831 | private void updateScrollingIndicatorPosition() { |
Winson Chung | 649723c | 2011-07-06 20:41:23 -0700 | [diff] [blame] | 1832 | if (!isScrollingIndicatorEnabled()) return; |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 1833 | if (mScrollIndicator == null) return; |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1834 | int numPages = getChildCount(); |
| 1835 | int pageWidth = getMeasuredWidth(); |
Winson Chung | ae890b8 | 2011-09-13 18:08:54 -0700 | [diff] [blame] | 1836 | int lastChildIndex = Math.max(0, getChildCount() - 1); |
| 1837 | int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex); |
Winson Chung | f5f8cef | 2011-07-22 11:16:13 -0700 | [diff] [blame] | 1838 | int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight; |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1839 | int indicatorWidth = mScrollIndicator.getMeasuredWidth() - |
| 1840 | mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight(); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1841 | |
Winson Chung | ae890b8 | 2011-09-13 18:08:54 -0700 | [diff] [blame] | 1842 | float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX)); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1843 | int indicatorSpace = trackWidth / numPages; |
Winson Chung | ae890b8 | 2011-09-13 18:08:54 -0700 | [diff] [blame] | 1844 | int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft; |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1845 | if (hasElasticScrollIndicator()) { |
| 1846 | if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) { |
| 1847 | mScrollIndicator.getLayoutParams().width = indicatorSpace; |
| 1848 | mScrollIndicator.requestLayout(); |
| 1849 | } |
| 1850 | } else { |
| 1851 | int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2; |
| 1852 | indicatorPos += indicatorCenterOffset; |
| 1853 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1854 | mScrollIndicator.setTranslationX(indicatorPos); |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1857 | public void showScrollIndicatorTrack() { |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | public void hideScrollIndicatorTrack() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1861 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1862 | |
| 1863 | /* Accessibility */ |
| 1864 | @Override |
| 1865 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 1866 | super.onInitializeAccessibilityNodeInfo(info); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1867 | info.setScrollable(getPageCount() > 1); |
| 1868 | if (getCurrentPage() < getPageCount() - 1) { |
| 1869 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); |
| 1870 | } |
| 1871 | if (getCurrentPage() > 0) { |
| 1872 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); |
| 1873 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | @Override |
| 1877 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 1878 | super.onInitializeAccessibilityEvent(event); |
| 1879 | event.setScrollable(true); |
| 1880 | if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 1881 | event.setFromIndex(mCurrentPage); |
| 1882 | event.setToIndex(mCurrentPage); |
| 1883 | event.setItemCount(getChildCount()); |
| 1884 | } |
| 1885 | } |
| 1886 | |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1887 | @Override |
| 1888 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 1889 | if (super.performAccessibilityAction(action, arguments)) { |
| 1890 | return true; |
| 1891 | } |
| 1892 | switch (action) { |
| 1893 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
| 1894 | if (getCurrentPage() < getPageCount() - 1) { |
| 1895 | scrollRight(); |
| 1896 | return true; |
| 1897 | } |
| 1898 | } break; |
| 1899 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
| 1900 | if (getCurrentPage() > 0) { |
| 1901 | scrollLeft(); |
| 1902 | return true; |
| 1903 | } |
| 1904 | } break; |
| 1905 | } |
| 1906 | return false; |
| 1907 | } |
| 1908 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1909 | protected String getCurrentPageDescription() { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1910 | return String.format(getContext().getString(R.string.default_scroll_format), |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 1911 | getNextPage() + 1, getChildCount()); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1912 | } |
Winson Chung | d11265e | 2011-08-30 13:37:23 -0700 | [diff] [blame] | 1913 | |
| 1914 | @Override |
| 1915 | public boolean onHoverEvent(android.view.MotionEvent event) { |
| 1916 | return true; |
| 1917 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1918 | } |