Bug 711198: Autocomplete Popup should have a minWidth. [r=mfinkle]
authorSriram Ramasubramanian <sriram@mozilla.com>
Fri, 27 Jan 2012 12:10:45 -0800
changeset 86837 0c26709f20dc82b401da8584fae5fdb8ed8144ee
parent 86836 9699edcbceddfc85c3dca074a7a69eadb9186865
child 86838 2108d51be2e7ac67f0f79bdf97969e5a160f7380
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs711198
milestone12.0a1
Bug 711198: Autocomplete Popup should have a minWidth. [r=mfinkle]
mobile/android/base/AutoCompletePopup.java
--- a/mobile/android/base/AutoCompletePopup.java
+++ b/mobile/android/base/AutoCompletePopup.java
@@ -38,16 +38,17 @@
 
 package org.mozilla.gecko;
 
 import org.mozilla.gecko.gfx.FloatSize;
 
 import android.content.Context;
 import android.util.Log;
 import android.util.AttributeSet;
+import android.util.DisplayMetrics;
 import android.view.View;
 import android.view.animation.Animation;
 import android.view.animation.AnimationUtils;
 import android.widget.ArrayAdapter;
 import android.widget.AdapterView;
 import android.widget.RelativeLayout;
 import android.widget.ListView;
 import android.widget.TextView;
@@ -61,16 +62,19 @@ public class AutoCompletePopup extends L
 
     private int mWidth;
     private int mHeight;
 
     private Animation mAnimation; 
 
     private static final String LOGTAG = "AutoCompletePopup";
 
+    private static int sMinWidth = 0;
+    private static final int AUTOCOMPLETE_MIN_WIDTH_IN_DPI = 200;
+
     public AutoCompletePopup(Context context, AttributeSet attrs) {
         super(context, attrs);
         mContext = context;
 
         mAnimation = AnimationUtils.loadAnimation(context, R.anim.grow_fade_in);
         mAnimation.setDuration(75);
 
         setFocusable(false);
@@ -122,20 +126,35 @@ public class AutoCompletePopup extends L
 
         int listWidth = mWidth;
         int listHeight = mHeight;
         int listLeft = left < 0 ? 0 : left;
         int listTop = top + height;
 
         FloatSize viewport = GeckoApp.mAppContext.getLayerController().getViewportSize();
 
+        // Late initializing variable to allow DisplayMetrics not to be null and avoid NPE
+        if (sMinWidth == 0) {
+            DisplayMetrics metrics = new DisplayMetrics();
+            GeckoApp.mAppContext.getWindowManager().getDefaultDisplay().getMetrics(metrics);
+            sMinWidth = (int) (AUTOCOMPLETE_MIN_WIDTH_IN_DPI * metrics.density);
+        }
+
         // If the textbox is smaller than the screen-width,
         // shrink the list's width
         if ((left + width) < viewport.width) 
-            listWidth = left + width;
+            listWidth = left < 0 ? left + width : width;
+
+        // listWidth can be negative if it is a constant - FILL_PARENT or MATCH_PARENT
+        if (listWidth >= 0 && listWidth < sMinWidth) {
+            listWidth = sMinWidth;
+
+            if ((listLeft + listWidth) > viewport.width)
+                listLeft = (int) (viewport.width - listWidth);
+        }
 
         // If the list is extending outside of the viewport
         // try moving above
         if (((listTop + listHeight) > viewport.height) && (listHeight <= top))
             listTop = (top - listHeight);
 
         mLayout = new RelativeLayout.LayoutParams(listWidth, listHeight);
         mLayout.setMargins(listLeft, listTop, 0, 0);