Merge "Update DisplayMetrics when resizing" into nyc-dev
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index b3daa16..195786d 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -1723,7 +1723,7 @@
             }
 
             boolean hwInitialized = false;
-            boolean contentInsetsChanged = false;
+            boolean framesChanged = false;
             boolean hadSurface = mSurface.isValid();
 
             try {
@@ -1763,7 +1763,7 @@
 
                 final boolean overscanInsetsChanged = !mPendingOverscanInsets.equals(
                         mAttachInfo.mOverscanInsets);
-                contentInsetsChanged = !mPendingContentInsets.equals(
+                boolean contentInsetsChanged = !mPendingContentInsets.equals(
                         mAttachInfo.mContentInsets);
                 final boolean visibleInsetsChanged = !mPendingVisibleInsets.equals(
                         mAttachInfo.mVisibleInsets);
@@ -1817,7 +1817,7 @@
                 // measure cache is cleared. We might have a pending MSG_RESIZED_REPORT
                 // that is supposed to take care of it, but since pending insets are
                 // already modified here, it won't detect the frame change after this.
-                final boolean framesChanged = overscanInsetsChanged
+                framesChanged = overscanInsetsChanged
                         || contentInsetsChanged
                         || stableInsetsChanged
                         || visibleInsetsChanged
@@ -2009,7 +2009,7 @@
                 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
                         (relayoutResult&WindowManagerGlobal.RELAYOUT_RES_IN_TOUCH_MODE) != 0);
                 if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth()
-                        || mHeight != host.getMeasuredHeight() || contentInsetsChanged ||
+                        || mHeight != host.getMeasuredHeight() || framesChanged ||
                         updatedConfiguration) {
                     int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
                     int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
@@ -2018,7 +2018,7 @@
                             + mWidth + " measuredWidth=" + host.getMeasuredWidth()
                             + " mHeight=" + mHeight
                             + " measuredHeight=" + host.getMeasuredHeight()
-                            + " coveredInsetsChanged=" + contentInsetsChanged);
+                            + " framesChanged=" + framesChanged);
 
                      // Ask host how big it wants to be
                     performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
diff --git a/core/java/android/widget/PopupWindow.java b/core/java/android/widget/PopupWindow.java
index 4066ef1..6477f07 100644
--- a/core/java/android/widget/PopupWindow.java
+++ b/core/java/android/widget/PopupWindow.java
@@ -1393,6 +1393,14 @@
         }
     }
 
+    private int computeGravity() {
+        int gravity = Gravity.START | Gravity.TOP;
+        if (mClipToScreen || mClippingEnabled) {
+            gravity |= Gravity.DISPLAY_CLIP_VERTICAL | Gravity.DISPLAY_CLIP_HORIZONTAL;
+        }
+        return gravity;
+    }
+
     /**
      * <p>Generate the layout parameters for the popup window.</p>
      *
@@ -1407,7 +1415,7 @@
         // screen. The view is then positioned to the appropriate location by
         // setting the x and y offsets to match the anchor's bottom-left
         // corner.
-        p.gravity = Gravity.START | Gravity.TOP;
+        p.gravity = computeGravity();
         p.flags = computeFlags(p.flags);
         p.type = mWindowLayoutType;
         p.token = token;
@@ -1958,6 +1966,12 @@
             update = true;
         }
 
+        final int newGravity = computeGravity();
+        if (newGravity != p.gravity) {
+            p.gravity = newGravity;
+            update = true;
+        }
+
         if (update) {
             setLayoutDirectionFromAnchor();
             mWindowManager.updateViewLayout(mDecorView, p);
@@ -2064,6 +2078,12 @@
             update = true;
         }
 
+        final int newGravity = computeGravity();
+        if (newGravity != p.gravity) {
+            p.gravity = newGravity;
+            update = true;
+        }
+
         int newAccessibilityIdOfAnchor =
                 (mAnchor != null) ? mAnchor.get().getAccessibilityViewId() : -1;
         if (newAccessibilityIdOfAnchor != p.accessibilityIdOfAnchor) {