Prevent ListView from scrolling/flinging its content when the content fits on screen.

Change-Id: Id46313bca3ae53b0717cd74a4a6f889a01f9fcb1
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 425ccab..d569220 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -46,7 +46,6 @@
 import android.os.SystemProperties;
 import android.util.AttributeSet;
 import android.util.Config;
-import android.util.DisplayMetrics;
 import android.util.EventLog;
 import android.util.Log;
 import android.util.Pool;
diff --git a/core/java/android/widget/AbsListView.java b/core/java/android/widget/AbsListView.java
index 67721c9..2f292d5 100644
--- a/core/java/android/widget/AbsListView.java
+++ b/core/java/android/widget/AbsListView.java
@@ -1996,7 +1996,10 @@
                 if (y != mLastY) {
                     deltaY -= mMotionCorrection;
                     int incrementalDeltaY = mLastY != Integer.MIN_VALUE ? y - mLastY : deltaY;
-                    trackMotionScroll(deltaY, incrementalDeltaY);
+                    // No need to do all this work if we're not going to move anyway
+                    if (incrementalDeltaY != 0) {
+                        trackMotionScroll(deltaY, incrementalDeltaY);
+                    }
 
                     // Check to see if we have bumped into the scroll limit
                     View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
@@ -2063,7 +2066,7 @@
                             if (mSelector != null) {
                                 Drawable d = mSelector.getCurrent();
                                 if (d != null && d instanceof TransitionDrawable) {
-                                    ((TransitionDrawable)d).resetTransition();
+                                    ((TransitionDrawable) d).resetTransition();
                                 }
                             }
                             postDelayed(new Runnable() {
@@ -2087,15 +2090,27 @@
                 mTouchMode = TOUCH_MODE_REST;
                 break;
             case TOUCH_MODE_SCROLL:
-                final VelocityTracker velocityTracker = mVelocityTracker;
-                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
-                final int initialVelocity = (int) velocityTracker.getYVelocity();
-                if (Math.abs(initialVelocity) > mMinimumVelocity && (getChildCount() > 0)) {
-                    if (mFlingRunnable == null) {
-                        mFlingRunnable = new FlingRunnable();
+                final int childCount = getChildCount();
+                if (childCount > 0) {
+                    if (mFirstPosition == 0 && getChildAt(0).getTop() >= mListPadding.top &&
+                            mFirstPosition + childCount < mItemCount &&
+                            getChildAt(childCount - 1).getBottom() <=
+                                    getHeight() - mListPadding.bottom) {
+                        mTouchMode = TOUCH_MODE_REST;
+                        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
+                    } else {
+                        final VelocityTracker velocityTracker = mVelocityTracker;
+                        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
+                        final int initialVelocity = (int) velocityTracker.getYVelocity();
+    
+                        if (Math.abs(initialVelocity) > mMinimumVelocity) {
+                            if (mFlingRunnable == null) {
+                                mFlingRunnable = new FlingRunnable();
+                            }
+                            reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
+                            mFlingRunnable.start(-initialVelocity);
+                        }
                     }
-                    reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
-                    mFlingRunnable.start(-initialVelocity);
                 } else {
                     mTouchMode = TOUCH_MODE_REST;
                     reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
diff --git a/core/java/android/widget/ListView.java b/core/java/android/widget/ListView.java
index 993b7cb..6316864 100644
--- a/core/java/android/widget/ListView.java
+++ b/core/java/android/widget/ListView.java
@@ -1328,19 +1328,23 @@
 
             // Make sure we are 1) Too low, and 2) Either there are more rows below the
             // last row or the last row is scrolled off the bottom of the drawable area
-            if (topOffset > 0 && (lastPosition < mItemCount - 1 || lastBottom > end))  {
-                if (lastPosition == mItemCount - 1 ) {
-                    // Don't pull the bottom too far up
-                    topOffset = Math.min(topOffset, lastBottom - end);
-                }
-                // Move everything up
-                offsetChildrenTopAndBottom(-topOffset);
-                if (lastPosition < mItemCount - 1) {
-                    // Fill the gap that was opened below the last position with more rows, if
-                    // possible
-                    fillDown(lastPosition + 1, lastChild.getBottom() + mDividerHeight);
-                    // Close up the remaining gap
-                    adjustViewsUpOrDown();
+            if (topOffset > 0) {
+                if (lastPosition < mItemCount - 1 || lastBottom > end)  {
+                    if (lastPosition == mItemCount - 1) {
+                        // Don't pull the bottom too far up
+                        topOffset = Math.min(topOffset, lastBottom - end);
+                    }
+                    // Move everything up
+                    offsetChildrenTopAndBottom(-topOffset);
+                    if (lastPosition < mItemCount - 1) {
+                        // Fill the gap that was opened below the last position with more rows, if
+                        // possible
+                        fillDown(lastPosition + 1, lastChild.getBottom() + mDividerHeight);
+                        // Close up the remaining gap
+                        adjustViewsUpOrDown();
+                    }
+                } else if (lastPosition == mItemCount - 1) {
+                    adjustViewsUpOrDown();                    
                 }
             }
         }