From ed88cee15b4f2245d4d191cf007f2b78d07788b9 Mon Sep 17 00:00:00 2001 From: Selim Cinek Date: Fri, 10 Jan 2014 17:28:15 -0800 Subject: [PATCH] Fixed issue on Keyguard camera icon, where dragging on it did not properly trigger abortion of the camera animation. Also minorly simplified the touch slop calculation BUG: 12036618 Change-Id: If01b48c815200066b32a37ffc44949edde1376f9 --- .../com/android/keyguard/KeyguardWidgetPager.java | 4 - .../src/com/android/keyguard/PagedView.java | 125 +++++++++++++-------- 2 files changed, 76 insertions(+), 53 deletions(-) diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardWidgetPager.java b/packages/Keyguard/src/com/android/keyguard/KeyguardWidgetPager.java index 99f775787cec..6120127683b6 100644 --- a/packages/Keyguard/src/com/android/keyguard/KeyguardWidgetPager.java +++ b/packages/Keyguard/src/com/android/keyguard/KeyguardWidgetPager.java @@ -953,17 +953,13 @@ public class KeyguardWidgetPager extends PagedView implements PagedView.PageSwit // to keep event dispatch happy. mCameraEventInProgress = true; userActivity(); - startPageWarp(cameraPage); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mCameraEventInProgress = false; - endWarp = isWarping(); break; } dispatchTouchEvent(event); - // This has to happen after the event has been handled by the real widget pager - if (endWarp) stopPageWarp(); } endCameraEvent(); } diff --git a/packages/Keyguard/src/com/android/keyguard/PagedView.java b/packages/Keyguard/src/com/android/keyguard/PagedView.java index 53c17a5e7adc..d233ee938a1d 100644 --- a/packages/Keyguard/src/com/android/keyguard/PagedView.java +++ b/packages/Keyguard/src/com/android/keyguard/PagedView.java @@ -36,15 +36,7 @@ import android.os.Parcelable; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.Log; -import android.view.InputDevice; -import android.view.KeyEvent; -import android.view.MotionEvent; -import android.view.VelocityTracker; -import android.view.View; -import android.view.ViewConfiguration; -import android.view.ViewGroup; -import android.view.ViewParent; -import android.view.ViewPropertyAnimator; +import android.view.*; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityManager; import android.view.accessibility.AccessibilityNodeInfo; @@ -262,6 +254,7 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc // Page warping private int mPageSwapIndex = -1; // the page we swapped out if needed private int mPageWarpIndex = -1; // the page we intend to warp + private boolean mWarpPageExposed; private ViewPropertyAnimator mWarpAnimation; @@ -494,9 +487,6 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc mIsPageMoving = true; if (isWarping()) { dispatchOnPageBeginWarp(); - if (mPageSwapIndex != -1) { - swapPages(mPageSwapIndex, mPageWarpIndex); - } } onPageBeginMoving(); } @@ -523,22 +513,13 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc if (mIsPageMoving) { mIsPageMoving = false; if (isWarping()) { - if (mPageSwapIndex != -1) { - swapPages(mPageSwapIndex, mPageWarpIndex); - } dispatchOnPageEndWarp(); - resetPageWarp(); + mWarpPageExposed = false; } onPageEndMoving(); } } - private void resetPageWarp() { - // TODO: Verify pages have been reset correctly - mPageSwapIndex = -1; - mPageWarpIndex = -1; - } - protected boolean isPageMoving() { return mIsPageMoving; } @@ -794,11 +775,6 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc setHorizontalScrollBarEnabled(true); mFirstLayout = false; } - // If a page was swapped when we rebuilt the layout, swap it again now. - if (mPageSwapIndex != -1) { - if (DEBUG_WARP) Log.v(TAG, "onLayout: swapping pages"); - swapPages(mPageSwapIndex, mPageWarpIndex); - } } protected void screenScrolled(int screenCenter) { @@ -1252,6 +1228,25 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc } } + private boolean isHorizontalCameraScroll(MotionEvent ev) { + // Disallow scrolling if we don't have a valid pointer index + final int pointerIndex = ev.findPointerIndex(mActivePointerId); + if (pointerIndex == -1) return false; + + // If we're only allowing edge swipes, we break out early if the down event wasn't + // at the edge. + if (mOnlyAllowEdgeSwipes && !mDownEventOnEdge) return false; + + final float x = ev.getX(pointerIndex); + final int xDiff = (int) Math.abs(x - mDownMotionX); + + final int touchSlop = Math.round(TOUCH_SLOP_SCALE * mTouchSlop); + boolean xPaged = xDiff > mPagingTouchSlop; + boolean xMoved = xDiff > touchSlop; + + return mIsCameraEvent && (mUsePagingTouchSlop ? xPaged : xMoved); + } + /* * Determines if we should change the touch state to start scrolling after the * user moves their touch point too far. @@ -1271,14 +1266,12 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc if (mOnlyAllowEdgeSwipes && !mDownEventOnEdge) return false; final int xDiff = (int) Math.abs(x - mLastMotionX); - final int yDiff = (int) Math.abs(y - mLastMotionY); final int touchSlop = Math.round(TOUCH_SLOP_SCALE * mTouchSlop); boolean xPaged = xDiff > mPagingTouchSlop; boolean xMoved = xDiff > touchSlop; - boolean yMoved = yDiff > touchSlop; - return (xMoved || xPaged || yMoved) && (mUsePagingTouchSlop ? xPaged : xMoved); + return mUsePagingTouchSlop ? xPaged : xMoved; } private void startScrolling(MotionEvent ev) { @@ -1446,7 +1439,7 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc mTouchX += deltaX; mSmoothingTime = System.nanoTime() / NANOTIME_DIV; if (isWarping()) { - KeyguardWidgetFrame v = (KeyguardWidgetFrame) getPageAt(mPageWarpIndex); + KeyguardWidgetFrame v = (KeyguardWidgetFrame) getPageAt(getPageWarpIndex()); v.setTranslationX(v.getTranslationX() - deltaX); } else if (!mDeferScrollUpdate) { scrollBy((int) deltaX, 0); @@ -1565,8 +1558,13 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc removeCallbacks(mSidePageHoverRunnable); mSidePageHoverIndex = -1; } - } else if (mIsCameraEvent || determineScrollingStart(ev)) { + } else if (determineScrollingStart(ev)) { + startScrolling(ev); + } else if (isHorizontalCameraScroll(ev)) { startScrolling(ev); + // we need to cancel the camera animation + KeyguardWidgetFrame v = (KeyguardWidgetFrame) getPageAt(getPageWarpIndex()); + v.animate().cancel(); } break; @@ -1612,7 +1610,8 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc } else if (((isSignificantMove && deltaX < 0 && !isFling) || (isFling && velocityX < 0)) && mCurrentPage < getChildCount() - 1) { - finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; + finalPage = returnToOriginalPage ? mCurrentPage : + isWarping() ? getPageWarpIndex() : mCurrentPage + 1; snapToPageWithVelocity(finalPage, velocityX); } else { snapToDestination(); @@ -1929,20 +1928,19 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc } protected void snapToPage(int whichPage, int delta, int duration, boolean immediate) { - if (mPageSwapIndex != -1 && whichPage == mPageSwapIndex) { - mNextPage = mPageWarpIndex; // jump to the warp page + if (isWarping() && whichPage == mCurrentPage+1) { + mNextPage = getPageWarpIndex(); // jump to the warp page if (DEBUG_WARP) Log.v(TAG, "snapToPage(" + whichPage + ") : reset mPageSwapIndex"); } else { mNextPage = whichPage; } - if (isWarping()) { + if(mWarpPageExposed) { dispatchOnPageEndWarp(); - notifyPageSwitching(whichPage); - resetPageWarp(); - } else { - notifyPageSwitching(whichPage); + mWarpPageExposed = false; } + notifyPageSwitching(whichPage); + View focusedChild = getFocusedChild(); if (focusedChild != null && whichPage != mCurrentPage && @@ -1973,7 +1971,7 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc } protected boolean isWarping() { - return mPageWarpIndex != -1; + return mWarpPageExposed; } public void scrollLeft() { @@ -2716,6 +2714,12 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc animateWarpPageOffScreen("onScreen end", true); } } + + @Override + public void onAnimationCancel(Animator animation) { + super.onAnimationCancel(animation); + mWarpAnimation = null; + } }; AnimatorListenerAdapter mOffScreenAnimationListener = new AnimatorListenerAdapter() { @@ -2723,6 +2727,14 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc public void onAnimationEnd(Animator animation) { mWarpAnimation = null; mWarpPageExposed = false; + KeyguardWidgetFrame v = (KeyguardWidgetFrame) getPageAt(getPageWarpIndex()); + v.setTranslationX(0.0f); + } + + @Override + public void onAnimationCancel(Animator animation) { + super.onAnimationCancel(animation); + mWarpAnimation = null; } }; @@ -2731,7 +2743,7 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc if (abortAnimation) { // We're done with the animation and moving to a new page. Let the scroller // take over the animation. - KeyguardWidgetFrame v = (KeyguardWidgetFrame) getPageAt(mPageWarpIndex); + KeyguardWidgetFrame v = (KeyguardWidgetFrame) getPageAt(getPageWarpIndex()); v.animate().cancel(); // Make the scroll amount match the current warp position. scrollBy(Math.round(-v.getTranslationX()), 0); @@ -2747,28 +2759,43 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc private void animateWarpPageOnScreen(String reason) { if (DEBUG_WARP) Log.v(TAG, "animateWarpPageOnScreen(" + reason + ")"); - if (isWarping() && !mWarpPageExposed) { + if (!mWarpPageExposed) { mWarpPageExposed = true; dispatchOnPageBeginWarp(); - KeyguardWidgetFrame v = (KeyguardWidgetFrame) getPageAt(mPageWarpIndex); + KeyguardWidgetFrame v = (KeyguardWidgetFrame) getPageAt(getPageWarpIndex()); if (DEBUG_WARP) Log.v(TAG, "moving page on screen: Tx=" + v.getTranslationX()); DecelerateInterpolator interp = new DecelerateInterpolator(1.5f); + int totalOffset = getCurrentWarpOffset(); + v.setTranslationX(totalOffset); mWarpAnimation = v.animate(); - mWarpAnimation.translationX(mWarpPeekAmount) + mWarpAnimation.translationX(mWarpPeekAmount+totalOffset) .setInterpolator(interp) .setDuration(WARP_PEEK_ANIMATION_DURATION) .setListener(mOnScreenAnimationListener); } } + private int getCurrentWarpOffset() { + if (mCurrentPage == getPageWarpIndex()) { + return 0; + } + View viewRight = getPageAt(mCurrentPage + 1); + View warpView = getPageAt(getPageWarpIndex()); + if (viewRight != warpView && viewRight != null && warpView != null) { + return viewRight.getLeft() - warpView.getLeft(); + } + return 0; + } + private void animateWarpPageOffScreen(String reason, boolean animate) { if (DEBUG_WARP) Log.v(TAG, "animateWarpPageOffScreen(" + reason + " anim:" + animate + ")"); - if (isWarping()) { + if (mWarpPageExposed) { dispatchOnPageEndWarp(); - KeyguardWidgetFrame v = (KeyguardWidgetFrame) getPageAt(mPageWarpIndex); + KeyguardWidgetFrame v = (KeyguardWidgetFrame) getPageAt(getPageWarpIndex()); if (DEBUG_WARP) Log.v(TAG, "moving page off screen: Tx=" + v.getTranslationX()); AccelerateInterpolator interp = new AccelerateInterpolator(1.5f); - v.animate().translationX(0.0f) + int totalOffset = getCurrentWarpOffset(); + v.animate().translationX(totalOffset) .setInterpolator(interp) .setDuration(animate ? WARP_PEEK_ANIMATION_DURATION : 0) .setListener(mOffScreenAnimationListener); @@ -2799,7 +2826,7 @@ public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarc } protected int getPageWarpIndex() { - return mPageWarpIndex; + return getPageCount() - 1; } public void stopPageWarp() { -- 2.11.0