Bug 1114589 - Add compatibility layer for Uri.getQueryParameterNames. r=rnewman
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Mon, 22 Dec 2014 13:35:41 -0500
changeset 220923 9d54f4c91c31b7f902c15205e3439cd24b97c305
parent 220922 b8e5a6a721a5cac4eea8fee2c013bbfc0f43579a
child 220924 86225c7d5bd0607b0688dba38da43fb84fd9fa79
push id28003
push userryanvm@gmail.com
push dateMon, 22 Dec 2014 22:44:16 +0000
treeherdermozilla-central@d5167dc0ded3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs1114589
milestone37.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 1114589 - Add compatibility layer for Uri.getQueryParameterNames. r=rnewman
mobile/android/base/util/StringUtils.java
mobile/android/search/java/org/mozilla/search/providers/SearchEngine.java
--- a/mobile/android/base/util/StringUtils.java
+++ b/mobile/android/base/util/StringUtils.java
@@ -3,16 +3,22 @@
  * 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;
 
 import android.net.Uri;
 import android.text.TextUtils;
 
+import org.mozilla.gecko.AppConstants.Versions;
+
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
 public class StringUtils {
     private static final String LOGTAG = "GeckoStringUtils";
 
     private static final String FILTER_URL_PREFIX = "filter://";
     private static final String USER_ENTERED_URL_PREFIX = "user-entered:";
 
     /*
      * This method tries to guess if the given string could be a search query or URL,
@@ -182,9 +188,56 @@ public class StringUtils {
             return uri.getSchemeSpecificPart();
         }
         return url;
     }
 
     public static String encodeUserEnteredUrl(String url) {
         return Uri.fromParts("user-entered", url, null).toString();
     }
+
+    /**
+     * Compatibility layer for API < 11.
+     *
+     * Returns a set of the unique names of all query parameters. Iterating
+     * over the set will return the names in order of their first occurrence.
+     *
+     * @param uri
+     * @throws UnsupportedOperationException if this isn't a hierarchical URI
+     *
+     * @return a set of decoded names
+     */
+    public static Set<String> getQueryParameterNames(Uri uri) {
+        if (Versions.feature11Plus) {
+            return uri.getQueryParameterNames();
+        }
+
+        // Logic below copied from Uri.java included with Android 5.0.0.
+        if (uri.isOpaque()) {
+            throw new UnsupportedOperationException("This isn't a hierarchical URI.");
+        }
+
+        String query = uri.getEncodedQuery();
+        if (query == null) {
+            return Collections.emptySet();
+        }
+
+        Set<String> names = new LinkedHashSet<String>();
+        int start = 0;
+        do {
+            int next = query.indexOf('&', start);
+            int end = (next == -1) ? query.length() : next;
+
+            int separator = query.indexOf('=', start);
+            if (separator > end || separator == -1) {
+                separator = end;
+            }
+
+            String name = query.substring(start, separator);
+            names.add(Uri.decode(name));
+
+            // Move start to end of name.
+            start = end + 1;
+        } while (start < query.length());
+
+        return Collections.unmodifiableSet(names);
+    }
 }
--- a/mobile/android/search/java/org/mozilla/search/providers/SearchEngine.java
+++ b/mobile/android/search/java/org/mozilla/search/providers/SearchEngine.java
@@ -3,16 +3,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.search.providers;
 
 import android.net.Uri;
 import android.util.Log;
 import android.util.Xml;
 
+import org.mozilla.gecko.util.StringUtils;
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
@@ -229,17 +230,17 @@ public class SearchEngine {
     /**
      * 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 Uri resultsUri = getResultsUri();
-        final Set<String> names = resultsUri.getQueryParameterNames();
+        final Set<String> names = StringUtils.getQueryParameterNames(resultsUri);
         for (String name : names) {
             if (resultsUri.getQueryParameter(name).matches(OS_PARAM_USER_DEFINED)) {
                 return Uri.parse(url).getQueryParameter(name);
             }
         }
         return "";
     }