DelayingSuggestionsAdapter.java revision 5229b06f00d20aac76cd8519b37f2a579d61c54f
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.quicksearchbox.ui; 18 19import com.android.quicksearchbox.Corpora; 20import com.android.quicksearchbox.SuggestionCursor; 21import com.android.quicksearchbox.Suggestions; 22 23import android.database.DataSetObserver; 24import android.util.Log; 25 26/** 27 * A {@link SuggestionsAdapter} that doesn't expose the new suggestions 28 * until there are some results to show. 29 */ 30public class DelayingSuggestionsAdapter extends SuggestionsAdapter { 31 32 private static final boolean DBG = false; 33 private static final String TAG = "QSB.DelayingSuggestionsAdapter"; 34 35 private DataSetObserver mPendingDataSetObserver; 36 37 private Suggestions mPendingSuggestions; 38 39 public DelayingSuggestionsAdapter(SuggestionViewFactory fallbackFactory, Corpora corpora) { 40 super(fallbackFactory, corpora); 41 } 42 43 @Override 44 public void close() { 45 setPendingSuggestions(null); 46 super.close(); 47 } 48 49 @Override 50 public void setSuggestions(Suggestions suggestions) { 51 if (suggestions == null) { 52 super.setSuggestions(null); 53 setPendingSuggestions(null); 54 return; 55 } 56 if (shouldPublish(suggestions)) { 57 if (DBG) Log.d(TAG, "Publishing suggestions immediately: " + suggestions); 58 super.setSuggestions(suggestions); 59 // Clear any old pending suggestions. 60 setPendingSuggestions(null); 61 } else { 62 if (DBG) Log.d(TAG, "Delaying suggestions publishing: " + suggestions); 63 setPendingSuggestions(suggestions); 64 } 65 } 66 67 /** 68 * Gets whether the given suggestions are non-empty for the selected source. 69 */ 70 private boolean shouldPublish(Suggestions suggestions) { 71 if (suggestions.isDone()) return true; 72 SuggestionCursor cursor = getPromoted(suggestions, getCorpus()); 73 return cursor != null && cursor.getCount() > 0; 74 } 75 76 private void setPendingSuggestions(Suggestions suggestions) { 77 if (mPendingSuggestions == suggestions) { 78 return; 79 } 80 if (isClosed()) { 81 if (suggestions != null) { 82 suggestions.release(); 83 } 84 return; 85 } 86 if (mPendingDataSetObserver == null) { 87 mPendingDataSetObserver = new PendingSuggestionsObserver(); 88 } 89 if (mPendingSuggestions != null) { 90 mPendingSuggestions.unregisterDataSetObserver(mPendingDataSetObserver); 91 // Close old suggestions, but only if they are not also the current 92 // suggestions. 93 if (mPendingSuggestions != getSuggestions()) { 94 mPendingSuggestions.release(); 95 } 96 } 97 mPendingSuggestions = suggestions; 98 if (mPendingSuggestions != null) { 99 mPendingSuggestions.registerDataSetObserver(mPendingDataSetObserver); 100 } 101 } 102 103 protected void onPendingSuggestionsChanged() { 104 if (DBG) { 105 Log.d(TAG, "onPendingSuggestionsChanged(), mPendingSuggestions=" 106 + mPendingSuggestions); 107 } 108 if (shouldPublish(mPendingSuggestions)) { 109 if (DBG) Log.d(TAG, "Suggestions now available, publishing: " + mPendingSuggestions); 110 super.setSuggestions(mPendingSuggestions); 111 // The suggestions are no longer pending. 112 setPendingSuggestions(null); 113 } 114 } 115 116 private class PendingSuggestionsObserver extends DataSetObserver { 117 @Override 118 public void onChanged() { 119 onPendingSuggestionsChanged(); 120 } 121 } 122 123} 124