Clear suggestions immediately when swicthing between corpora.

When switching from All to a specific corpora, the suggestions are filtered so that the results are available immediately. When switching from a specific corpora to All, we leave the suggestions in place, since they are still appropriate. When switching between individual seperate corpora, all suggestions are cleared.

Bug: 2750930

Change-Id: Ie284dee7bd93ebf048d86f025b4c30111f3dd403
diff --git a/src/com/android/quicksearchbox/SearchActivity.java b/src/com/android/quicksearchbox/SearchActivity.java
index 78f7944..6898ea1 100644
--- a/src/com/android/quicksearchbox/SearchActivity.java
+++ b/src/com/android/quicksearchbox/SearchActivity.java
@@ -54,7 +54,7 @@
  */
 public class SearchActivity extends Activity {
 
-    private static final boolean DBG = true;
+    private static final boolean DBG = false;
     private static final String TAG = "QSB.SearchActivity";
     private static final boolean TRACE = false;
 
diff --git a/src/com/android/quicksearchbox/ShortcutsProvider.java b/src/com/android/quicksearchbox/ShortcutsProvider.java
index acc1822..a8bed1c 100644
--- a/src/com/android/quicksearchbox/ShortcutsProvider.java
+++ b/src/com/android/quicksearchbox/ShortcutsProvider.java
@@ -33,7 +33,7 @@
  */
 public class ShortcutsProvider extends ContentProvider {
 
-    private static final boolean DBG = true;
+    private static final boolean DBG = false;
     private static final String TAG = "QSB.ExternalShortcutReceiver";
 
     public static final String EXTRA_SHORTCUT_SOURCE = "shortcut_source";
diff --git a/src/com/android/quicksearchbox/Suggestions.java b/src/com/android/quicksearchbox/Suggestions.java
index 94fd841..3e8ce2a 100644
--- a/src/com/android/quicksearchbox/Suggestions.java
+++ b/src/com/android/quicksearchbox/Suggestions.java
@@ -23,6 +23,7 @@
 import android.util.Log;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -40,7 +41,7 @@
     private final String mQuery;
 
     /** The sources that are expected to report. */
-    private final List<Corpus> mExpectedCorpora;
+    private List<Corpus> mExpectedCorpora;
 
     /**
      * The observers that want notifications of changes to the published suggestions.
@@ -53,7 +54,7 @@
      * in the order that they were published.
      * This object may only be accessed on the UI thread.
      * */
-    private final ArrayList<CorpusResult> mCorpusResults;
+    private ArrayList<CorpusResult> mCorpusResults;
 
     private SuggestionCursor mShortcuts;
 
@@ -69,14 +70,14 @@
     /**
      * Creates a new empty Suggestions.
      *
-     * @param expectedCorpusCount The number of sources that are expected to report.
+     * @param expectedCorpora The sources that are expected to report.
      */
     public Suggestions(Promoter promoter, int maxPromoted,
-            String query, List<Corpus> expectedCorppra) {
+            String query, List<Corpus> expectedCorpora) {
         mPromoter = promoter;
         mMaxPromoted = maxPromoted;
         mQuery = query;
-        mExpectedCorpora = expectedCorppra;
+        mExpectedCorpora = expectedCorpora;
         mCorpusResults = new ArrayList<CorpusResult>(mExpectedCorpora.size());
         mPromoted = null;  // will be set by updatePromoted()
     }
@@ -235,6 +236,35 @@
         return mCorpusResults == null ? 0 : mCorpusResults.size();
     }
 
+    public void filterByCorpus(Corpus singleCorpus) {
+        if ((mExpectedCorpora.size() == 1) && (mExpectedCorpora.get(0) == this)) {
+            return;
+        }
+        boolean haveCorpus = false;
+        for (Corpus corpus : mExpectedCorpora) {
+            if (corpus == singleCorpus) {
+                haveCorpus = true;
+            }
+        }
+        if (!haveCorpus) {
+            mExpectedCorpora = Collections.emptyList();
+            mPromoted = null;
+            mCorpusResults.clear();
+            notifyDataSetChanged();
+            return;
+        }
+        mExpectedCorpora = Collections.singletonList(singleCorpus);
+        ArrayList<CorpusResult> filteredResults = new ArrayList<CorpusResult>(1);
+        for (CorpusResult result : mCorpusResults) {
+            if (result.getCorpus() == singleCorpus) {
+                filteredResults.add(result);
+            }
+        }
+        mCorpusResults = filteredResults;
+        mPromoted = null;
+        notifyDataSetChanged();
+    }
+
     private class MyShortcutsObserver extends DataSetObserver {
         @Override
         public void onChanged() {
diff --git a/src/com/android/quicksearchbox/ui/SuggestionsAdapter.java b/src/com/android/quicksearchbox/ui/SuggestionsAdapter.java
index dc8d6ad..ec7790d 100644
--- a/src/com/android/quicksearchbox/ui/SuggestionsAdapter.java
+++ b/src/com/android/quicksearchbox/ui/SuggestionsAdapter.java
@@ -101,6 +101,21 @@
      * Sets the source whose results are displayed.
      */
     public void setCorpus(Corpus corpus) {
+        if (mSuggestions != null) {
+            if ((mCorpus == null) && (corpus != null)) {
+                // we've just switched from the 'All' corpus to a specific corpus
+                // we can filter the existing results immediately.
+                if (DBG) Log.v(TAG, "setCorpus(" + corpus.getName() + ") Filter suggestions");
+                mSuggestions.filterByCorpus(corpus);
+            } else if (corpus != null) {
+                // Note, when switching from a specific corpus to 'All' we do not change the
+                // suggestions, since they're still relevant for 'All'. Hence 'corpus != null'
+                if (DBG) Log.v(TAG, "setCorpus(" + corpus.getName() + ") Clear suggestions");
+                mSuggestions.unregisterDataSetObserver(mDataSetObserver);
+                mSuggestions.close();
+                mSuggestions = null;
+            }
+        }
         mCorpus = corpus;
         onSuggestionsChanged();
     }