Code clean-up around ScrollView.

Change-Id: Ia110f30279a93c96741e9afbd93ed6231aa97ef1
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index e00b9cd..ffd6de2 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -58,6 +58,7 @@
 import android.util.Pools;
 import android.util.SparseArray;
 import android.view.ContextMenu.ContextMenuInfo;
+import android.view.View.MeasureSpec;
 import android.view.accessibility.AccessibilityEvent;
 import android.view.accessibility.AccessibilityEventSource;
 import android.view.accessibility.AccessibilityManager;
@@ -1771,12 +1772,10 @@
      */
     public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
 
-
     /**
      * @hide
      */
     public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
-    
 
     /**
      * Controls the over-scroll mode for this view.
@@ -3180,8 +3179,8 @@
      * gives it focus no matter what.  It should only be called internally by framework
      * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
      *
-     * @param direction values are View.FOCUS_UP, View.FOCUS_DOWN,
-     *        View.FOCUS_LEFT or View.FOCUS_RIGHT. This is the direction which
+     * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
+     *        {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
      *        focus moved when requestFocus() is called. It may not always
      *        apply, in which case use the default View.FOCUS_DOWN.
      * @param previouslyFocusedRect The rectangle of the view that had focus
@@ -4332,9 +4331,9 @@
      * Call this to try to give focus to a specific view or to one of its
      * descendants.
      *
-     * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
-     * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
-     * while the device is in touch mode.
+     * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
+     * false), or if it is focusable and it is not focusable in touch mode
+     * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
      *
      * See also {@link #focusSearch}, which is what you call to say that you
      * have focus, and you want your parent to look for the next one.
@@ -4353,9 +4352,9 @@
      * Call this to try to give focus to a specific view or to one of its
      * descendants and give it a hint about what direction focus is heading.
      *
-     * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
-     * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
-     * while the device is in touch mode.
+     * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
+     * false), or if it is focusable and it is not focusable in touch mode
+     * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
      *
      * See also {@link #focusSearch}, which is what you call to say that you
      * have focus, and you want your parent to look for the next one.
@@ -4377,14 +4376,15 @@
      * about where focus is coming from, and therefore, where to show selection, or
      * forward focus change internally.
      *
-     * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
-     * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
-     * while the device is in touch mode.
+     * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
+     * false), or if it is focusable and it is not focusable in touch mode
+     * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
      *
      * A View will not take focus if it is not visible.
      *
-     * A View will not take focus if one of its parents has {@link android.view.ViewGroup#getDescendantFocusability()}
-     * equal to {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
+     * A View will not take focus if one of its parents has
+     * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
+     * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
      *
      * See also {@link #focusSearch}, which is what you call to say that you
      * have focus, and you want your parent to look for the next one.
@@ -6453,7 +6453,7 @@
         mTranslationY = y - mTop;
         mMatrixDirty = true;
     }
-    
+
     /**
      * @hide
      */
@@ -6476,7 +6476,7 @@
     public void setFastAlpha(float alpha) {
         mAlpha = alpha;
     }
-    
+
     /**
      * @hide
      */
@@ -6484,7 +6484,7 @@
         mRotationY = y;
         mMatrixDirty = true;
     }
-    
+
     /**
      * Hit rectangle in parent's coordinates
      *
@@ -8662,7 +8662,7 @@
     public void buildDrawingCache() {
         buildDrawingCache(false);
     }
-    
+
     /**
      * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
      *
diff --git a/core/java/android/widget/HorizontalScrollView.java b/core/java/android/widget/HorizontalScrollView.java
index d75748f..e26e99b 100644
--- a/core/java/android/widget/HorizontalScrollView.java
+++ b/core/java/android/widget/HorizontalScrollView.java
@@ -16,25 +16,25 @@
 
 package android.widget;
 
-import android.view.ViewDebug;
 import com.android.internal.R;
 
-import android.util.AttributeSet;
-import android.graphics.Canvas;
-import android.graphics.Rect;
-import android.graphics.drawable.Drawable;
-import android.view.View;
-import android.view.VelocityTracker;
-import android.view.ViewConfiguration;
-import android.view.ViewGroup;
-import android.view.KeyEvent;
-import android.view.FocusFinder;
-import android.view.MotionEvent;
-import android.view.ViewParent;
-import android.view.animation.AnimationUtils;
 import android.content.Context;
 import android.content.res.Resources;
 import android.content.res.TypedArray;
+import android.graphics.Canvas;
+import android.graphics.Rect;
+import android.graphics.drawable.Drawable;
+import android.util.AttributeSet;
+import android.view.FocusFinder;
+import android.view.KeyEvent;
+import android.view.MotionEvent;
+import android.view.VelocityTracker;
+import android.view.View;
+import android.view.ViewConfiguration;
+import android.view.ViewDebug;
+import android.view.ViewGroup;
+import android.view.ViewParent;
+import android.view.animation.AnimationUtils;
 
 import java.util.List;
 
@@ -76,13 +76,6 @@
     private EdgeGlow mEdgeGlowRight;
 
     /**
-     * Flag to indicate that we are moving focus ourselves. This is so the
-     * code that watches for focus changes initiated outside this ScrollView
-     * knows that it does not have to do anything.
-     */
-    private boolean mScrollViewMovedFocus;
-
-    /**
      * Position of the last motion event.
      */
     private float mLastMotionX;
@@ -885,10 +878,7 @@
             doScrollX(delta);
         }
 
-        if (newFocused != findFocus() && newFocused.requestFocus(direction)) {
-            mScrollViewMovedFocus = true;
-            mScrollViewMovedFocus = false;
-        }
+        if (newFocused != findFocus()) newFocused.requestFocus(direction);
 
         return handled;
     }
@@ -1239,13 +1229,11 @@
 
     @Override
     public void requestChildFocus(View child, View focused) {
-        if (!mScrollViewMovedFocus) {
-            if (!mIsLayoutDirty) {
-                scrollToChild(focused);
-            } else {
-                // The child may not be laid out yet, we can't compute the scroll yet
-                mChildToScrollTo = focused;
-            }
+        if (!mIsLayoutDirty) {
+            scrollToChild(focused);
+        } else {
+            // The child may not be laid out yet, we can't compute the scroll yet
+            mChildToScrollTo = focused;
         }
         super.requestChildFocus(child, focused);
     }
@@ -1363,17 +1351,16 @@
 
             final boolean movingRight = velocityX > 0;
 
+            View currentFocused = findFocus();
             View newFocused = findFocusableViewInMyBounds(movingRight,
-                    mScroller.getFinalX(), findFocus());
+                    mScroller.getFinalX(), currentFocused);
 
             if (newFocused == null) {
                 newFocused = this;
             }
 
-            if (newFocused != findFocus()
-                    && newFocused.requestFocus(movingRight ? View.FOCUS_RIGHT : View.FOCUS_LEFT)) {
-                mScrollViewMovedFocus = true;
-                mScrollViewMovedFocus = false;
+            if (newFocused != currentFocused) {
+                newFocused.requestFocus(movingRight ? View.FOCUS_RIGHT : View.FOCUS_LEFT);
             }
 
             invalidate();
@@ -1385,6 +1372,7 @@
      *
      * <p>This version also clamps the scrolling to the bounds of our child.
      */
+    @Override
     public void scrollTo(int x, int y) {
         // we rely on the fact the View.scrollBy calls scrollTo.
         if (getChildCount() > 0) {
diff --git a/core/java/android/widget/ScrollView.java b/core/java/android/widget/ScrollView.java
index 4cc4a27..9932320 100644
--- a/core/java/android/widget/ScrollView.java
+++ b/core/java/android/widget/ScrollView.java
@@ -16,7 +16,6 @@
 
 package android.widget;
 
-import android.view.ViewDebug;
 import com.android.internal.R;
 
 import android.content.Context;
@@ -33,6 +32,7 @@
 import android.view.VelocityTracker;
 import android.view.View;
 import android.view.ViewConfiguration;
+import android.view.ViewDebug;
 import android.view.ViewGroup;
 import android.view.ViewParent;
 import android.view.animation.AnimationUtils;
@@ -71,13 +71,6 @@
     private EdgeGlow mEdgeGlowBottom;
 
     /**
-     * Flag to indicate that we are moving focus ourselves. This is so the
-     * code that watches for focus changes initiated outside this ScrollView
-     * knows that it does not have to do anything.
-     */
-    private boolean mScrollViewMovedFocus;
-
-    /**
      * Position of the last motion event.
      */
     private float mLastMotionY;
@@ -671,15 +664,13 @@
      * the parameter top.
      * </p>
      *
-     * @param topFocus           look for a candidate is the one at the top of the bounds
-     *                           if topFocus is true, or at the bottom of the bounds if topFocus is
-     *                           false
+     * @param topFocus           look for a candidate at the top of the bounds if topFocus is true,
+     *                           or at the bottom of the bounds if topFocus is false
      * @param top                the top offset of the bounds in which a focusable must be
      *                           found (the fading edge is assumed to start at this position)
      * @param preferredFocusable the View that has highest priority and will be
      *                           returned if it is within my bounds (null is valid)
-     * @return the next focusable component in the bounds or null if none can be
-     *         found
+     * @return the next focusable component in the bounds or null if none can be found
      */
     private View findFocusableViewInMyBounds(final boolean topFocus,
             final int top, View preferredFocusable) {
@@ -856,11 +847,10 @@
      * <p>Scrolls the view to make the area defined by <code>top</code> and
      * <code>bottom</code> visible. This method attempts to give the focus
      * to a component visible in this area. If no component can be focused in
-     * the new visible area, the focus is reclaimed by this scrollview.</p>
+     * the new visible area, the focus is reclaimed by this ScrollView.</p>
      *
      * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
-     *                  to go upward
-     *                  {@link android.view.View#FOCUS_DOWN} to downward
+     *                  to go upward, {@link android.view.View#FOCUS_DOWN} to downward
      * @param top       the top offset of the new area to be made visible
      * @param bottom    the bottom offset of the new area to be made visible
      * @return true if the key event is consumed by this method, false otherwise
@@ -885,10 +875,7 @@
             doScrollY(delta);
         }
 
-        if (newFocused != findFocus() && newFocused.requestFocus(direction)) {
-            mScrollViewMovedFocus = true;
-            mScrollViewMovedFocus = false;
-        }
+        if (newFocused != findFocus()) newFocused.requestFocus(direction);
 
         return handled;
     }
@@ -1249,13 +1236,11 @@
 
     @Override
     public void requestChildFocus(View child, View focused) {
-        if (!mScrollViewMovedFocus) {
-            if (!mIsLayoutDirty) {
-                scrollToChild(focused);
-            } else {
-                // The child may not be laid out yet, we can't compute the scroll yet
-                mChildToScrollTo = focused;
-            }
+        if (!mIsLayoutDirty) {
+            scrollToChild(focused);
+        } else {
+            // The child may not be laid out yet, we can't compute the scroll yet
+            mChildToScrollTo = focused;
         }
         super.requestChildFocus(child, focused);
     }
@@ -1388,16 +1373,15 @@
 
             final boolean movingDown = velocityY > 0;
 
+            View currentFocused = findFocus();
             View newFocused =
-                    findFocusableViewInMyBounds(movingDown, mScroller.getFinalY(), findFocus());
+                    findFocusableViewInMyBounds(movingDown, mScroller.getFinalY(), currentFocused);
             if (newFocused == null) {
                 newFocused = this;
             }
 
-            if (newFocused != findFocus()
-                    && newFocused.requestFocus(movingDown ? View.FOCUS_DOWN : View.FOCUS_UP)) {
-                mScrollViewMovedFocus = true;
-                mScrollViewMovedFocus = false;
+            if (newFocused != currentFocused) {
+                    newFocused.requestFocus(movingDown ? View.FOCUS_DOWN : View.FOCUS_UP);
             }
 
             if (mFlingStrictSpan == null) {