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 | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 19 | import java.util.ArrayList; |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 20 | |
Winson Chung | 228a0fa | 2011-01-26 22:14:13 -0800 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorInflater; |
| 23 | import android.animation.AnimatorListenerAdapter; |
| 24 | import android.animation.ObjectAnimator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 26 | import android.content.res.TypedArray; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 27 | import android.graphics.Canvas; |
| 28 | import android.graphics.Rect; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 29 | import android.os.Parcel; |
| 30 | import android.os.Parcelable; |
| 31 | import android.util.AttributeSet; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 32 | import android.util.Log; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 33 | import android.view.ActionMode; |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 34 | import android.view.InputDevice; |
| 35 | import android.view.KeyEvent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 36 | import android.view.MotionEvent; |
| 37 | import android.view.VelocityTracker; |
| 38 | import android.view.View; |
| 39 | import android.view.ViewConfiguration; |
| 40 | import android.view.ViewGroup; |
| 41 | import android.view.ViewParent; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 42 | import android.view.animation.Interpolator; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 43 | import android.widget.Checkable; |
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 | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 48 | /** |
| 49 | * An abstraction of the original Workspace which supports browsing through a |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 50 | * sequential list of "pages" |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 51 | */ |
| 52 | public abstract class PagedView extends ViewGroup { |
| 53 | private static final String TAG = "PagedView"; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 54 | private static final boolean DEBUG = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 55 | protected static final int INVALID_PAGE = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 56 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 57 | // 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] | 58 | private static final int MIN_LENGTH_FOR_FLING = 25; |
| 59 | // The min drag distance to trigger a page shift (regardless of velocity) |
| 60 | private static final int MIN_LENGTH_FOR_MOVE = 200; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 61 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 62 | private static final int PAGE_SNAP_ANIMATION_DURATION = 550; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 63 | protected static final float NANOTIME_DIV = 1000000000.0f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 64 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 65 | private static final float OVERSCROLL_DAMP_FACTOR = 0.08f; |
| 66 | private static final int MINIMUM_SNAP_VELOCITY = 2200; |
| 67 | private static final int MIN_FLING_VELOCITY = 250; |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 68 | private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 69 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 70 | // the velocity at which a fling gesture will cause us to snap to the next page |
| 71 | protected int mSnapVelocity = 500; |
| 72 | |
| 73 | protected float mSmoothingTime; |
| 74 | protected float mTouchX; |
| 75 | |
| 76 | protected boolean mFirstLayout = true; |
| 77 | |
| 78 | protected int mCurrentPage; |
| 79 | protected int mNextPage = INVALID_PAGE; |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 80 | protected int mRestorePage = -1; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 81 | protected int mMaxScrollX; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 82 | protected Scroller mScroller; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 83 | private VelocityTracker mVelocityTracker; |
| 84 | |
| 85 | private float mDownMotionX; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 86 | protected float mLastMotionX; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 87 | protected float mLastMotionXRemainder; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 88 | protected float mLastMotionY; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 89 | protected float mTotalMotionX; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 90 | private int mLastScreenCenter = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 91 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 92 | protected final static int TOUCH_STATE_REST = 0; |
| 93 | protected final static int TOUCH_STATE_SCROLLING = 1; |
| 94 | protected final static int TOUCH_STATE_PREV_PAGE = 2; |
| 95 | protected final static int TOUCH_STATE_NEXT_PAGE = 3; |
Adam Cohen | e45440e | 2010-10-14 18:33:38 -0700 | [diff] [blame] | 96 | protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 97 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 98 | protected int mTouchState = TOUCH_STATE_REST; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 99 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 100 | protected OnLongClickListener mLongClickListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 101 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 102 | protected boolean mAllowLongPress = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 103 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 104 | protected int mTouchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 105 | private int mPagingTouchSlop; |
| 106 | private int mMaximumVelocity; |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 107 | private int mMinimumWidth; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 108 | protected int mPageSpacing; |
| 109 | protected int mPageLayoutPaddingTop; |
| 110 | protected int mPageLayoutPaddingBottom; |
| 111 | protected int mPageLayoutPaddingLeft; |
| 112 | protected int mPageLayoutPaddingRight; |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 113 | protected int mPageLayoutWidthGap; |
| 114 | protected int mPageLayoutHeightGap; |
Michael Jurka | 36fcb74 | 2011-04-06 17:08:58 -0700 | [diff] [blame] | 115 | protected int mPageLayoutMaxHeight; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 116 | protected int mCellCountX; |
| 117 | protected int mCellCountY; |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 118 | protected boolean mCenterPagesVertically; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 119 | protected boolean mAllowOverScroll = true; |
| 120 | protected int mUnboundedScrollX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 121 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 122 | // 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] | 123 | protected float mLayoutScale = 1.0f; |
| 124 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 125 | protected static final int INVALID_POINTER = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 126 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 127 | protected int mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 128 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 129 | private PageSwitchListener mPageSwitchListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 130 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 131 | private ArrayList<Boolean> mDirtyPageContent; |
| 132 | private boolean mDirtyPageAlpha; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 133 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 134 | // choice modes |
| 135 | protected static final int CHOICE_MODE_NONE = 0; |
| 136 | protected static final int CHOICE_MODE_SINGLE = 1; |
| 137 | // Multiple selection mode is not supported by all Launcher actions atm |
| 138 | protected static final int CHOICE_MODE_MULTIPLE = 2; |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 139 | |
Michael Jurka | e17e19c | 2010-09-28 11:01:39 -0700 | [diff] [blame] | 140 | protected int mChoiceMode; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 141 | private ActionMode mActionMode; |
| 142 | |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 143 | // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in |
| 144 | // AllApps and Customize, and allows them to share holographic icons for the application view |
| 145 | // (which is in both). |
| 146 | protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache(); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 147 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 148 | // If true, syncPages and syncPageItems will be called to refresh pages |
| 149 | protected boolean mContentIsRefreshable = true; |
| 150 | |
| 151 | // If true, modify alpha of neighboring pages as user scrolls left/right |
| 152 | protected boolean mFadeInAdjacentScreens = true; |
| 153 | |
| 154 | // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding |
| 155 | // to switch to a new page |
| 156 | protected boolean mUsePagingTouchSlop = true; |
| 157 | |
| 158 | // If true, the subclass should directly update mScrollX itself in its computeScroll method |
| 159 | // (SmoothPagedView does this) |
| 160 | protected boolean mDeferScrollUpdate = false; |
| 161 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 162 | protected boolean mIsPageMoving = false; |
| 163 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 164 | public interface PageSwitchListener { |
| 165 | void onPageSwitch(View newPage, int newPageIndex); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 168 | public PagedView(Context context) { |
| 169 | this(context, null); |
| 170 | } |
| 171 | |
| 172 | public PagedView(Context context, AttributeSet attrs) { |
| 173 | this(context, attrs, 0); |
| 174 | } |
| 175 | |
| 176 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 177 | super(context, attrs, defStyle); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 178 | mChoiceMode = CHOICE_MODE_NONE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 179 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 180 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 181 | R.styleable.PagedView, defStyle, 0); |
| 182 | mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0); |
| 183 | mPageLayoutPaddingTop = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 184 | R.styleable.PagedView_pageLayoutPaddingTop, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 185 | mPageLayoutPaddingBottom = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 186 | R.styleable.PagedView_pageLayoutPaddingBottom, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 187 | mPageLayoutPaddingLeft = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 188 | R.styleable.PagedView_pageLayoutPaddingLeft, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 189 | mPageLayoutPaddingRight = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 190 | R.styleable.PagedView_pageLayoutPaddingRight, 0); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 191 | mPageLayoutWidthGap = a.getDimensionPixelSize( |
| 192 | R.styleable.PagedView_pageLayoutWidthGap, -1); |
| 193 | mPageLayoutHeightGap = a.getDimensionPixelSize( |
| 194 | R.styleable.PagedView_pageLayoutHeightGap, -1); |
Michael Jurka | 36fcb74 | 2011-04-06 17:08:58 -0700 | [diff] [blame] | 195 | mPageLayoutMaxHeight = a.getDimensionPixelSize( |
| 196 | R.styleable.PagedView_pageLayoutMaxHeight, -1); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 197 | a.recycle(); |
| 198 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 199 | setHapticFeedbackEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 200 | init(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Initializes various states for this workspace. |
| 205 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 206 | protected void init() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 207 | mDirtyPageContent = new ArrayList<Boolean>(); |
| 208 | mDirtyPageContent.ensureCapacity(32); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 209 | mScroller = new Scroller(getContext(), new ScrollInterpolator()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 210 | mCurrentPage = 0; |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 211 | mCenterPagesVertically = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 212 | |
| 213 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
| 214 | mTouchSlop = configuration.getScaledTouchSlop(); |
| 215 | mPagingTouchSlop = configuration.getScaledPagingTouchSlop(); |
| 216 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
| 217 | } |
| 218 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 219 | public void setPageSwitchListener(PageSwitchListener pageSwitchListener) { |
| 220 | mPageSwitchListener = pageSwitchListener; |
| 221 | if (mPageSwitchListener != null) { |
| 222 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 227 | * Returns the index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 228 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 229 | * @return The index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 230 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 231 | int getCurrentPage() { |
| 232 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 235 | int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 236 | return getChildCount(); |
| 237 | } |
| 238 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 239 | View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 240 | return getChildAt(index); |
| 241 | } |
| 242 | |
| 243 | int getScrollWidth() { |
| 244 | return getWidth(); |
| 245 | } |
| 246 | |
Adam Cohen | be90934 | 2011-01-28 17:02:58 -0800 | [diff] [blame] | 247 | public int getTouchState() { |
| 248 | return mTouchState; |
| 249 | } |
| 250 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 251 | /** |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 252 | * Updates the scroll of the current page immediately to its final scroll position. We use this |
| 253 | * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of |
| 254 | * the previous tab page. |
| 255 | */ |
| 256 | protected void updateCurrentPageScroll() { |
| 257 | int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage); |
| 258 | scrollTo(newX, 0); |
| 259 | mScroller.setFinalX(newX); |
| 260 | } |
| 261 | |
| 262 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 263 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 264 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 265 | void setCurrentPage(int currentPage) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 266 | if (!mScroller.isFinished()) { |
| 267 | mScroller.abortAnimation(); |
| 268 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 269 | // don't introduce any checks like mCurrentPage == currentPage here-- if we change the |
| 270 | // the default |
| 271 | if (getChildCount() == 0) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 272 | return; |
| 273 | } |
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 | mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1)); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 276 | updateCurrentPageScroll(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 277 | notifyPageSwitchListener(); |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 278 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 281 | protected void notifyPageSwitchListener() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 282 | if (mPageSwitchListener != null) { |
| 283 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 287 | protected void pageBeginMoving() { |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 288 | mIsPageMoving = true; |
| 289 | onPageBeginMoving(); |
| 290 | } |
| 291 | |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 292 | protected void pageEndMoving() { |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 293 | onPageEndMoving(); |
| 294 | mIsPageMoving = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 297 | protected boolean isPageMoving() { |
| 298 | return mIsPageMoving; |
| 299 | } |
| 300 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 301 | // a method that subclasses can override to add behavior |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 302 | protected void onPageBeginMoving() { |
| 303 | } |
| 304 | |
| 305 | // a method that subclasses can override to add behavior |
| 306 | protected void onPageEndMoving() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 309 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 310 | * Registers the specified listener on each page contained in this workspace. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 311 | * |
| 312 | * @param l The listener used to respond to long clicks. |
| 313 | */ |
| 314 | @Override |
| 315 | public void setOnLongClickListener(OnLongClickListener l) { |
| 316 | mLongClickListener = l; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 317 | final int count = getPageCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 318 | for (int i = 0; i < count; i++) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 319 | getPageAt(i).setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | @Override |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 324 | public void scrollBy(int x, int y) { |
| 325 | scrollTo(mUnboundedScrollX + x, mScrollY + y); |
| 326 | } |
| 327 | |
| 328 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 329 | public void scrollTo(int x, int y) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 330 | mUnboundedScrollX = x; |
| 331 | |
| 332 | if (x < 0) { |
| 333 | super.scrollTo(0, y); |
| 334 | if (mAllowOverScroll) { |
| 335 | overScroll(x); |
| 336 | } |
| 337 | } else if (x > mMaxScrollX) { |
| 338 | super.scrollTo(mMaxScrollX, y); |
| 339 | if (mAllowOverScroll) { |
| 340 | overScroll(x - mMaxScrollX); |
| 341 | } |
| 342 | } else { |
| 343 | super.scrollTo(x, y); |
| 344 | } |
| 345 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 346 | mTouchX = x; |
| 347 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 348 | } |
| 349 | |
| 350 | // we moved this functionality to a helper function so SmoothPagedView can reuse it |
| 351 | protected boolean computeScrollHelper() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 352 | if (mScroller.computeScrollOffset()) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 353 | mDirtyPageAlpha = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 354 | scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 355 | invalidate(); |
| 356 | return true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 357 | } else if (mNextPage != INVALID_PAGE) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 358 | mDirtyPageAlpha = true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 359 | mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 360 | mNextPage = INVALID_PAGE; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 361 | notifyPageSwitchListener(); |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 362 | // We don't want to trigger a page end moving unless the page has settled |
| 363 | // and the user has stopped scrolling |
| 364 | if (mTouchState == TOUCH_STATE_REST) { |
| 365 | pageEndMoving(); |
| 366 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 367 | return true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 368 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 369 | return false; |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public void computeScroll() { |
| 374 | computeScrollHelper(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | @Override |
| 378 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 379 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 380 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 381 | if (widthMode != MeasureSpec.EXACTLY) { |
| 382 | throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); |
| 383 | } |
| 384 | |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 385 | /* Allow the height to be set as WRAP_CONTENT. This allows the particular case |
| 386 | * of the All apps view on XLarge displays to not take up more space then it needs. Width |
| 387 | * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect |
| 388 | * each page to have the same width. |
| 389 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 390 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 391 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
| 392 | int maxChildHeight = 0; |
| 393 | |
| 394 | final int verticalPadding = mPaddingTop + mPaddingBottom; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 395 | |
Michael Jurka | 36fcb74 | 2011-04-06 17:08:58 -0700 | [diff] [blame] | 396 | if (mPageLayoutMaxHeight != -1) { |
| 397 | heightSize = Math.min(mPageLayoutMaxHeight, heightSize); |
| 398 | } |
| 399 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 400 | // The children are given the same width and height as the workspace |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 401 | // unless they were set to WRAP_CONTENT |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 402 | if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 403 | final int childCount = getChildCount(); |
| 404 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 405 | // disallowing padding in paged view (just pass 0) |
| 406 | final View child = getChildAt(i); |
| 407 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 408 | |
| 409 | int childWidthMode; |
| 410 | if (lp.width == LayoutParams.WRAP_CONTENT) { |
| 411 | childWidthMode = MeasureSpec.AT_MOST; |
| 412 | } else { |
| 413 | childWidthMode = MeasureSpec.EXACTLY; |
| 414 | } |
| 415 | |
| 416 | int childHeightMode; |
| 417 | if (lp.height == LayoutParams.WRAP_CONTENT) { |
| 418 | childHeightMode = MeasureSpec.AT_MOST; |
| 419 | } else { |
| 420 | childHeightMode = MeasureSpec.EXACTLY; |
| 421 | } |
| 422 | |
| 423 | final int childWidthMeasureSpec = |
| 424 | MeasureSpec.makeMeasureSpec(widthSize, childWidthMode); |
| 425 | final int childHeightMeasureSpec = |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 426 | MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 427 | |
| 428 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 429 | maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 430 | if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", " |
| 431 | + child.getMeasuredHeight()); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | if (heightMode == MeasureSpec.AT_MOST) { |
| 435 | heightSize = maxChildHeight + verticalPadding; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 436 | } |
Adam Cohen | faa2830 | 2010-11-19 12:02:18 -0800 | [diff] [blame] | 437 | if (childCount > 0) { |
| 438 | mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1); |
| 439 | } else { |
| 440 | mMaxScrollX = 0; |
| 441 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 442 | |
| 443 | setMeasuredDimension(widthSize, heightSize); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 446 | protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) { |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 447 | int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage); |
| 448 | int delta = newX - mScrollX; |
| 449 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 450 | final int pageCount = getChildCount(); |
| 451 | for (int i = 0; i < pageCount; i++) { |
| 452 | View page = (View) getChildAt(i); |
| 453 | page.setX(page.getX() + delta); |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 454 | } |
| 455 | setCurrentPage(newCurrentPage); |
| 456 | } |
| 457 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 458 | // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a |
| 459 | // 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] | 460 | // tightens the layout accordingly |
| 461 | public void setLayoutScale(float childrenScale) { |
| 462 | mLayoutScale = childrenScale; |
| 463 | |
| 464 | // Now we need to do a re-layout, but preserving absolute X and Y coordinates |
| 465 | int childCount = getChildCount(); |
| 466 | float childrenX[] = new float[childCount]; |
| 467 | float childrenY[] = new float[childCount]; |
| 468 | for (int i = 0; i < childCount; i++) { |
| 469 | final View child = getChildAt(i); |
| 470 | childrenX[i] = child.getX(); |
| 471 | childrenY[i] = child.getY(); |
| 472 | } |
| 473 | onLayout(false, mLeft, mTop, mRight, mBottom); |
| 474 | for (int i = 0; i < childCount; i++) { |
| 475 | final View child = getChildAt(i); |
| 476 | child.setX(childrenX[i]); |
| 477 | child.setY(childrenY[i]); |
| 478 | } |
| 479 | // Also, the page offset has changed (since the pages are now smaller); |
| 480 | // update the page offset, but again preserving absolute X and Y coordinates |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 481 | scrollToNewPageWithoutMovingPages(mCurrentPage); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 484 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 485 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 486 | if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); |
Michael Jurka | cfc6294 | 2010-09-14 14:01:07 -0700 | [diff] [blame] | 487 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
| 488 | setHorizontalScrollBarEnabled(false); |
| 489 | int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage); |
| 490 | scrollTo(newX, 0); |
| 491 | mScroller.setFinalX(newX); |
| 492 | setHorizontalScrollBarEnabled(true); |
| 493 | mFirstLayout = false; |
| 494 | } |
| 495 | |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 496 | final int verticalPadding = mPaddingTop + mPaddingBottom; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 497 | final int childCount = getChildCount(); |
| 498 | int childLeft = 0; |
| 499 | if (childCount > 0) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 500 | if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", " |
| 501 | + getChildWidth(0)); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 502 | childLeft = getRelativeChildOffset(0); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | for (int i = 0; i < childCount; i++) { |
| 506 | final View child = getChildAt(i); |
| 507 | if (child.getVisibility() != View.GONE) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 508 | final int childWidth = getScaledMeasuredWidth(child); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 509 | final int childHeight = child.getMeasuredHeight(); |
| 510 | int childTop = mPaddingTop; |
| 511 | if (mCenterPagesVertically) { |
| 512 | childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2; |
| 513 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 514 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 515 | if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 516 | child.layout(childLeft, childTop, |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 517 | childLeft + child.getMeasuredWidth(), childTop + childHeight); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 518 | childLeft += childWidth + mPageSpacing; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 521 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
| 522 | mFirstLayout = false; |
| 523 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Winson Chung | 0392977 | 2011-02-23 17:07:10 -0800 | [diff] [blame] | 526 | protected void forceUpdateAdjacentPagesAlpha() { |
| 527 | mDirtyPageAlpha = true; |
| 528 | updateAdjacentPagesAlpha(); |
| 529 | } |
| 530 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 531 | protected void updateAdjacentPagesAlpha() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 532 | if (mFadeInAdjacentScreens) { |
| 533 | if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 534 | int halfScreenSize = getMeasuredWidth() / 2; |
| 535 | int screenCenter = mScrollX + halfScreenSize; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 536 | final int childCount = getChildCount(); |
| 537 | for (int i = 0; i < childCount; ++i) { |
| 538 | View layout = (View) getChildAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 539 | int childWidth = getScaledMeasuredWidth(layout); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 540 | int halfChildWidth = (childWidth / 2); |
| 541 | int childCenter = getChildOffset(i) + halfChildWidth; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 542 | |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 543 | // On the first layout, we may not have a width nor a proper offset, so for now |
| 544 | // we should just assume full page width (and calculate the offset according to |
| 545 | // that). |
| 546 | if (childWidth <= 0) { |
| 547 | childWidth = getMeasuredWidth(); |
| 548 | childCenter = (i * childWidth) + (childWidth / 2); |
| 549 | } |
| 550 | |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 551 | int d = halfChildWidth; |
| 552 | int distanceFromScreenCenter = childCenter - screenCenter; |
| 553 | if (distanceFromScreenCenter > 0) { |
| 554 | if (i > 0) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 555 | d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 556 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 557 | } else { |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 558 | if (i < childCount - 1) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 559 | d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 560 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 561 | } |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 562 | d += mPageSpacing; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 563 | |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 564 | // Preventing potential divide-by-zero |
| 565 | d = Math.max(1, d); |
| 566 | |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 567 | float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d; |
| 568 | dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha))); |
| 569 | float alpha = 1.0f - dimAlpha; |
| 570 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 571 | if (alpha < ALPHA_QUANTIZE_LEVEL) { |
| 572 | alpha = 0.0f; |
| 573 | } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) { |
| 574 | alpha = 1.0f; |
| 575 | } |
| 576 | |
Michael Jurka | a40829a | 2011-02-16 18:02:45 -0800 | [diff] [blame] | 577 | // Due to the way we're setting alpha on our children in PagedViewCellLayout, |
| 578 | // this optimization causes alpha to not be properly updated sometimes (repro |
| 579 | // case: in xlarge mode, swipe to second page in All Apps, then click on "My |
| 580 | // Apps" tab. the page will have alpha 0 until you swipe it). Removing |
| 581 | // optimization fixes the issue, but we should fix this in a better manner |
Michael Jurka | cfdb096 | 2011-02-04 01:38:00 -0800 | [diff] [blame] | 582 | //if (Float.compare(alpha, layout.getAlpha()) != 0) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 583 | layout.setAlpha(alpha); |
Michael Jurka | cfdb096 | 2011-02-04 01:38:00 -0800 | [diff] [blame] | 584 | //} |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 585 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 586 | mDirtyPageAlpha = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 587 | } |
| 588 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 591 | protected void screenScrolled(int screenCenter) { |
| 592 | } |
| 593 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 594 | @Override |
| 595 | protected void dispatchDraw(Canvas canvas) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 596 | int halfScreenSize = getMeasuredWidth() / 2; |
| 597 | int screenCenter = mScrollX + halfScreenSize; |
| 598 | |
| 599 | if (screenCenter != mLastScreenCenter) { |
| 600 | screenScrolled(screenCenter); |
| 601 | updateAdjacentPagesAlpha(); |
| 602 | mLastScreenCenter = screenCenter; |
| 603 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 604 | |
| 605 | // Find out which screens are visible; as an optimization we only call draw on them |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 606 | // As an optimization, this code assumes that all pages have the same width as the 0th |
| 607 | // page. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 608 | final int pageCount = getChildCount(); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 609 | if (pageCount > 0) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 610 | final int pageWidth = getScaledMeasuredWidth(getChildAt(0)); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 611 | final int screenWidth = getMeasuredWidth(); |
| 612 | int x = getRelativeChildOffset(0) + pageWidth; |
| 613 | int leftScreen = 0; |
| 614 | int rightScreen = 0; |
| 615 | while (x <= mScrollX) { |
| 616 | leftScreen++; |
Winson Chung | c3f9f4f | 2010-12-14 17:25:59 -0800 | [diff] [blame] | 617 | x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing; |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 618 | } |
| 619 | rightScreen = leftScreen; |
Winson Chung | c3f9f4f | 2010-12-14 17:25:59 -0800 | [diff] [blame] | 620 | while (x < mScrollX + screenWidth && rightScreen < pageCount) { |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 621 | rightScreen++; |
Winson Chung | c3f9f4f | 2010-12-14 17:25:59 -0800 | [diff] [blame] | 622 | if (rightScreen < pageCount) { |
| 623 | x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing; |
| 624 | } |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 625 | } |
| 626 | rightScreen = Math.min(getChildCount() - 1, rightScreen); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 627 | |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 628 | final long drawingTime = getDrawingTime(); |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 629 | // Clip to the bounds |
| 630 | canvas.save(); |
| 631 | canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft, |
| 632 | mScrollY + mBottom - mTop); |
| 633 | |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 634 | for (int i = leftScreen; i <= rightScreen; i++) { |
| 635 | drawChild(canvas, getChildAt(i), drawingTime); |
| 636 | } |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 637 | canvas.restore(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 638 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 643 | int page = indexOfChild(child); |
| 644 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 645 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 646 | return true; |
| 647 | } |
| 648 | return false; |
| 649 | } |
| 650 | |
| 651 | @Override |
| 652 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 653 | int focusablePage; |
| 654 | if (mNextPage != INVALID_PAGE) { |
| 655 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 656 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 657 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 658 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 659 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 660 | if (v != null) { |
| 661 | v.requestFocus(direction, previouslyFocusedRect); |
| 662 | } |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | @Override |
| 667 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 668 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 669 | if (getCurrentPage() > 0) { |
| 670 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 671 | return true; |
| 672 | } |
| 673 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 674 | if (getCurrentPage() < getPageCount() - 1) { |
| 675 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 676 | return true; |
| 677 | } |
| 678 | } |
| 679 | return super.dispatchUnhandledMove(focused, direction); |
| 680 | } |
| 681 | |
| 682 | @Override |
| 683 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 684 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
| 685 | getPageAt(mCurrentPage).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 686 | } |
| 687 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 688 | if (mCurrentPage > 0) { |
| 689 | getPageAt(mCurrentPage - 1).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 690 | } |
| 691 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 692 | if (mCurrentPage < getPageCount() - 1) { |
| 693 | getPageAt(mCurrentPage + 1).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * 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] | 700 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 701 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 702 | * This happens when live folders requery, and if they're off page, they |
| 703 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 704 | */ |
| 705 | @Override |
| 706 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 707 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 708 | View v = focused; |
| 709 | while (true) { |
| 710 | if (v == current) { |
| 711 | super.focusableViewAvailable(focused); |
| 712 | return; |
| 713 | } |
| 714 | if (v == this) { |
| 715 | return; |
| 716 | } |
| 717 | ViewParent parent = v.getParent(); |
| 718 | if (parent instanceof View) { |
| 719 | v = (View)v.getParent(); |
| 720 | } else { |
| 721 | return; |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * {@inheritDoc} |
| 728 | */ |
| 729 | @Override |
| 730 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 731 | if (disallowIntercept) { |
| 732 | // We need to make sure to cancel our long press if |
| 733 | // a scrollable widget takes over touch events |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 734 | final View currentPage = getChildAt(mCurrentPage); |
| 735 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 736 | } |
| 737 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 738 | } |
| 739 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 740 | /** |
| 741 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 742 | */ |
| 743 | protected boolean hitsPreviousPage(float x, float y) { |
| 744 | return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing); |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 749 | */ |
| 750 | protected boolean hitsNextPage(float x, float y) { |
| 751 | return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing)); |
| 752 | } |
| 753 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 754 | @Override |
| 755 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 756 | /* |
| 757 | * This method JUST determines whether we want to intercept the motion. |
| 758 | * If we return true, onTouchEvent will be called and we do the actual |
| 759 | * scrolling there. |
| 760 | */ |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 761 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 762 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 763 | // Skip touch handling if there are no pages to swipe |
| 764 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 765 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 766 | /* |
| 767 | * Shortcut the most recurring case: the user is in the dragging |
| 768 | * state and he is moving his finger. We want to intercept this |
| 769 | * motion. |
| 770 | */ |
| 771 | final int action = ev.getAction(); |
| 772 | if ((action == MotionEvent.ACTION_MOVE) && |
| 773 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 774 | return true; |
| 775 | } |
| 776 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 777 | switch (action & MotionEvent.ACTION_MASK) { |
| 778 | case MotionEvent.ACTION_MOVE: { |
| 779 | /* |
| 780 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 781 | * whether the user has moved far enough from his original down touch. |
| 782 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 783 | if (mActivePointerId != INVALID_POINTER) { |
| 784 | determineScrollingStart(ev); |
| 785 | break; |
| 786 | } |
| 787 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 788 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 789 | // i.e. fall through to the next case (don't break) |
| 790 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 791 | // 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] | 792 | } |
| 793 | |
| 794 | case MotionEvent.ACTION_DOWN: { |
| 795 | final float x = ev.getX(); |
| 796 | final float y = ev.getY(); |
| 797 | // Remember location of down touch |
| 798 | mDownMotionX = x; |
| 799 | mLastMotionX = x; |
| 800 | mLastMotionY = y; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 801 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 802 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 803 | mActivePointerId = ev.getPointerId(0); |
| 804 | mAllowLongPress = true; |
| 805 | |
| 806 | /* |
| 807 | * If being flinged and user touches the screen, initiate drag; |
| 808 | * otherwise don't. mScroller.isFinished should be false when |
| 809 | * being flinged. |
| 810 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame] | 811 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 812 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop); |
| 813 | if (finishedScrolling) { |
| 814 | mTouchState = TOUCH_STATE_REST; |
| 815 | mScroller.abortAnimation(); |
| 816 | } else { |
| 817 | mTouchState = TOUCH_STATE_SCROLLING; |
| 818 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 819 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 820 | // 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] | 821 | // to scroll the current page |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 822 | if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 823 | if (getChildCount() > 0) { |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 824 | if (hitsPreviousPage(x, y)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 825 | mTouchState = TOUCH_STATE_PREV_PAGE; |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 826 | } else if (hitsNextPage(x, y)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 827 | mTouchState = TOUCH_STATE_NEXT_PAGE; |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | break; |
| 832 | } |
| 833 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 834 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 835 | onWallpaperTap(ev); |
| 836 | case MotionEvent.ACTION_CANCEL: |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 837 | mTouchState = TOUCH_STATE_REST; |
| 838 | mAllowLongPress = false; |
| 839 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 840 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 841 | break; |
| 842 | |
| 843 | case MotionEvent.ACTION_POINTER_UP: |
| 844 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 845 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 846 | break; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * The only time we want to intercept motion events is if we are in the |
| 851 | * drag mode. |
| 852 | */ |
| 853 | return mTouchState != TOUCH_STATE_REST; |
| 854 | } |
| 855 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 856 | protected void animateClickFeedback(View v, final Runnable r) { |
| 857 | // animate the view slightly to show click feedback running some logic after it is "pressed" |
Winson Chung | 228a0fa | 2011-01-26 22:14:13 -0800 | [diff] [blame] | 858 | ObjectAnimator anim = (ObjectAnimator) AnimatorInflater. |
| 859 | loadAnimator(mContext, R.anim.paged_view_click_feedback); |
| 860 | anim.setTarget(v); |
| 861 | anim.addListener(new AnimatorListenerAdapter() { |
| 862 | public void onAnimationRepeat(Animator animation) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 863 | r.run(); |
| 864 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 865 | }); |
Winson Chung | 228a0fa | 2011-01-26 22:14:13 -0800 | [diff] [blame] | 866 | anim.start(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 869 | protected void determineScrollingStart(MotionEvent ev) { |
| 870 | determineScrollingStart(ev, 1.0f); |
| 871 | } |
| 872 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 873 | /* |
| 874 | * Determines if we should change the touch state to start scrolling after the |
| 875 | * user moves their touch point too far. |
| 876 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 877 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 878 | /* |
| 879 | * Locally do absolute value. mLastMotionX is set to the y value |
| 880 | * of the down event. |
| 881 | */ |
| 882 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 883 | final float x = ev.getX(pointerIndex); |
| 884 | final float y = ev.getY(pointerIndex); |
| 885 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 886 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 887 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 888 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 889 | boolean xPaged = xDiff > mPagingTouchSlop; |
| 890 | boolean xMoved = xDiff > touchSlop; |
| 891 | boolean yMoved = yDiff > touchSlop; |
| 892 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 893 | if (xMoved || xPaged || yMoved) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 894 | if (mUsePagingTouchSlop ? xPaged : xMoved) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 895 | // Scroll if the user moved far enough along the X axis |
| 896 | mTouchState = TOUCH_STATE_SCROLLING; |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 897 | mTotalMotionX += Math.abs(mLastMotionX - x); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 898 | mLastMotionX = x; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 899 | mLastMotionXRemainder = 0; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 900 | mTouchX = mScrollX; |
| 901 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 902 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 903 | } |
| 904 | // Either way, cancel any pending longpress |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 905 | cancelCurrentPageLongPress(); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | protected void cancelCurrentPageLongPress() { |
| 910 | if (mAllowLongPress) { |
| 911 | mAllowLongPress = false; |
| 912 | // Try canceling the long press. It could also have been scheduled |
| 913 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 914 | // everything |
| 915 | final View currentPage = getPageAt(mCurrentPage); |
| 916 | if (currentPage != null) { |
| 917 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 922 | // This curve determines how the effect of scrolling over the limits of the page dimishes |
| 923 | // as the user pulls further and further from the bounds |
| 924 | private float overScrollInfluenceCurve(float f) { |
| 925 | f -= 1.0f; |
| 926 | return f * f * f + 1.0f; |
| 927 | } |
| 928 | |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 929 | protected void overScroll(float amount) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 930 | int screenSize = getMeasuredWidth(); |
| 931 | |
| 932 | float f = (amount / screenSize); |
| 933 | |
| 934 | if (f == 0) return; |
| 935 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 936 | |
Adam Cohen | 7bfc979 | 2011-01-28 13:52:37 -0800 | [diff] [blame] | 937 | // Clamp this factor, f, to -1 < f < 1 |
| 938 | if (Math.abs(f) >= 1) { |
| 939 | f /= Math.abs(f); |
| 940 | } |
| 941 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 942 | int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 943 | if (amount < 0) { |
| 944 | mScrollX = overScrollAmount; |
| 945 | } else { |
| 946 | mScrollX = mMaxScrollX + overScrollAmount; |
| 947 | } |
| 948 | invalidate(); |
| 949 | } |
| 950 | |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 951 | protected float maxOverScroll() { |
| 952 | // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not |
| 953 | // exceed). Used to find out how much extra wallpaper we need for the overscroll effect |
| 954 | float f = 1.0f; |
| 955 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 956 | return OVERSCROLL_DAMP_FACTOR * f; |
| 957 | } |
| 958 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 959 | @Override |
| 960 | public boolean onTouchEvent(MotionEvent ev) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 961 | // Skip touch handling if there are no pages to swipe |
| 962 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 963 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 964 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 965 | |
| 966 | final int action = ev.getAction(); |
| 967 | |
| 968 | switch (action & MotionEvent.ACTION_MASK) { |
| 969 | case MotionEvent.ACTION_DOWN: |
| 970 | /* |
| 971 | * If being flinged and user touches, stop the fling. isFinished |
| 972 | * will be false if being flinged. |
| 973 | */ |
| 974 | if (!mScroller.isFinished()) { |
| 975 | mScroller.abortAnimation(); |
| 976 | } |
| 977 | |
| 978 | // Remember where the motion event started |
| 979 | mDownMotionX = mLastMotionX = ev.getX(); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 980 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 981 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 982 | mActivePointerId = ev.getPointerId(0); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 983 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 984 | pageBeginMoving(); |
| 985 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 986 | break; |
| 987 | |
| 988 | case MotionEvent.ACTION_MOVE: |
| 989 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 990 | // Scroll to follow the motion event |
| 991 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 992 | final float x = ev.getX(pointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 993 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 994 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 995 | mTotalMotionX += Math.abs(deltaX); |
| 996 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 997 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 998 | // keep the remainder because we are actually testing if we've moved from the last |
| 999 | // scrolled position (which is discrete). |
| 1000 | if (Math.abs(deltaX) >= 1.0f) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1001 | mTouchX += deltaX; |
| 1002 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1003 | if (!mDeferScrollUpdate) { |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1004 | scrollBy((int) deltaX, 0); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 1005 | if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1006 | } else { |
| 1007 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1008 | } |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1009 | mLastMotionX = x; |
| 1010 | mLastMotionXRemainder = deltaX - (int) deltaX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1011 | } else { |
| 1012 | awakenScrollBars(); |
| 1013 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1014 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1015 | determineScrollingStart(ev); |
| 1016 | } |
| 1017 | break; |
| 1018 | |
| 1019 | case MotionEvent.ACTION_UP: |
| 1020 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1021 | final int activePointerId = mActivePointerId; |
| 1022 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 1023 | final float x = ev.getX(pointerIndex); |
| 1024 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1025 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1026 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 1027 | final int deltaX = (int) (x - mDownMotionX); |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1028 | boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1029 | final int snapVelocity = mSnapVelocity; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1030 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1031 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1032 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1033 | // In the case that the page is moved far to one direction and then is flung |
| 1034 | // in the opposite direction, we use a threshold to determine whether we should |
| 1035 | // just return to the starting page, or if we should skip one further. |
| 1036 | boolean returnToOriginalPage = false; |
| 1037 | final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage)); |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1038 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1039 | Math.signum(velocityX) != Math.signum(deltaX)) { |
| 1040 | returnToOriginalPage = true; |
| 1041 | } |
| 1042 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1043 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1044 | Math.abs(velocityX) > snapVelocity; |
| 1045 | |
| 1046 | int finalPage; |
| 1047 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1048 | // test for a large move if a fling has been registered. That is, a large |
| 1049 | // move to the left and fling to the right will register as a fling to the right. |
| 1050 | if (((isSignificantMove && deltaX > 0 && !isFling) || |
| 1051 | (isFling && velocityX > 0)) && mCurrentPage > 0) { |
| 1052 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1053 | snapToPageWithVelocity(finalPage, velocityX); |
| 1054 | } else if (((isSignificantMove && deltaX < 0 && !isFling) || |
| 1055 | (isFling && velocityX < 0)) && |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1056 | mCurrentPage < getChildCount() - 1) { |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1057 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1058 | snapToPageWithVelocity(finalPage, velocityX); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1059 | } else { |
| 1060 | snapToDestination(); |
| 1061 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1062 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1063 | // at this point we have not moved beyond the touch slop |
| 1064 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1065 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1066 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1067 | if (nextPage != mCurrentPage) { |
| 1068 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1069 | } else { |
| 1070 | snapToDestination(); |
| 1071 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1072 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1073 | // at this point we have not moved beyond the touch slop |
| 1074 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1075 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1076 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1077 | if (nextPage != mCurrentPage) { |
| 1078 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1079 | } else { |
| 1080 | snapToDestination(); |
| 1081 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1082 | } else { |
| 1083 | onWallpaperTap(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1084 | } |
| 1085 | mTouchState = TOUCH_STATE_REST; |
| 1086 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1087 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1088 | break; |
| 1089 | |
| 1090 | case MotionEvent.ACTION_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1091 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1092 | snapToDestination(); |
| 1093 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1094 | mTouchState = TOUCH_STATE_REST; |
| 1095 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1096 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1097 | break; |
| 1098 | |
| 1099 | case MotionEvent.ACTION_POINTER_UP: |
| 1100 | onSecondaryPointerUp(ev); |
| 1101 | break; |
| 1102 | } |
| 1103 | |
| 1104 | return true; |
| 1105 | } |
| 1106 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1107 | @Override |
| 1108 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1109 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1110 | switch (event.getAction()) { |
| 1111 | case MotionEvent.ACTION_SCROLL: { |
| 1112 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1113 | final float vscroll; |
| 1114 | final float hscroll; |
| 1115 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1116 | vscroll = 0; |
| 1117 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1118 | } else { |
| 1119 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1120 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1121 | } |
| 1122 | if (hscroll != 0 || vscroll != 0) { |
| 1123 | if (hscroll > 0 || vscroll > 0) { |
| 1124 | scrollRight(); |
| 1125 | } else { |
| 1126 | scrollLeft(); |
| 1127 | } |
| 1128 | return true; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | return super.onGenericMotionEvent(event); |
| 1134 | } |
| 1135 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1136 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1137 | if (mVelocityTracker == null) { |
| 1138 | mVelocityTracker = VelocityTracker.obtain(); |
| 1139 | } |
| 1140 | mVelocityTracker.addMovement(ev); |
| 1141 | } |
| 1142 | |
| 1143 | private void releaseVelocityTracker() { |
| 1144 | if (mVelocityTracker != null) { |
| 1145 | mVelocityTracker.recycle(); |
| 1146 | mVelocityTracker = null; |
| 1147 | } |
| 1148 | } |
| 1149 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1150 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 1151 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 1152 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 1153 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1154 | if (pointerId == mActivePointerId) { |
| 1155 | // This was our active pointer going up. Choose a new |
| 1156 | // active pointer and adjust accordingly. |
| 1157 | // TODO: Make this decision more intelligent. |
| 1158 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 1159 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 1160 | mLastMotionY = ev.getY(newPointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1161 | mLastMotionXRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1162 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1163 | if (mVelocityTracker != null) { |
| 1164 | mVelocityTracker.clear(); |
| 1165 | } |
| 1166 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1167 | if (mTouchState == TOUCH_STATE_REST) { |
| 1168 | onWallpaperTap(ev); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | protected void onWallpaperTap(MotionEvent ev) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | @Override |
| 1176 | public void requestChildFocus(View child, View focused) { |
| 1177 | super.requestChildFocus(child, focused); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1178 | int page = indexOfChild(child); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1179 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1180 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1181 | } |
| 1182 | } |
| 1183 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1184 | protected int getChildIndexForRelativeOffset(int relativeOffset) { |
| 1185 | final int childCount = getChildCount(); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 1186 | int left; |
| 1187 | int right; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1188 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 1189 | left = getRelativeChildOffset(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1190 | right = (left + getScaledMeasuredWidth(getChildAt(i))); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1191 | if (left <= relativeOffset && relativeOffset <= right) { |
| 1192 | return i; |
| 1193 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1194 | } |
| 1195 | return -1; |
| 1196 | } |
| 1197 | |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1198 | protected void setMinimumWidthOverride(int minimumWidth) { |
| 1199 | mMinimumWidth = minimumWidth; |
| 1200 | } |
Winson Chung | 34b23d5 | 2011-03-18 11:29:34 -0700 | [diff] [blame] | 1201 | protected void resetMinimumWidthOverride() { |
| 1202 | mMinimumWidth = 0; |
| 1203 | } |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1204 | |
| 1205 | protected int getChildWidth(int index) { |
| 1206 | return Math.max(mMinimumWidth, getChildAt(index).getMeasuredWidth()); |
| 1207 | } |
| 1208 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1209 | protected int getRelativeChildOffset(int index) { |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1210 | return (getMeasuredWidth() - getChildWidth(index)) / 2; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | protected int getChildOffset(int index) { |
| 1214 | if (getChildCount() == 0) |
| 1215 | return 0; |
| 1216 | |
| 1217 | int offset = getRelativeChildOffset(0); |
| 1218 | for (int i = 0; i < index; ++i) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1219 | offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1220 | } |
| 1221 | return offset; |
| 1222 | } |
| 1223 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1224 | protected int getScaledMeasuredWidth(View child) { |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1225 | return (int) (Math.max(mMinimumWidth, child.getMeasuredWidth()) * mLayoutScale + 0.5f); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1226 | } |
| 1227 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1228 | int getPageNearestToCenterOfScreen() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1229 | int minDistanceFromScreenCenter = getMeasuredWidth(); |
| 1230 | int minDistanceFromScreenCenterIndex = -1; |
| 1231 | int screenCenter = mScrollX + (getMeasuredWidth() / 2); |
| 1232 | final int childCount = getChildCount(); |
| 1233 | for (int i = 0; i < childCount; ++i) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1234 | View layout = (View) getChildAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1235 | int childWidth = getScaledMeasuredWidth(layout); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1236 | int halfChildWidth = (childWidth / 2); |
| 1237 | int childCenter = getChildOffset(i) + halfChildWidth; |
| 1238 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 1239 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1240 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1241 | minDistanceFromScreenCenterIndex = i; |
| 1242 | } |
| 1243 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1244 | return minDistanceFromScreenCenterIndex; |
| 1245 | } |
| 1246 | |
| 1247 | protected void snapToDestination() { |
| 1248 | snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1251 | private static class ScrollInterpolator implements Interpolator { |
| 1252 | public ScrollInterpolator() { |
| 1253 | } |
| 1254 | |
| 1255 | public float getInterpolation(float t) { |
| 1256 | t -= 1.0f; |
| 1257 | return t*t*t*t*t + 1; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1262 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1263 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1264 | // of travel has on the overall snap duration. |
| 1265 | float distanceInfluenceForSnapDuration(float f) { |
| 1266 | f -= 0.5f; // center the values about 0. |
| 1267 | f *= 0.3f * Math.PI / 2.0f; |
| 1268 | return (float) Math.sin(f); |
| 1269 | } |
| 1270 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1271 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1272 | whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1)); |
| 1273 | int halfScreenSize = getMeasuredWidth() / 2; |
| 1274 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 1275 | if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage)); |
| 1276 | if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): " |
| 1277 | + getMeasuredWidth() + ", " + getChildWidth(whichPage)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1278 | final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
| 1279 | int delta = newX - mUnboundedScrollX; |
| 1280 | int duration = 0; |
| 1281 | |
| 1282 | if (Math.abs(velocity) < MIN_FLING_VELOCITY) { |
| 1283 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 1284 | // as opposed to an apparent physical response to flinging |
| 1285 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
| 1286 | return; |
| 1287 | } |
| 1288 | |
| 1289 | // Here we compute a "distance" that will be used in the computation of the overall |
| 1290 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 1291 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 1292 | // duration as a function of the distance the page needs to travel. |
| 1293 | float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize; |
| 1294 | float distance = halfScreenSize + halfScreenSize * |
| 1295 | distanceInfluenceForSnapDuration(distanceRatio); |
| 1296 | |
| 1297 | velocity = Math.abs(velocity); |
| 1298 | velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity); |
| 1299 | |
| 1300 | // we want the page's snap velocity to approximately match the velocity at which the |
| 1301 | // user flings, so we scale the duration by a value near to the derivative of the scroll |
| 1302 | // interpolator at zero, ie. 5. We use 6 to make it a little slower. |
| 1303 | duration = 6 * Math.round(1000 * Math.abs(distance / velocity)); |
| 1304 | |
| 1305 | snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | protected void snapToPage(int whichPage) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1309 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1312 | protected void snapToPage(int whichPage, int duration) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1313 | whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1314 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 1315 | if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage)); |
| 1316 | if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", " |
| 1317 | + getChildWidth(whichPage)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1318 | int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1319 | final int sX = mUnboundedScrollX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1320 | final int delta = newX - sX; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1321 | snapToPage(whichPage, delta, duration); |
| 1322 | } |
| 1323 | |
| 1324 | protected void snapToPage(int whichPage, int delta, int duration) { |
| 1325 | mNextPage = whichPage; |
| 1326 | |
| 1327 | View focusedChild = getFocusedChild(); |
| 1328 | if (focusedChild != null && whichPage != mCurrentPage && |
| 1329 | focusedChild == getChildAt(mCurrentPage)) { |
| 1330 | focusedChild.clearFocus(); |
| 1331 | } |
| 1332 | |
| 1333 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1334 | awakenScrollBars(duration); |
| 1335 | if (duration == 0) { |
| 1336 | duration = Math.abs(delta); |
| 1337 | } |
| 1338 | |
| 1339 | if (!mScroller.isFinished()) mScroller.abortAnimation(); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1340 | mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1341 | |
| 1342 | // only load some associated pages |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1343 | loadAssociatedPages(mNextPage); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1344 | notifyPageSwitchListener(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1345 | invalidate(); |
| 1346 | } |
| 1347 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1348 | public void scrollLeft() { |
| 1349 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1350 | if (mCurrentPage > 0) snapToPage(mCurrentPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1351 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1352 | if (mNextPage > 0) snapToPage(mNextPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | public void scrollRight() { |
| 1357 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1358 | if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1359 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1360 | if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1364 | public int getPageForView(View v) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1365 | int result = -1; |
| 1366 | if (v != null) { |
| 1367 | ViewParent vp = v.getParent(); |
| 1368 | int count = getChildCount(); |
| 1369 | for (int i = 0; i < count; i++) { |
| 1370 | if (vp == getChildAt(i)) { |
| 1371 | return i; |
| 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | return result; |
| 1376 | } |
| 1377 | |
| 1378 | /** |
| 1379 | * @return True is long presses are still allowed for the current touch |
| 1380 | */ |
| 1381 | public boolean allowLongPress() { |
| 1382 | return mAllowLongPress; |
| 1383 | } |
| 1384 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1385 | /** |
| 1386 | * Set true to allow long-press events to be triggered, usually checked by |
| 1387 | * {@link Launcher} to accept or block dpad-initiated long-presses. |
| 1388 | */ |
| 1389 | public void setAllowLongPress(boolean allowLongPress) { |
| 1390 | mAllowLongPress = allowLongPress; |
| 1391 | } |
| 1392 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1393 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1394 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1395 | |
| 1396 | SavedState(Parcelable superState) { |
| 1397 | super(superState); |
| 1398 | } |
| 1399 | |
| 1400 | private SavedState(Parcel in) { |
| 1401 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1402 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | @Override |
| 1406 | public void writeToParcel(Parcel out, int flags) { |
| 1407 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1408 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 1412 | new Parcelable.Creator<SavedState>() { |
| 1413 | public SavedState createFromParcel(Parcel in) { |
| 1414 | return new SavedState(in); |
| 1415 | } |
| 1416 | |
| 1417 | public SavedState[] newArray(int size) { |
| 1418 | return new SavedState[size]; |
| 1419 | } |
| 1420 | }; |
| 1421 | } |
| 1422 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1423 | public void loadAssociatedPages(int page) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1424 | if (mContentIsRefreshable) { |
| 1425 | final int count = getChildCount(); |
| 1426 | if (page < count) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1427 | int lowerPageBound = getAssociatedLowerPageBound(page); |
| 1428 | int upperPageBound = getAssociatedUpperPageBound(page); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 1429 | if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/" |
| 1430 | + upperPageBound); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1431 | for (int i = 0; i < count; ++i) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1432 | Page layout = (Page) getChildAt(i); |
| 1433 | final int childCount = layout.getPageChildCount(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1434 | if (lowerPageBound <= i && i <= upperPageBound) { |
| 1435 | if (mDirtyPageContent.get(i)) { |
| 1436 | syncPageItems(i); |
| 1437 | mDirtyPageContent.set(i, false); |
| 1438 | } |
| 1439 | } else { |
| 1440 | if (childCount > 0) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1441 | layout.removeAllViewsOnPage(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1442 | } |
| 1443 | mDirtyPageContent.set(i, true); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1444 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1445 | } |
| 1446 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1447 | } |
| 1448 | } |
| 1449 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1450 | protected int getAssociatedLowerPageBound(int page) { |
| 1451 | return Math.max(0, page - 1); |
| 1452 | } |
| 1453 | protected int getAssociatedUpperPageBound(int page) { |
| 1454 | final int count = getChildCount(); |
| 1455 | return Math.min(page + 1, count - 1); |
| 1456 | } |
| 1457 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1458 | protected void startChoiceMode(int mode, ActionMode.Callback callback) { |
Patrick Dubroy | 430c53b | 2010-09-08 16:01:19 -0700 | [diff] [blame] | 1459 | if (isChoiceMode(CHOICE_MODE_NONE)) { |
| 1460 | mChoiceMode = mode; |
| 1461 | mActionMode = startActionMode(callback); |
| 1462 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 1465 | public void endChoiceMode() { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1466 | if (!isChoiceMode(CHOICE_MODE_NONE)) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1467 | mChoiceMode = CHOICE_MODE_NONE; |
| 1468 | resetCheckedGrandchildren(); |
Michael Jurka | e17e19c | 2010-09-28 11:01:39 -0700 | [diff] [blame] | 1469 | if (mActionMode != null) mActionMode.finish(); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1470 | mActionMode = null; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | protected boolean isChoiceMode(int mode) { |
| 1475 | return mChoiceMode == mode; |
| 1476 | } |
| 1477 | |
| 1478 | protected ArrayList<Checkable> getCheckedGrandchildren() { |
| 1479 | ArrayList<Checkable> checked = new ArrayList<Checkable>(); |
| 1480 | final int childCount = getChildCount(); |
| 1481 | for (int i = 0; i < childCount; ++i) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1482 | Page layout = (Page) getChildAt(i); |
| 1483 | final int grandChildCount = layout.getPageChildCount(); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1484 | for (int j = 0; j < grandChildCount; ++j) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1485 | final View v = layout.getChildOnPageAt(j); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1486 | if (v instanceof Checkable && ((Checkable) v).isChecked()) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1487 | checked.add((Checkable) v); |
| 1488 | } |
| 1489 | } |
| 1490 | } |
| 1491 | return checked; |
| 1492 | } |
| 1493 | |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1494 | /** |
| 1495 | * If in CHOICE_MODE_SINGLE and an item is checked, returns that item. |
| 1496 | * Otherwise, returns null. |
| 1497 | */ |
| 1498 | protected Checkable getSingleCheckedGrandchild() { |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 1499 | if (mChoiceMode != CHOICE_MODE_MULTIPLE) { |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1500 | final int childCount = getChildCount(); |
| 1501 | for (int i = 0; i < childCount; ++i) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1502 | Page layout = (Page) getChildAt(i); |
| 1503 | final int grandChildCount = layout.getPageChildCount(); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1504 | for (int j = 0; j < grandChildCount; ++j) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1505 | final View v = layout.getChildOnPageAt(j); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1506 | if (v instanceof Checkable && ((Checkable) v).isChecked()) { |
| 1507 | return (Checkable) v; |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | return null; |
| 1513 | } |
| 1514 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1515 | protected void resetCheckedGrandchildren() { |
| 1516 | // loop through children, and set all of their children to _not_ be checked |
| 1517 | final ArrayList<Checkable> checked = getCheckedGrandchildren(); |
| 1518 | for (int i = 0; i < checked.size(); ++i) { |
| 1519 | final Checkable c = checked.get(i); |
| 1520 | c.setChecked(false); |
| 1521 | } |
| 1522 | } |
| 1523 | |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 1524 | public void setRestorePage(int restorePage) { |
| 1525 | mRestorePage = restorePage; |
| 1526 | } |
| 1527 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1528 | /** |
| 1529 | * This method is called ONLY to synchronize the number of pages that the paged view has. |
| 1530 | * To actually fill the pages with information, implement syncPageItems() below. It is |
| 1531 | * guaranteed that syncPageItems() will be called for a particular page before it is shown, |
| 1532 | * and therefore, individual page items do not need to be updated in this method. |
| 1533 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1534 | public abstract void syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1535 | |
| 1536 | /** |
| 1537 | * This method is called to synchronize the items that are on a particular page. If views on |
| 1538 | * the page can be reused, then they should be updated within this method. |
| 1539 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1540 | public abstract void syncPageItems(int page); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1541 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1542 | public void invalidatePageData() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1543 | if (mContentIsRefreshable) { |
| 1544 | // Update all the pages |
| 1545 | syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1546 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1547 | // Mark each of the pages as dirty |
| 1548 | final int count = getChildCount(); |
| 1549 | mDirtyPageContent.clear(); |
| 1550 | for (int i = 0; i < count; ++i) { |
| 1551 | mDirtyPageContent.add(true); |
| 1552 | } |
| 1553 | |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 1554 | // Use the restore page if necessary |
| 1555 | if (mRestorePage > -1) { |
| 1556 | mCurrentPage = mRestorePage; |
| 1557 | mRestorePage = -1; |
| 1558 | } |
| 1559 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1560 | // Load any pages that are necessary for the current window of views |
| 1561 | loadAssociatedPages(mCurrentPage); |
| 1562 | mDirtyPageAlpha = true; |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 1563 | updateAdjacentPagesAlpha(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1564 | requestLayout(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1565 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1566 | } |
| 1567 | } |