Update Search recents behavior

- per UX request
- Search recents show now by defaults with the recent searches
when the SearchView is empty
- when we press on a Serach recent, we are showing the Search results
and the recents are disappearing
- when keying text in the SearchView, we hide the recents and start
showing the results
- we still save the Search query when hitting on a result

Change-Id: I6c2be21660a30f6973dea27d4852b2068a05963d
This commit is contained in:
Fabrice Di Meglio
2014-05-23 16:03:43 -07:00
parent 2416a3e071
commit 7e4855e8f6
2 changed files with 31 additions and 30 deletions

View File

@@ -1212,7 +1212,7 @@ public class SettingsActivity extends Activity
@Override @Override
public boolean onQueryTextChange(String newText) { public boolean onQueryTextChange(String newText) {
mSearchQuery = newText; mSearchQuery = newText;
if (TextUtils.isEmpty(newText) || mSearchResultsFragment == null) { if (mSearchResultsFragment == null) {
return false; return false;
} }
return mSearchResultsFragment.onQueryTextChange(newText); return mSearchResultsFragment.onQueryTextChange(newText);

View File

@@ -51,7 +51,7 @@ public class SearchResultsSummary extends Fragment {
private static final String EMPTY_QUERY = ""; private static final String EMPTY_QUERY = "";
private static char ELLIPSIS = '\u2026'; private static char ELLIPSIS = '\u2026';
private static final String SAVE_KEY_SHOW_ONLY_RESULTS = ":settings:show_only_results"; private static final String SAVE_KEY_SHOW_RESULTS = ":settings:show_results";
private SearchView mSearchView; private SearchView mSearchView;
@@ -68,7 +68,7 @@ public class SearchResultsSummary extends Fragment {
private String mQuery; private String mQuery;
private boolean mShowOnlyResults; private boolean mShowResults;
/** /**
* A basic AsyncTask for updating the query results cursor * A basic AsyncTask for updating the query results cursor
@@ -83,6 +83,7 @@ public class SearchResultsSummary extends Fragment {
protected void onPostExecute(Cursor cursor) { protected void onPostExecute(Cursor cursor) {
if (!isCancelled()) { if (!isCancelled()) {
setResultsCursor(cursor); setResultsCursor(cursor);
setResultsVisibility(cursor.getCount() > 0);
} else if (cursor != null) { } else if (cursor != null) {
cursor.close(); cursor.close();
} }
@@ -102,6 +103,7 @@ public class SearchResultsSummary extends Fragment {
protected void onPostExecute(Cursor cursor) { protected void onPostExecute(Cursor cursor) {
if (!isCancelled()) { if (!isCancelled()) {
setSuggestionsCursor(cursor); setSuggestionsCursor(cursor);
setSuggestionsVisibility(cursor.getCount() > 0);
} else if (cursor != null) { } else if (cursor != null) {
cursor.close(); cursor.close();
} }
@@ -116,7 +118,7 @@ public class SearchResultsSummary extends Fragment {
mSuggestionsAdapter = new SuggestionsAdapter(getActivity()); mSuggestionsAdapter = new SuggestionsAdapter(getActivity());
if (savedInstanceState != null) { if (savedInstanceState != null) {
mShowOnlyResults = savedInstanceState.getBoolean(SAVE_KEY_SHOW_ONLY_RESULTS); mShowResults = savedInstanceState.getBoolean(SAVE_KEY_SHOW_RESULTS);
} }
} }
@@ -124,7 +126,7 @@ public class SearchResultsSummary extends Fragment {
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putBoolean(SAVE_KEY_SHOW_ONLY_RESULTS, mShowOnlyResults); outState.putBoolean(SAVE_KEY_SHOW_RESULTS, mShowResults);
} }
@Override @Override
@@ -209,10 +211,9 @@ public class SearchResultsSummary extends Fragment {
final Cursor cursor = mSuggestionsAdapter.mCursor; final Cursor cursor = mSuggestionsAdapter.mCursor;
cursor.moveToPosition(position); cursor.moveToPosition(position);
mShowResults = true;
mQuery = cursor.getString(0); mQuery = cursor.getString(0);
mSearchView.setQuery(mQuery, false); mSearchView.setQuery(mQuery, false);
setSuggestionsVisibility(false);
mShowOnlyResults = true;
} }
}); });
@@ -220,10 +221,10 @@ public class SearchResultsSummary extends Fragment {
} }
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onResume() {
super.onActivityCreated(savedInstanceState); super.onResume();
if (!mShowOnlyResults) { if (!mShowResults) {
showSomeSuggestions(); showSomeSuggestions();
} }
} }
@@ -250,31 +251,28 @@ public class SearchResultsSummary extends Fragment {
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
mQuery = getFilteredQueryString(query); mQuery = getFilteredQueryString(query);
setSuggestionsVisibility(!mShowOnlyResults); mShowResults = true;
setSuggestionsVisibility(false);
updateSearchResults(); updateSearchResults();
saveQueryToDatabase();
return true; return true;
} }
public boolean onQueryTextChange(String query) { public boolean onQueryTextChange(String query) {
final String newQuery = getFilteredQueryString(query); final String newQuery = getFilteredQueryString(query);
boolean isNewQuery;
if (!TextUtils.isEmpty(mQuery)) {
isNewQuery = !mQuery.equals(query);
} else {
isNewQuery = !TextUtils.isEmpty(query);
}
mQuery = newQuery; mQuery = newQuery;
if (isNewQuery) { if (TextUtils.isEmpty(mQuery)) {
mShowOnlyResults = false; mShowResults = false;
} setResultsVisibility(false);
if (!mShowOnlyResults) {
updateSuggestions(); updateSuggestions();
} else {
mShowResults = true;
setSuggestionsVisibility(false);
updateSearchResults();
} }
updateSearchResults();
return true; return true;
} }
@@ -335,30 +333,33 @@ public class SearchResultsSummary extends Fragment {
return filtered.toString(); return filtered.toString();
} }
private void updateSuggestions() { private void clearAllTasks() {
if (mUpdateSearchResultsTask != null) {
mUpdateSearchResultsTask.cancel(false);
mUpdateSearchResultsTask = null;
}
if (mUpdateSuggestionsTask != null) { if (mUpdateSuggestionsTask != null) {
mUpdateSuggestionsTask.cancel(false); mUpdateSuggestionsTask.cancel(false);
mUpdateSuggestionsTask = null; mUpdateSuggestionsTask = null;
} }
}
private void updateSuggestions() {
clearAllTasks();
if (mQuery == null) { if (mQuery == null) {
setSuggestionsCursor(null); setSuggestionsCursor(null);
} else { } else {
setSuggestionsVisibility(true);
mUpdateSuggestionsTask = new UpdateSuggestionsTask(); mUpdateSuggestionsTask = new UpdateSuggestionsTask();
mUpdateSuggestionsTask.execute(mQuery); mUpdateSuggestionsTask.execute(mQuery);
} }
} }
private void updateSearchResults() { private void updateSearchResults() {
if (mUpdateSearchResultsTask != null) { clearAllTasks();
mUpdateSearchResultsTask.cancel(false);
mUpdateSearchResultsTask = null;
}
if (TextUtils.isEmpty(mQuery)) { if (TextUtils.isEmpty(mQuery)) {
setResultsVisibility(false); setResultsVisibility(false);
setResultsCursor(null); setResultsCursor(null);
} else { } else {
setResultsVisibility(true);
mUpdateSearchResultsTask = new UpdateSearchResultsTask(); mUpdateSearchResultsTask = new UpdateSearchResultsTask();
mUpdateSearchResultsTask.execute(mQuery); mUpdateSearchResultsTask.execute(mQuery);
} }