diff options
author | Alan Viverette <alanv@google.com> | 2013-06-01 01:37:01 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2013-06-01 01:37:01 +0000 |
commit | 66dbeac0e94af70dc4ed93aeba05bdb550ca13be (patch) | |
tree | 5a13cbbd2cfcfe709bf8020d5e443d63b3518339 /core/java/android/widget | |
parent | 3d6a3800c8c99bd086f2d8e605e3b01e4cb20f79 (diff) | |
parent | e7f30dc14dce081058d6739f71ae99abe757fc48 (diff) | |
download | frameworks_base-66dbeac0e94af70dc4ed93aeba05bdb550ca13be.zip frameworks_base-66dbeac0e94af70dc4ed93aeba05bdb550ca13be.tar.gz frameworks_base-66dbeac0e94af70dc4ed93aeba05bdb550ca13be.tar.bz2 |
Merge "Split AbsListView.onTouchEvent into multiple methods for readability."
Diffstat (limited to 'core/java/android/widget')
-rw-r--r-- | core/java/android/widget/AbsListView.java | 595 |
1 files changed, 306 insertions, 289 deletions
diff --git a/core/java/android/widget/AbsListView.java b/core/java/android/widget/AbsListView.java index 4c0ccca..c6cd62e 100644 --- a/core/java/android/widget/AbsListView.java +++ b/core/java/android/widget/AbsListView.java @@ -3308,353 +3308,370 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { - switch (mTouchMode) { - case TOUCH_MODE_OVERFLING: { - mFlingRunnable.endFling(); - if (mPositionScroller != null) { - mPositionScroller.stop(); - } - mTouchMode = TOUCH_MODE_OVERSCROLL; - mMotionX = (int) ev.getX(); - mMotionY = mLastY = (int) ev.getY(); - mMotionCorrection = 0; - mActivePointerId = ev.getPointerId(0); - mDirection = 0; - break; - } + onTouchDown(ev); + break; + } - default: { - mActivePointerId = ev.getPointerId(0); - final int x = (int) ev.getX(); - final int y = (int) ev.getY(); - int motionPosition = pointToPosition(x, y); - if (!mDataChanged) { - if ((mTouchMode != TOUCH_MODE_FLING) && (motionPosition >= 0) - && (getAdapter().isEnabled(motionPosition))) { - // User clicked on an actual view (and was not stopping a fling). - // It might be a click or a scroll. Assume it is a click until - // proven otherwise - mTouchMode = TOUCH_MODE_DOWN; - // FIXME Debounce - if (mPendingCheckForTap == null) { - mPendingCheckForTap = new CheckForTap(); - } - postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); - } else { - if (mTouchMode == TOUCH_MODE_FLING) { - // Stopped a fling. It is a scroll. - createScrollingCache(); - mTouchMode = TOUCH_MODE_SCROLL; - mMotionCorrection = 0; - motionPosition = findMotionRow(y); - mFlingRunnable.flywheelTouch(); - } - } - } + case MotionEvent.ACTION_MOVE: { + onTouchMove(ev); + break; + } - if (motionPosition >= 0) { - // Remember where the motion event started - v = getChildAt(motionPosition - mFirstPosition); - mMotionViewOriginalTop = v.getTop(); - } - mMotionX = x; - mMotionY = y; + case MotionEvent.ACTION_UP: { + onTouchUp(ev); + break; + } + + case MotionEvent.ACTION_CANCEL: { + onTouchCancel(); + break; + } + + case MotionEvent.ACTION_POINTER_UP: { + onSecondaryPointerUp(ev); + final int x = mMotionX; + final int y = mMotionY; + final int motionPosition = pointToPosition(x, y); + if (motionPosition >= 0) { + // Remember where the motion event started + v = getChildAt(motionPosition - mFirstPosition); + mMotionViewOriginalTop = v.getTop(); mMotionPosition = motionPosition; - mLastY = Integer.MIN_VALUE; - break; } + mLastY = y; + break; + } + + case MotionEvent.ACTION_POINTER_DOWN: { + // New pointers take over dragging duties + final int index = ev.getActionIndex(); + final int id = ev.getPointerId(index); + final int x = (int) ev.getX(index); + final int y = (int) ev.getY(index); + mMotionCorrection = 0; + mActivePointerId = id; + mMotionX = x; + mMotionY = y; + final int motionPosition = pointToPosition(x, y); + if (motionPosition >= 0) { + // Remember where the motion event started + v = getChildAt(motionPosition - mFirstPosition); + mMotionViewOriginalTop = v.getTop(); + mMotionPosition = motionPosition; } + mLastY = y; + break; + } + } - if (performButtonActionOnTouchDown(ev)) { - if (mTouchMode == TOUCH_MODE_DOWN) { - removeCallbacks(mPendingCheckForTap); - } + return true; + } + + private void onTouchDown(MotionEvent ev) { + View v; + switch (mTouchMode) { + case TOUCH_MODE_OVERFLING: { + mFlingRunnable.endFling(); + if (mPositionScroller != null) { + mPositionScroller.stop(); } + mTouchMode = TOUCH_MODE_OVERSCROLL; + mMotionX = (int) ev.getX(); + mMotionY = mLastY = (int) ev.getY(); + mMotionCorrection = 0; + mActivePointerId = ev.getPointerId(0); + mDirection = 0; break; } - case MotionEvent.ACTION_MOVE: { - int pointerIndex = ev.findPointerIndex(mActivePointerId); - if (pointerIndex == -1) { - pointerIndex = 0; - mActivePointerId = ev.getPointerId(pointerIndex); + default: { + mActivePointerId = ev.getPointerId(0); + final int x = (int) ev.getX(); + final int y = (int) ev.getY(); + int motionPosition = pointToPosition(x, y); + if (!mDataChanged) { + if ((mTouchMode != TOUCH_MODE_FLING) && (motionPosition >= 0) + && (getAdapter().isEnabled(motionPosition))) { + // User clicked on an actual view (and was not stopping a fling). + // It might be a click or a scroll. Assume it is a click until + // proven otherwise + mTouchMode = TOUCH_MODE_DOWN; + // FIXME Debounce + if (mPendingCheckForTap == null) { + mPendingCheckForTap = new CheckForTap(); + } + postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); + } else { + if (mTouchMode == TOUCH_MODE_FLING) { + // Stopped a fling. It is a scroll. + createScrollingCache(); + mTouchMode = TOUCH_MODE_SCROLL; + mMotionCorrection = 0; + motionPosition = findMotionRow(y); + mFlingRunnable.flywheelTouch(); + } + } } - final int y = (int) ev.getY(pointerIndex); - if (mDataChanged) { - // Re-sync everything if data has been changed - // since the scroll operation can query the adapter. - layoutChildren(); + if (motionPosition >= 0) { + // Remember where the motion event started + v = getChildAt(motionPosition - mFirstPosition); + mMotionViewOriginalTop = v.getTop(); } + mMotionX = x; + mMotionY = y; + mMotionPosition = motionPosition; + mLastY = Integer.MIN_VALUE; + break; + } + } - switch (mTouchMode) { - case TOUCH_MODE_DOWN: - case TOUCH_MODE_TAP: - case TOUCH_MODE_DONE_WAITING: - // Check if we have moved far enough that it looks more like a - // scroll than a tap - startScrollIfNeeded(y); - break; - case TOUCH_MODE_SCROLL: - case TOUCH_MODE_OVERSCROLL: - scrollIfNeeded(y); - break; + if (performButtonActionOnTouchDown(ev)) { + if (mTouchMode == TOUCH_MODE_DOWN) { + removeCallbacks(mPendingCheckForTap); } + } + } + + private void onTouchMove(MotionEvent ev) { + int pointerIndex = ev.findPointerIndex(mActivePointerId); + if (pointerIndex == -1) { + pointerIndex = 0; + mActivePointerId = ev.getPointerId(pointerIndex); + } + final int y = (int) ev.getY(pointerIndex); + + if (mDataChanged) { + // Re-sync everything if data has been changed + // since the scroll operation can query the adapter. + layoutChildren(); + } + + switch (mTouchMode) { + case TOUCH_MODE_DOWN: + case TOUCH_MODE_TAP: + case TOUCH_MODE_DONE_WAITING: + // Check if we have moved far enough that it looks more like a + // scroll than a tap + startScrollIfNeeded(y); + break; + case TOUCH_MODE_SCROLL: + case TOUCH_MODE_OVERSCROLL: + scrollIfNeeded(y); break; } + } - case MotionEvent.ACTION_UP: { - switch (mTouchMode) { - case TOUCH_MODE_DOWN: - case TOUCH_MODE_TAP: - case TOUCH_MODE_DONE_WAITING: - final int motionPosition = mMotionPosition; - final View child = getChildAt(motionPosition - mFirstPosition); + private void onTouchUp(MotionEvent ev) { + switch (mTouchMode) { + case TOUCH_MODE_DOWN: + case TOUCH_MODE_TAP: + case TOUCH_MODE_DONE_WAITING: + final int motionPosition = mMotionPosition; + final View child = getChildAt(motionPosition - mFirstPosition); - final float x = ev.getX(); - final boolean inList = x > mListPadding.left && x < getWidth() - mListPadding.right; + final float x = ev.getX(); + final boolean inList = x > mListPadding.left && x < getWidth() - mListPadding.right; - if (child != null && !child.hasFocusable() && inList) { - if (mTouchMode != TOUCH_MODE_DOWN) { - child.setPressed(false); - } + if (child != null && !child.hasFocusable() && inList) { + if (mTouchMode != TOUCH_MODE_DOWN) { + child.setPressed(false); + } - if (mPerformClick == null) { - mPerformClick = new PerformClick(); - } + if (mPerformClick == null) { + mPerformClick = new PerformClick(); + } - final AbsListView.PerformClick performClick = mPerformClick; - performClick.mClickMotionPosition = motionPosition; - performClick.rememberWindowAttachCount(); + final AbsListView.PerformClick performClick = mPerformClick; + performClick.mClickMotionPosition = motionPosition; + performClick.rememberWindowAttachCount(); - mResurrectToPosition = motionPosition; + mResurrectToPosition = motionPosition; - if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) { - final Handler handler = getHandler(); - if (handler != null) { - handler.removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ? - mPendingCheckForTap : mPendingCheckForLongPress); + if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) { + final Handler handler = getHandler(); + if (handler != null) { + handler.removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ? + mPendingCheckForTap : mPendingCheckForLongPress); + } + mLayoutMode = LAYOUT_NORMAL; + if (!mDataChanged && mAdapter.isEnabled(motionPosition)) { + mTouchMode = TOUCH_MODE_TAP; + setSelectedPositionInt(mMotionPosition); + layoutChildren(); + child.setPressed(true); + positionSelector(mMotionPosition, child); + setPressed(true); + if (mSelector != null) { + Drawable d = mSelector.getCurrent(); + if (d != null && d instanceof TransitionDrawable) { + ((TransitionDrawable) d).resetTransition(); + } } - mLayoutMode = LAYOUT_NORMAL; - if (!mDataChanged && mAdapter.isEnabled(motionPosition)) { - mTouchMode = TOUCH_MODE_TAP; - setSelectedPositionInt(mMotionPosition); - layoutChildren(); - child.setPressed(true); - positionSelector(mMotionPosition, child); - setPressed(true); - if (mSelector != null) { - Drawable d = mSelector.getCurrent(); - if (d != null && d instanceof TransitionDrawable) { - ((TransitionDrawable) d).resetTransition(); + if (mTouchModeReset != null) { + removeCallbacks(mTouchModeReset); + } + mTouchModeReset = new Runnable() { + @Override + public void run() { + mTouchModeReset = null; + mTouchMode = TOUCH_MODE_REST; + child.setPressed(false); + setPressed(false); + if (!mDataChanged) { + performClick.run(); } } - if (mTouchModeReset != null) { - removeCallbacks(mTouchModeReset); - } - mTouchModeReset = new Runnable() { - @Override - public void run() { - mTouchModeReset = null; - mTouchMode = TOUCH_MODE_REST; - child.setPressed(false); - setPressed(false); - if (!mDataChanged) { - performClick.run(); - } - } - }; - postDelayed(mTouchModeReset, - ViewConfiguration.getPressedStateDuration()); - } else { - mTouchMode = TOUCH_MODE_REST; - updateSelectorState(); - } - return true; - } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) { - performClick.run(); + }; + postDelayed(mTouchModeReset, + ViewConfiguration.getPressedStateDuration()); + } else { + mTouchMode = TOUCH_MODE_REST; + updateSelectorState(); } + return; + } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) { + performClick.run(); } - mTouchMode = TOUCH_MODE_REST; - updateSelectorState(); - break; - case TOUCH_MODE_SCROLL: - final int childCount = getChildCount(); - if (childCount > 0) { - final int firstChildTop = getChildAt(0).getTop(); - final int lastChildBottom = getChildAt(childCount - 1).getBottom(); - final int contentTop = mListPadding.top; - final int contentBottom = getHeight() - mListPadding.bottom; - if (mFirstPosition == 0 && firstChildTop >= contentTop && - mFirstPosition + childCount < mItemCount && - lastChildBottom <= getHeight() - contentBottom) { + } + mTouchMode = TOUCH_MODE_REST; + updateSelectorState(); + break; + case TOUCH_MODE_SCROLL: + final int childCount = getChildCount(); + if (childCount > 0) { + final int firstChildTop = getChildAt(0).getTop(); + final int lastChildBottom = getChildAt(childCount - 1).getBottom(); + final int contentTop = mListPadding.top; + final int contentBottom = getHeight() - mListPadding.bottom; + if (mFirstPosition == 0 && firstChildTop >= contentTop && + mFirstPosition + childCount < mItemCount && + lastChildBottom <= getHeight() - contentBottom) { + mTouchMode = TOUCH_MODE_REST; + reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); + } else { + final VelocityTracker velocityTracker = mVelocityTracker; + velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); + + final int initialVelocity = (int) + (velocityTracker.getYVelocity(mActivePointerId) * mVelocityScale); + // Fling if we have enough velocity and we aren't at a boundary. + // Since we can potentially overfling more than we can overscroll, don't + // allow the weird behavior where you can scroll to a boundary then + // fling further. + if (Math.abs(initialVelocity) > mMinimumVelocity && + !((mFirstPosition == 0 && + firstChildTop == contentTop - mOverscrollDistance) || + (mFirstPosition + childCount == mItemCount && + lastChildBottom == contentBottom + mOverscrollDistance))) { + if (mFlingRunnable == null) { + mFlingRunnable = new FlingRunnable(); + } + reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING); + + mFlingRunnable.start(-initialVelocity); + } else { mTouchMode = TOUCH_MODE_REST; reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); - } else { - final VelocityTracker velocityTracker = mVelocityTracker; - velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); - - final int initialVelocity = (int) - (velocityTracker.getYVelocity(mActivePointerId) * mVelocityScale); - // Fling if we have enough velocity and we aren't at a boundary. - // Since we can potentially overfling more than we can overscroll, don't - // allow the weird behavior where you can scroll to a boundary then - // fling further. - if (Math.abs(initialVelocity) > mMinimumVelocity && - !((mFirstPosition == 0 && - firstChildTop == contentTop - mOverscrollDistance) || - (mFirstPosition + childCount == mItemCount && - lastChildBottom == contentBottom + mOverscrollDistance))) { - if (mFlingRunnable == null) { - mFlingRunnable = new FlingRunnable(); - } - reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING); - - mFlingRunnable.start(-initialVelocity); - } else { - mTouchMode = TOUCH_MODE_REST; - reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); - if (mFlingRunnable != null) { - mFlingRunnable.endFling(); - } - if (mPositionScroller != null) { - mPositionScroller.stop(); - } + if (mFlingRunnable != null) { + mFlingRunnable.endFling(); + } + if (mPositionScroller != null) { + mPositionScroller.stop(); } } - } else { - mTouchMode = TOUCH_MODE_REST; - reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); - } - break; - - case TOUCH_MODE_OVERSCROLL: - if (mFlingRunnable == null) { - mFlingRunnable = new FlingRunnable(); - } - final VelocityTracker velocityTracker = mVelocityTracker; - velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); - final int initialVelocity = (int) velocityTracker.getYVelocity(mActivePointerId); - - reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING); - if (Math.abs(initialVelocity) > mMinimumVelocity) { - mFlingRunnable.startOverfling(-initialVelocity); - } else { - mFlingRunnable.startSpringback(); } - - break; + } else { + mTouchMode = TOUCH_MODE_REST; + reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); } + break; - setPressed(false); - - if (mEdgeGlowTop != null) { - mEdgeGlowTop.onRelease(); - mEdgeGlowBottom.onRelease(); + case TOUCH_MODE_OVERSCROLL: + if (mFlingRunnable == null) { + mFlingRunnable = new FlingRunnable(); } + final VelocityTracker velocityTracker = mVelocityTracker; + velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); + final int initialVelocity = (int) velocityTracker.getYVelocity(mActivePointerId); - // Need to redraw since we probably aren't drawing the selector anymore - invalidate(); - - final Handler handler = getHandler(); - if (handler != null) { - handler.removeCallbacks(mPendingCheckForLongPress); + reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING); + if (Math.abs(initialVelocity) > mMinimumVelocity) { + mFlingRunnable.startOverfling(-initialVelocity); + } else { + mFlingRunnable.startSpringback(); } - recycleVelocityTracker(); + break; + } - mActivePointerId = INVALID_POINTER; + setPressed(false); - if (PROFILE_SCROLLING) { - if (mScrollProfilingStarted) { - Debug.stopMethodTracing(); - mScrollProfilingStarted = false; - } - } - - if (mScrollStrictSpan != null) { - mScrollStrictSpan.finish(); - mScrollStrictSpan = null; - } - break; + if (mEdgeGlowTop != null) { + mEdgeGlowTop.onRelease(); + mEdgeGlowBottom.onRelease(); } - case MotionEvent.ACTION_CANCEL: { - switch (mTouchMode) { - case TOUCH_MODE_OVERSCROLL: - if (mFlingRunnable == null) { - mFlingRunnable = new FlingRunnable(); - } - mFlingRunnable.startSpringback(); - break; + // Need to redraw since we probably aren't drawing the selector anymore + invalidate(); - case TOUCH_MODE_OVERFLING: - // Do nothing - let it play out. - break; + final Handler handler = getHandler(); + if (handler != null) { + handler.removeCallbacks(mPendingCheckForLongPress); + } - default: - mTouchMode = TOUCH_MODE_REST; - setPressed(false); - View motionView = this.getChildAt(mMotionPosition - mFirstPosition); - if (motionView != null) { - motionView.setPressed(false); - } - clearScrollingCache(); + recycleVelocityTracker(); - final Handler handler = getHandler(); - if (handler != null) { - handler.removeCallbacks(mPendingCheckForLongPress); - } + mActivePointerId = INVALID_POINTER; - recycleVelocityTracker(); + if (PROFILE_SCROLLING) { + if (mScrollProfilingStarted) { + Debug.stopMethodTracing(); + mScrollProfilingStarted = false; } + } - if (mEdgeGlowTop != null) { - mEdgeGlowTop.onRelease(); - mEdgeGlowBottom.onRelease(); - } - mActivePointerId = INVALID_POINTER; - break; + if (mScrollStrictSpan != null) { + mScrollStrictSpan.finish(); + mScrollStrictSpan = null; } + } - case MotionEvent.ACTION_POINTER_UP: { - onSecondaryPointerUp(ev); - final int x = mMotionX; - final int y = mMotionY; - final int motionPosition = pointToPosition(x, y); - if (motionPosition >= 0) { - // Remember where the motion event started - v = getChildAt(motionPosition - mFirstPosition); - mMotionViewOriginalTop = v.getTop(); - mMotionPosition = motionPosition; + private void onTouchCancel() { + switch (mTouchMode) { + case TOUCH_MODE_OVERSCROLL: + if (mFlingRunnable == null) { + mFlingRunnable = new FlingRunnable(); } - mLastY = y; + mFlingRunnable.startSpringback(); break; - } - case MotionEvent.ACTION_POINTER_DOWN: { - // New pointers take over dragging duties - final int index = ev.getActionIndex(); - final int id = ev.getPointerId(index); - final int x = (int) ev.getX(index); - final int y = (int) ev.getY(index); - mMotionCorrection = 0; - mActivePointerId = id; - mMotionX = x; - mMotionY = y; - final int motionPosition = pointToPosition(x, y); - if (motionPosition >= 0) { - // Remember where the motion event started - v = getChildAt(motionPosition - mFirstPosition); - mMotionViewOriginalTop = v.getTop(); - mMotionPosition = motionPosition; - } - mLastY = y; + case TOUCH_MODE_OVERFLING: + // Do nothing - let it play out. break; - } + + default: + mTouchMode = TOUCH_MODE_REST; + setPressed(false); + View motionView = this.getChildAt(mMotionPosition - mFirstPosition); + if (motionView != null) { + motionView.setPressed(false); + } + clearScrollingCache(); + + final Handler handler = getHandler(); + if (handler != null) { + handler.removeCallbacks(mPendingCheckForLongPress); + } + + recycleVelocityTracker(); } - return true; + if (mEdgeGlowTop != null) { + mEdgeGlowTop.onRelease(); + mEdgeGlowBottom.onRelease(); + } + mActivePointerId = INVALID_POINTER; } @Override |