Bug 1041604 - Update query in search bar when user navigates to new results page. r=wesj

This commit is contained in:
Margaret Leibovic 2014-10-02 07:49:43 -07:00
Родитель 40b848e7e5
Коммит fa2c67d7dc
4 изменённых файлов: 41 добавлений и 2 удалений

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

@ -32,6 +32,13 @@ public interface AcceptsSearchQuery {
*/
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.
*/

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

@ -95,8 +95,18 @@ public class PostSearchFragment extends Fragment {
@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;
}

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

@ -247,6 +247,11 @@ public class SearchActivity extends LocaleAware.LocaleAwareFragmentActivity
}
}
@Override
public void onQueryChange(String query) {
searchBar.setText(query);
}
private void startSearch(final String query) {
if (engine != null) {
postSearchFragment.startSearch(engine, query);

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

@ -14,6 +14,7 @@ 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
@ -202,6 +203,22 @@ public class SearchEngine {
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.
*