Bug 704879 - (4/6) Make FormAssistant in charge of hiding an empty popup. r=lucasr
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Tue, 06 Mar 2012 11:56:43 -0800
changeset 88396 44923daa1770f8e3716074d40d128daf4be40785
parent 88395 c39f37a0d9f45e6b0ea418b5452f5d9870b80276
child 88397 c251670745e0935339b1df94903d6c34485da872
push id22194
push usermak77@bonardo.net
push dateWed, 07 Mar 2012 09:33:54 +0000
treeherdermozilla-central@8ef88a69f861 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslucasr
bugs704879
milestone13.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 704879 - (4/6) Make FormAssistant in charge of hiding an empty popup. r=lucasr
mobile/android/base/FormAssistPopup.java
mobile/android/chrome/content/browser.js
--- a/mobile/android/base/FormAssistPopup.java
+++ b/mobile/android/base/FormAssistPopup.java
@@ -87,37 +87,40 @@ public class FormAssistPopup extends Lis
             public void onItemClick(AdapterView<?> parentView, View view, int position, long id) {
                 String value = ((TextView) view).getText().toString();
                 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FormAssist:AutoComplete", value));
                 hide();
             }
         });
 
         GeckoAppShell.registerGeckoEventListener("FormAssist:AutoComplete", this);
+        GeckoAppShell.registerGeckoEventListener("FormAssist:Hide", this);
     }
 
     public void handleMessage(String event, JSONObject message) {
         try {
             if (event.equals("FormAssist:AutoComplete")) {
                 final JSONArray suggestions = message.getJSONArray("suggestions");
-                if (suggestions.length() == 0) {
-                    hide();
-                } else {
-                    final JSONArray rect = message.getJSONArray("rect");
-                    final double zoom = message.getDouble("zoom");
-                    GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
-                         public void run() {
-                             // Don't show autocomplete popup when using fullscreen VKB
-                             InputMethodManager imm =
-                                     (InputMethodManager) GeckoApp.mAppContext.getSystemService(Context.INPUT_METHOD_SERVICE);
-                             if (!imm.isFullscreenMode())
-                                 show(suggestions, rect, zoom);
-                         }
-                    });
-                }
+                final JSONArray rect = message.getJSONArray("rect");
+                final double zoom = message.getDouble("zoom");
+                GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
+                    public void run() {
+                        // Don't show autocomplete popup when using fullscreen // VKB
+                        InputMethodManager imm =
+                                (InputMethodManager) GeckoApp.mAppContext.getSystemService(Context.INPUT_METHOD_SERVICE);
+                        if (!imm.isFullscreenMode())
+                            show(suggestions, rect, zoom);
+                    }
+                });
+            } else if (event.equals("FormAssist:Hide")) {
+                GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
+                    public void run() {
+                        hide();
+                    }
+                });
             }
         } catch (Exception e) {
             Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
         }
     }
 
     public void show(JSONArray suggestions, JSONArray rect, double zoom) {
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, R.layout.autocomplete_list_item);
@@ -206,12 +209,12 @@ public class FormAssistPopup extends Lis
         mLayout.setMargins(listLeft, listTop, 0, 0);
         setLayoutParams(mLayout);
         requestLayout();
     }
 
     public void hide() {
         if (isShown()) {
             setVisibility(View.GONE);
-            GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FormAssist:Closed", null));
+            GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FormAssist:Hidden", null));
         }
     }
 }
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -2833,48 +2833,52 @@ var ErrorPageEventHandler = {
 
 var FormAssistant = {
   // Used to keep track of the element that corresponds to the current
   // autocomplete suggestions
   _currentInputElement: null,
 
   init: function() {
     Services.obs.addObserver(this, "FormAssist:AutoComplete", false);
-    Services.obs.addObserver(this, "FormAssist:Closed", false);
+    Services.obs.addObserver(this, "FormAssist:Hidden", false);
 
     BrowserApp.deck.addEventListener("input", this, false);
   },
 
   uninit: function() {
     Services.obs.removeObserver(this, "FormAssist:AutoComplete");
-    Services.obs.removeObserver(this, "FormAssist:Closed");
+    Services.obs.removeObserver(this, "FormAssist:Hidden");
   },
 
   observe: function(aSubject, aTopic, aData) {
     switch (aTopic) {
       case "FormAssist:AutoComplete":
         if (!this._currentInputElement)
           break;
 
         // Remove focus from the textbox to avoid some bad IME interactions
         this._currentInputElement.blur();
         this._currentInputElement.value = aData;
         break;
 
-      case "FormAssist:Closed":
+      case "FormAssist:Hidden":
         this._currentInputElement = null;
         break;
     }
   },
 
   handleEvent: function(aEvent) {
     switch (aEvent.type) {
       case "input":
         let currentElement = aEvent.target;
-        this._showAutoCompleteSuggestions(currentElement);
+        if (this._showAutoCompleteSuggestions(currentElement))
+          break;
+
+        // If we're not showing autocomplete suggestions, hide the form assist popup
+        this._hideFormAssistPopup();
     }
   },
 
   // We only want to show autocomplete suggestions for certain elements
   _isAutoComplete: function _isAutoComplete(aElement) {
     if (!(aElement instanceof HTMLInputElement) ||
         (aElement.getAttribute("type") == "password") ||
         (aElement.hasAttribute("autocomplete") &&
@@ -2923,31 +2927,41 @@ var FormAssistant = {
   // and sends the suggestions to the Java UI, along with element position data.
   // Returns true if there are suggestions to show, false otherwise.
   _showAutoCompleteSuggestions: function _showAutoCompleteSuggestions(aElement) {
     if (!this._isAutoComplete(aElement))
       return false;
 
     let suggestions = this._getAutoCompleteSuggestions(aElement.value, aElement);
 
+    // Return false if there are no suggestions to show
+    if (!suggestions.length)
+      return false;
+
     let positionData = this._getElementPositionData(aElement);
     sendMessageToJava({
       gecko: {
         type:  "FormAssist:AutoComplete",
         suggestions: suggestions,
         rect: positionData.rect,
         zoom: positionData.zoom
       }
     });
 
     // Keep track of input element so we can fill it in if the user
     // selects an autocomplete suggestion
     this._currentInputElement = aElement;
 
     return true;
+  },
+
+  _hideFormAssistPopup: function _hideFormAssistPopup() {
+    sendMessageToJava({
+      gecko: { type:  "FormAssist:Hide" }
+    });
   }
 };
 
 var XPInstallObserver = {
   init: function xpi_init() {
     Services.obs.addObserver(XPInstallObserver, "addon-install-blocked", false);
     Services.obs.addObserver(XPInstallObserver, "addon-install-started", false);