Fix scrolling bug in AutoCompleteTextView.
Bug #2495033

This fixes various issues. ACTV would sometimes not update its popup to match
its size/location.

Change-Id: Ic662bddf40e49b09482b15ff91666be3709da1d5
diff --git a/core/java/android/widget/AutoCompleteTextView.java b/core/java/android/widget/AutoCompleteTextView.java
index 0f47b96..ed63787 100644
--- a/core/java/android/widget/AutoCompleteTextView.java
+++ b/core/java/android/widget/AutoCompleteTextView.java
@@ -1081,11 +1081,11 @@
     }
 
     @Override
-    protected boolean setFrame(int l, int t, int r, int b) {
+    protected boolean setFrame(final int l, int t, final int r, int b) {
         boolean result = super.setFrame(l, t, r, b);
 
         if (mPopup.isShowing()) {
-            mPopup.update(this, r - l, -1);
+            showDropDown();
         }
 
         return result;
diff --git a/core/java/android/widget/PopupWindow.java b/core/java/android/widget/PopupWindow.java
index d20ab3b..cf2ed86 100644
--- a/core/java/android/widget/PopupWindow.java
+++ b/core/java/android/widget/PopupWindow.java
@@ -126,7 +126,7 @@
                     WindowManager.LayoutParams p = (WindowManager.LayoutParams)
                             mPopupView.getLayoutParams();
 
-                    mAboveAnchor = findDropDownPosition(anchor, p, mAnchorXoff, mAnchorYoff);
+                    updateAboveAnchor(findDropDownPosition(anchor, p, mAnchorXoff, mAnchorYoff));
                     update(p.x, p.y, -1, -1, true);
                 }
             }
@@ -729,22 +729,8 @@
 
         WindowManager.LayoutParams p = createPopupLayout(anchor.getWindowToken());
         preparePopup(p);
-        mAboveAnchor = findDropDownPosition(anchor, p, xoff, yoff);
 
-        if (mBackground != null) {
-            // If the background drawable provided was a StateListDrawable with above-anchor
-            // and below-anchor states, use those. Otherwise rely on refreshDrawableState to
-            // do the job.
-            if (mAboveAnchorBackgroundDrawable != null) {
-                if (mAboveAnchor) {
-                    mPopupView.setBackgroundDrawable(mAboveAnchorBackgroundDrawable);
-                } else {
-                    mPopupView.setBackgroundDrawable(mBelowAnchorBackgroundDrawable);
-                }
-            } else {
-                mPopupView.refreshDrawableState();
-            }
-        }
+        updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff));
 
         if (mHeightMode < 0) p.height = mLastHeight = mHeightMode;
         if (mWidthMode < 0) p.width = mLastWidth = mWidthMode;
@@ -754,6 +740,27 @@
         invokePopup(p);
     }
 
+    private void updateAboveAnchor(boolean aboveAnchor) {
+        if (aboveAnchor != mAboveAnchor) {
+            mAboveAnchor = aboveAnchor;
+
+            if (mBackground != null) {
+                // If the background drawable provided was a StateListDrawable with above-anchor
+                // and below-anchor states, use those. Otherwise rely on refreshDrawableState to
+                // do the job.
+                if (mAboveAnchorBackgroundDrawable != null) {
+                    if (mAboveAnchor) {
+                        mPopupView.setBackgroundDrawable(mAboveAnchorBackgroundDrawable);
+                    } else {
+                        mPopupView.setBackgroundDrawable(mBelowAnchorBackgroundDrawable);
+                    }
+                } else {
+                    mPopupView.refreshDrawableState();
+                }
+            }
+        }
+    }
+
     /**
      * Indicates whether the popup is showing above (the y coordinate of the popup's bottom
      * is less than the y coordinate of the anchor) or below the anchor view (the y coordinate
@@ -915,7 +922,7 @@
 
         anchor.getLocationInWindow(mDrawingLocation);
         p.x = mDrawingLocation[0] + xoff;
-        p.y = mDrawingLocation[1] + anchor.getMeasuredHeight() + yoff;
+        p.y = mDrawingLocation[1] + anchor.getHeight() + yoff;
         
         boolean onTop = false;
 
@@ -932,26 +939,26 @@
             // the edit box
             int scrollX = anchor.getScrollX();
             int scrollY = anchor.getScrollY();
-            Rect r = new Rect(scrollX, scrollY,  scrollX + mPopupWidth,
-                    scrollY + mPopupHeight + anchor.getMeasuredHeight());
+            Rect r = new Rect(scrollX, scrollY,  scrollX + mPopupWidth + xoff,
+                    scrollY + mPopupHeight + anchor.getHeight() + yoff);
             anchor.requestRectangleOnScreen(r, true);
-            
+
             // now we re-evaluate the space available, and decide from that
             // whether the pop-up will go above or below the anchor.
             anchor.getLocationInWindow(mDrawingLocation);
             p.x = mDrawingLocation[0] + xoff;
-            p.y = mDrawingLocation[1] + anchor.getMeasuredHeight() + yoff;
+            p.y = mDrawingLocation[1] + anchor.getHeight() + yoff;
             
             // determine whether there is more space above or below the anchor
             anchor.getLocationOnScreen(mScreenLocation);
             
-            onTop = (displayFrame.bottom - mScreenLocation[1] - anchor.getMeasuredHeight() - yoff) <
+            onTop = (displayFrame.bottom - mScreenLocation[1] - anchor.getHeight() - yoff) <
                     (mScreenLocation[1] - yoff - displayFrame.top);
             if (onTop) {
                 p.gravity = Gravity.LEFT | Gravity.BOTTOM;
                 p.y = root.getHeight() - mDrawingLocation[1] + yoff;
             } else {
-                p.y = mDrawingLocation[1] + anchor.getMeasuredHeight() + yoff;
+                p.y = mDrawingLocation[1] + anchor.getHeight() + yoff;
             }
         }
 
@@ -1257,13 +1264,16 @@
             }
         }
 
-        if (updateLocation) {
-            mAboveAnchor = findDropDownPosition(anchor, p, xoff, yoff);
-        } else {
-            mAboveAnchor = findDropDownPosition(anchor, p, mAnchorXoff, mAnchorYoff);            
-        }
+        int x = p.x;
+        int y = p.y;
 
-        update(p.x, p.y, width, height);
+        if (updateLocation) {
+            updateAboveAnchor(findDropDownPosition(anchor, p, xoff, yoff));
+        } else {
+            updateAboveAnchor(findDropDownPosition(anchor, p, mAnchorXoff, mAnchorYoff));            
+        }
+        
+        update(p.x, p.y, width, height, x != p.x || y != p.y);
     }
 
     /**