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
diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index 5b78950..f2d4026 100644
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -1212,7 +1212,7 @@
     @Override
     public boolean onQueryTextChange(String newText) {
         mSearchQuery = newText;
-        if (TextUtils.isEmpty(newText) || mSearchResultsFragment == null) {
+        if (mSearchResultsFragment == null) {
             return false;
         }
         return mSearchResultsFragment.onQueryTextChange(newText);
diff --git a/src/com/android/settings/dashboard/SearchResultsSummary.java b/src/com/android/settings/dashboard/SearchResultsSummary.java
index 862b53b..1bd0bf0 100644
--- a/src/com/android/settings/dashboard/SearchResultsSummary.java
+++ b/src/com/android/settings/dashboard/SearchResultsSummary.java
@@ -51,7 +51,7 @@
     private static final String EMPTY_QUERY = "";
     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;
 
@@ -68,7 +68,7 @@
 
     private String mQuery;
 
-    private boolean mShowOnlyResults;
+    private boolean mShowResults;
 
     /**
      * A basic AsyncTask for updating the query results cursor
@@ -83,6 +83,7 @@
         protected void onPostExecute(Cursor cursor) {
             if (!isCancelled()) {
                 setResultsCursor(cursor);
+                setResultsVisibility(cursor.getCount() > 0);
             } else if (cursor != null) {
                 cursor.close();
             }
@@ -102,6 +103,7 @@
         protected void onPostExecute(Cursor cursor) {
             if (!isCancelled()) {
                 setSuggestionsCursor(cursor);
+                setSuggestionsVisibility(cursor.getCount() > 0);
             } else if (cursor != null) {
                 cursor.close();
             }
@@ -116,7 +118,7 @@
         mSuggestionsAdapter = new SuggestionsAdapter(getActivity());
 
         if (savedInstanceState != null) {
-            mShowOnlyResults = savedInstanceState.getBoolean(SAVE_KEY_SHOW_ONLY_RESULTS);
+            mShowResults = savedInstanceState.getBoolean(SAVE_KEY_SHOW_RESULTS);
         }
     }
 
@@ -124,7 +126,7 @@
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
 
-        outState.putBoolean(SAVE_KEY_SHOW_ONLY_RESULTS, mShowOnlyResults);
+        outState.putBoolean(SAVE_KEY_SHOW_RESULTS, mShowResults);
     }
 
     @Override
@@ -209,10 +211,9 @@
                 final Cursor cursor = mSuggestionsAdapter.mCursor;
                 cursor.moveToPosition(position);
 
+                mShowResults = true;
                 mQuery = cursor.getString(0);
                 mSearchView.setQuery(mQuery, false);
-                setSuggestionsVisibility(false);
-                mShowOnlyResults = true;
             }
         });
 
@@ -220,10 +221,10 @@
     }
 
     @Override
-    public void onActivityCreated(Bundle savedInstanceState) {
-        super.onActivityCreated(savedInstanceState);
+    public void onResume() {
+        super.onResume();
 
-        if (!mShowOnlyResults) {
+        if (!mShowResults) {
             showSomeSuggestions();
         }
     }
@@ -250,31 +251,28 @@
 
     public boolean onQueryTextSubmit(String query) {
         mQuery = getFilteredQueryString(query);
-        setSuggestionsVisibility(!mShowOnlyResults);
+        mShowResults = true;
+        setSuggestionsVisibility(false);
         updateSearchResults();
+        saveQueryToDatabase();
         return true;
     }
 
     public boolean onQueryTextChange(String query) {
         final String newQuery = getFilteredQueryString(query);
 
-        boolean isNewQuery;
-        if (!TextUtils.isEmpty(mQuery)) {
-            isNewQuery = !mQuery.equals(query);
-        } else {
-            isNewQuery = !TextUtils.isEmpty(query);
-        }
-
         mQuery = newQuery;
 
-        if (isNewQuery) {
-            mShowOnlyResults = false;
-        }
-        if (!mShowOnlyResults) {
+        if (TextUtils.isEmpty(mQuery)) {
+            mShowResults = false;
+            setResultsVisibility(false);
             updateSuggestions();
+        } else {
+            mShowResults = true;
+            setSuggestionsVisibility(false);
+            updateSearchResults();
         }
 
-        updateSearchResults();
         return true;
     }
 
@@ -335,30 +333,33 @@
         return filtered.toString();
     }
 
-    private void updateSuggestions() {
+    private void clearAllTasks() {
+        if (mUpdateSearchResultsTask != null) {
+            mUpdateSearchResultsTask.cancel(false);
+            mUpdateSearchResultsTask = null;
+        }
         if (mUpdateSuggestionsTask != null) {
             mUpdateSuggestionsTask.cancel(false);
             mUpdateSuggestionsTask = null;
         }
+    }
+
+    private void updateSuggestions() {
+        clearAllTasks();
         if (mQuery == null) {
             setSuggestionsCursor(null);
         } else {
-            setSuggestionsVisibility(true);
             mUpdateSuggestionsTask = new UpdateSuggestionsTask();
             mUpdateSuggestionsTask.execute(mQuery);
         }
     }
 
     private void updateSearchResults() {
-        if (mUpdateSearchResultsTask != null) {
-            mUpdateSearchResultsTask.cancel(false);
-            mUpdateSearchResultsTask = null;
-        }
+        clearAllTasks();
         if (TextUtils.isEmpty(mQuery)) {
             setResultsVisibility(false);
             setResultsCursor(null);
         } else {
-            setResultsVisibility(true);
             mUpdateSearchResultsTask = new UpdateSearchResultsTask();
             mUpdateSearchResultsTask.execute(mQuery);
         }