Bug 1041604 - Update query in search bar when user navigates to new results page. r=wesj
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Thu, 02 Oct 2014 07:49:43 -0700
changeset 208628 c3793a1dd33b2216e4da54af44d0e7ead0136f1a
parent 208627 e45de3a150aa63776797ee815284cf0b4511c94a
child 208629 497e4c92e6800e83c0015bbc84f619da9cb19751
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerswesj
bugs1041604
milestone35.0a1
Bug 1041604 - Update query in search bar when user navigates to new results page. r=wesj
mobile/android/search/java/org/mozilla/search/AcceptsSearchQuery.java
mobile/android/search/java/org/mozilla/search/PostSearchFragment.java
mobile/android/search/java/org/mozilla/search/SearchActivity.java
mobile/android/search/java/org/mozilla/search/providers/SearchEngine.java
--- a/mobile/android/search/java/org/mozilla/search/AcceptsSearchQuery.java
+++ b/mobile/android/search/java/org/mozilla/search/AcceptsSearchQuery.java
@@ -28,14 +28,21 @@ public interface AcceptsSearchQuery {
      * Starts a search and animates a suggestion.
      *
      * @param query
      * @param suggestionAnimation
      */
     void onSearch(String query, SuggestionAnimation suggestionAnimation);
 
     /**
+     * Handles a change to the current search query.
+     *
+     * @param query
+     */
+    void onQueryChange(String query);
+
+    /**
      * Interface to specify search suggestion animation details.
      */
     public interface SuggestionAnimation {
         public Rect getStartBounds();
     }
 }
--- a/mobile/android/search/java/org/mozilla/search/PostSearchFragment.java
+++ b/mobile/android/search/java/org/mozilla/search/PostSearchFragment.java
@@ -90,18 +90,28 @@ public class PostSearchFragment extends 
         @Override
         public void onPageStarted(WebView view, final String url, Bitmap favicon) {
             // Reset the error state.
             networkError = false;
         }
 
         @Override
         public boolean shouldOverrideUrlLoading(WebView view, String url) {
-            // We keep URLs in the webview that are either about:blank or a search engine result page.
-            if (TextUtils.equals(url, Constants.ABOUT_BLANK) || engine.isSearchResultsPage(url)) {
+            // Ignore about:blank URL loads.
+            if (TextUtils.equals(url, Constants.ABOUT_BLANK)) {
+                return false;
+            }
+
+            // If the URL is a results page, don't override the URL load, but
+            // do update the query in the search bar if possible.
+            if (engine.isSearchResultsPage(url)) {
+                final String query = engine.queryForResultsUrl(url);
+                if (!TextUtils.isEmpty(query)) {
+                    ((AcceptsSearchQuery) getActivity()).onQueryChange(query);
+                }
                 return false;
             }
 
             try {
                 // If the url URI does not have an intent scheme, the intent data will be the entire
                 // URI and its action will be ACTION_VIEW.
                 final Intent i = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
 
--- a/mobile/android/search/java/org/mozilla/search/SearchActivity.java
+++ b/mobile/android/search/java/org/mozilla/search/SearchActivity.java
@@ -242,16 +242,21 @@ public class SearchActivity extends Loca
             animateSuggestion(suggestionAnimation);
         } else {
             // Otherwise immediately switch to the results view.
             setEditState(EditState.WAITING);
             setSearchState(SearchState.POSTSEARCH);
         }
     }
 
+    @Override
+    public void onQueryChange(String query) {
+        searchBar.setText(query);
+    }
+
     private void startSearch(final String query) {
         if (engine != null) {
             postSearchFragment.startSearch(engine, query);
             return;
         }
 
         // engine will only be null if startSearch is called before the getEngine
         // call in onCreate is completed.
--- a/mobile/android/search/java/org/mozilla/search/providers/SearchEngine.java
+++ b/mobile/android/search/java/org/mozilla/search/providers/SearchEngine.java
@@ -9,16 +9,17 @@ import android.util.Log;
 import android.util.Xml;
 
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Locale;
+import java.util.Set;
 
 /**
  * Extend this class to add a new search engine to
  * the search activity.
  */
 public class SearchEngine {
     private static final String LOG_TAG = "SearchEngine";
 
@@ -198,16 +199,32 @@ public class SearchEngine {
      * Determine whether a particular url belongs to this search engine. If not,
      * the url will be sent to Fennec.
      */
     public boolean isSearchResultsPage(String url) {
         return resultsUri.getAuthority().equalsIgnoreCase(Uri.parse(url).getAuthority());
     }
 
     /**
+     * Finds the search query encoded in a given results URL.
+     *
+     * @param url Current results URL.
+     * @return The search query, or an empty string if a query couldn't be found.
+     */
+    public String queryForResultsUrl(String url) {
+        final Set<String> names = resultsUri.getQueryParameterNames();
+        for (String name : names) {
+            if (resultsUri.getQueryParameter(name).matches(OS_PARAM_USER_DEFINED)) {
+                return Uri.parse(url).getQueryParameter(name);
+            }
+        }
+        return "";
+    }
+
+    /**
      * Create a uri string that can be used to fetch the results page.
      *
      * @param query The user's query. This method will escape and encode the query.
      */
     public String resultsUriForQuery(String query) {
         if (resultsUri == null) {
             Log.e(LOG_TAG, "No results URL for search engine: " + identifier);
             return "";