Bug 1063193 - Add mobile-specific URL to wikipedia search plugin. r=bnicholson

This commit is contained in:
Margaret Leibovic 2014-10-10 10:24:30 -04:00
Родитель 179060f6e5
Коммит 6e5a3d23d1
2 изменённых файлов: 30 добавлений и 4 удалений

Просмотреть файл

@ -13,6 +13,8 @@ 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;
import java.util.Set;
@ -26,6 +28,8 @@ public class SearchEngine {
private static final String URLTYPE_SUGGEST_JSON = "application/x-suggestions+json";
private static final String URLTYPE_SEARCH_HTML = "text/html";
private static final String URL_REL_MOBILE = "mobile";
// Parameters copied from nsSearchService.js
private static final String MOZ_PARAM_LOCALE = "\\{moz:locale\\}";
private static final String MOZ_PARAM_DIST_ID = "\\{moz:distributionID\\}";
@ -52,8 +56,8 @@ public class SearchEngine {
private String shortName;
private String iconURL;
// TODO: Make something more robust (like EngineURL in nsSearchService.js)
private Uri resultsUri;
// Ordered list of preferred results URIs.
private final List<Uri> resultsUris = new ArrayList<Uri>();
private Uri suggestUri;
/**
@ -102,6 +106,7 @@ public class SearchEngine {
final String type = parser.getAttributeValue(null, "type");
final String template = parser.getAttributeValue(null, "template");
final String rel = parser.getAttributeValue(null, "rel");
Uri uri = Uri.parse(template);
@ -125,7 +130,12 @@ public class SearchEngine {
}
if (type.equals(URLTYPE_SEARCH_HTML)) {
resultsUri = uri;
// Prefer mobile URIs.
if (rel != null && rel.equals(URL_REL_MOBILE)) {
resultsUris.add(0, uri);
} else {
resultsUris.add(uri);
}
} else if (type.equals(URLTYPE_SUGGEST_JSON)) {
suggestUri = uri;
}
@ -200,7 +210,7 @@ public class SearchEngine {
* the url will be sent to Fennec.
*/
public boolean isSearchResultsPage(String url) {
return resultsUri.getAuthority().equalsIgnoreCase(Uri.parse(url).getAuthority());
return getResultsUri().getAuthority().equalsIgnoreCase(Uri.parse(url).getAuthority());
}
/**
@ -210,6 +220,7 @@ public class SearchEngine {
* @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();
for (String name : names) {
if (resultsUri.getQueryParameter(name).matches(OS_PARAM_USER_DEFINED)) {
@ -225,6 +236,7 @@ public class SearchEngine {
* @param query The user's query. This method will escape and encode the query.
*/
public String resultsUriForQuery(String query) {
final Uri resultsUri = getResultsUri();
if (resultsUri == null) {
Log.e(LOG_TAG, "No results URL for search engine: " + identifier);
return "";
@ -247,6 +259,16 @@ public class SearchEngine {
return paramSubstitution(template, Uri.encode(query));
}
/**
* @return Preferred results URI.
*/
private Uri getResultsUri() {
if (resultsUris.isEmpty()) {
return null;
}
return resultsUris.get(0);
}
/**
* Formats template string with proper parameters. Modeled after
* ParamSubstitution in nsSearchService.js

Просмотреть файл

@ -14,5 +14,9 @@
<Param name="search" value="{searchTerms}"/>
<Param name="sourceid" value="Mozilla-search"/>
</Url>
<Url type="text/html" method="GET" rel="mobile" template="https://en.m.wikipedia.org/wiki/Special:Search">
<Param name="search" value="{searchTerms}"/>
<Param name="sourceid" value="Mozilla-search"/>
</Url>
<SearchForm>https://en.wikipedia.org/wiki/Special:Search</SearchForm>
</SearchPlugin>