Bug 836589 - Lazily switch between AwesomeBar search and URL modes; r=blassey
authorJim Chen <nchen@mozilla.com>
Thu, 07 Feb 2013 18:17:06 -0500
changeset 131114 c362e5037dcc16ba796015f8600414a32b3fb55f
parent 131113 46d7188acb0c17a6d3320cac21b5cbc41be454c7
child 131115 b8c6eb40240fa2f42856a86f8fc27d1ac926729c
push id2323
push userbbajaj@mozilla.com
push dateMon, 01 Apr 2013 19:47:02 +0000
treeherdermozilla-beta@7712be144d91 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersblassey
bugs836589
milestone21.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 836589 - Lazily switch between AwesomeBar search and URL modes; r=blassey
mobile/android/base/AwesomeBar.java
mobile/android/base/awesomebar/AllPagesTab.java
mobile/android/base/util/StringUtils.java
--- a/mobile/android/base/AwesomeBar.java
+++ b/mobile/android/base/AwesomeBar.java
@@ -314,34 +314,36 @@ public class AwesomeBar extends GeckoAct
             return;
         }
 
         mGoButton.setVisibility(View.VISIBLE);
 
         int imageResource = R.drawable.ic_awesomebar_go;
         String contentDescription = getString(R.string.go);
         int imeAction = EditorInfo.IME_ACTION_GO;
-        if (StringUtils.isSearchQuery(text)) {
+
+        int actionBits = mText.getImeOptions() & EditorInfo.IME_MASK_ACTION;
+        if (StringUtils.isSearchQuery(text, actionBits == EditorInfo.IME_ACTION_SEARCH)) {
             imageResource = R.drawable.ic_awesomebar_search;
             contentDescription = getString(R.string.search);
             imeAction = EditorInfo.IME_ACTION_SEARCH;
         }
         mGoButton.setImageResource(imageResource);
         mGoButton.setContentDescription(contentDescription);
 
         InputMethodManager imm = InputMethods.getInputMethodManager(mText.getContext());
         if (imm == null) {
             return;
         }
-        int actionBits = mText.getImeOptions() & EditorInfo.IME_MASK_ACTION;
         if (actionBits != imeAction) {
             int optionBits = mText.getImeOptions() & ~EditorInfo.IME_MASK_ACTION;
             mText.setImeOptions(optionBits | imeAction);
 
-            mDelayRestartInput = InputMethods.shouldDelayAwesomebarUpdate(mText.getContext());
+            mDelayRestartInput = (imeAction == EditorInfo.IME_ACTION_GO) &&
+                                 (InputMethods.shouldDelayAwesomebarUpdate(mText.getContext()));
             if (!mDelayRestartInput) {
                 imm.restartInput(mText);
             }
         } else if (mDelayRestartInput) {
             // Only call delayed restartInput when actionBits == imeAction
             // so if there are two restarts in a row, the first restarts will
             // be discarded and the second restart will be properly delayed
             mDelayRestartInput = false;
@@ -448,18 +450,23 @@ public class AwesomeBar extends GeckoAct
             }
             return true;
         }
     }
 
     @Override
     public void onResume() {
         super.onResume();
-        if (mText != null && mText.getText() != null)
+        if (mText != null && mText.getText() != null) {
             updateGoButton(mText.getText().toString());
+            if (mDelayRestartInput) {
+                // call updateGoButton again to force a restartInput call
+                updateGoButton(mText.getText().toString());
+            }
+        }
 
         // Invlidate the cached value that keeps track of whether or
         // not desktop bookmarks exist
         BrowserDB.invalidateCachedState();
     }
 
     @Override
     public void onDestroy() {
--- a/mobile/android/base/awesomebar/AllPagesTab.java
+++ b/mobile/android/base/awesomebar/AllPagesTab.java
@@ -455,17 +455,17 @@ public class AllPagesTab extends Awesome
                     AwesomeBarTabs.OnUrlOpenListener listener = getUrlListener();
                     if (listener != null) {
                         String suggestion = ((TextView) v.findViewById(R.id.suggestion_text)).getText().toString();
 
                         // If we're not clicking the user-entered view (the
                         // first suggestion item) and the search matches a URL
                         // pattern, go to that URL. Otherwise, do a search for
                         // the term.
-                        if (v != viewHolder.userEnteredView && !StringUtils.isSearchQuery(suggestion)) {
+                        if (v != viewHolder.userEnteredView && !StringUtils.isSearchQuery(suggestion, false)) {
                             listener.onUrlOpen(suggestion, null);
                         } else {
                             listener.onSearch(engine.name, suggestion);
                         }
                     }
                 }
             };
 
--- a/mobile/android/base/util/StringUtils.java
+++ b/mobile/android/base/util/StringUtils.java
@@ -2,35 +2,46 @@
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.util;
 
 public class StringUtils {
     /*
-     * This method tries to guess if the given string could be a search query or URL
+     * This method tries to guess if the given string could be a search query or URL,
+     * and returns a previous result if there is ambiguity
+     *
      * Search examples:
      *  foo
      *  foo bar.com
      *  foo http://bar.com
      *
      * URL examples
      *  foo.com
      *  foo.c
      *  :foo
      *  http://foo.com bar
+     *
+     * wasSearchQuery specifies whether text was a search query before the latest change
+     * in text. In ambiguous cases where the new text can be either a search or a URL,
+     * wasSearchQuery is returned
     */
-    public static boolean isSearchQuery(String text) {
-        text = text.trim();
+    public static boolean isSearchQuery(String text, boolean wasSearchQuery) {
         if (text.length() == 0)
-            return false;
+            return wasSearchQuery;
 
         int colon = text.indexOf(':');
         int dot = text.indexOf('.');
         int space = text.indexOf(' ');
 
-        // If a space is found before any dot or colon, we assume this is a search query
-        boolean spacedOut = space > -1 && (space < colon || space < dot);
-
-        return spacedOut || (dot == -1 && colon == -1);
+        // If a space is found before any dot and colon, we assume this is a search query
+        if (space > -1 && (colon == -1 || space < colon) && (dot == -1 || space < dot)) {
+            return true;
+        }
+        // Otherwise, if a dot or a colon is found, we assume this is a URL
+        if (dot > -1 || colon > -1) {
+            return false;
+        }
+        // Otherwise, text is ambiguous, and we keep its status unchanged
+        return wasSearchQuery;
     }
 }