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
parent 9c45c57fe7
commit 4a1e72ff69
4 changed files with 41 additions and 2 deletions

View File

@ -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.
*/

View File

@ -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;
}

View File

@ -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);

View File

@ -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.
*