Bug 1114589 - Add compatibility layer for Uri.getQueryParameterNames. r=rnewman

--HG--
extra : rebase_source : 3b7d1fa11409cf3de9855df3a6575319b5a6f93c
This commit is contained in:
Margaret Leibovic 2014-12-22 13:35:41 -05:00
parent 8020afb31d
commit a55507efbe
2 changed files with 55 additions and 1 deletions

View File

@ -8,6 +8,12 @@ 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";
@ -187,4 +193,51 @@ public class StringUtils {
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);
}
}

View File

@ -8,6 +8,7 @@ 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;
@ -234,7 +235,7 @@ public class SearchEngine {
*/
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);