DelayingSuggestionsAdapter.java revision 816804b67619af133860a1e28e92f58bc642260d
1816804b67619af133860a1e28e92f58bc642260dBjorn Bringert/* 2816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * Copyright (C) 2009 The Android Open Source Project 3816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * 4816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 5816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * you may not use this file except in compliance with the License. 6816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * You may obtain a copy of the License at 7816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * 8816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 9816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * 10816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * Unless required by applicable law or agreed to in writing, software 11816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 12816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * See the License for the specific language governing permissions and 14816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * limitations under the License. 15816804b67619af133860a1e28e92f58bc642260dBjorn Bringert */ 16816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 17816804b67619af133860a1e28e92f58bc642260dBjorn Bringertpackage com.android.quicksearchbox.ui; 18816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 19816804b67619af133860a1e28e92f58bc642260dBjorn Bringertimport com.android.quicksearchbox.SuggestionCursor; 20816804b67619af133860a1e28e92f58bc642260dBjorn Bringertimport com.android.quicksearchbox.Suggestions; 21816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 22816804b67619af133860a1e28e92f58bc642260dBjorn Bringertimport android.database.DataSetObserver; 23816804b67619af133860a1e28e92f58bc642260dBjorn Bringertimport android.util.Log; 24816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 25816804b67619af133860a1e28e92f58bc642260dBjorn Bringert/** 26816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * A {@link SuggestionsAdapter} that doesn't expose the new suggestions 27816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * until there are some results to show. 28816804b67619af133860a1e28e92f58bc642260dBjorn Bringert */ 29816804b67619af133860a1e28e92f58bc642260dBjorn Bringertpublic class DelayingSuggestionsAdapter extends SuggestionsAdapter { 30816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 31816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private static final boolean DBG = true; 32816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private static final String TAG = "QSB.DelayingSuggestionsAdapter"; 33816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 34816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private DataSetObserver mPendingDataSetObserver; 35816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 36816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private Suggestions mPendingSuggestions; 37816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 38816804b67619af133860a1e28e92f58bc642260dBjorn Bringert public DelayingSuggestionsAdapter(SuggestionViewFactory viewFactory) { 39816804b67619af133860a1e28e92f58bc642260dBjorn Bringert super(viewFactory); 40816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 41816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 42816804b67619af133860a1e28e92f58bc642260dBjorn Bringert public void close() { 43816804b67619af133860a1e28e92f58bc642260dBjorn Bringert setPendingSuggestions(null); 44816804b67619af133860a1e28e92f58bc642260dBjorn Bringert super.close(); 45816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 46816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 47816804b67619af133860a1e28e92f58bc642260dBjorn Bringert public void setSuggestions(Suggestions suggestions) { 48816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (suggestions == null) { 49816804b67619af133860a1e28e92f58bc642260dBjorn Bringert super.setSuggestions(null); 50816804b67619af133860a1e28e92f58bc642260dBjorn Bringert setPendingSuggestions(null); 51816804b67619af133860a1e28e92f58bc642260dBjorn Bringert return; 52816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 53816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (shouldPublish(suggestions)) { 54816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (DBG) Log.d(TAG, "Publishing suggestions immediately: " + suggestions); 55816804b67619af133860a1e28e92f58bc642260dBjorn Bringert super.setSuggestions(suggestions); 56816804b67619af133860a1e28e92f58bc642260dBjorn Bringert // Clear any old pending suggestions. 57816804b67619af133860a1e28e92f58bc642260dBjorn Bringert setPendingSuggestions(null); 58816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } else { 59816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (DBG) Log.d(TAG, "Delaying suggestions publishing: " + suggestions); 60816804b67619af133860a1e28e92f58bc642260dBjorn Bringert setPendingSuggestions(suggestions); 61816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 62816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 63816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 64816804b67619af133860a1e28e92f58bc642260dBjorn Bringert /** 65816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * Gets whether the given suggestions are non-empty for the selected source. 66816804b67619af133860a1e28e92f58bc642260dBjorn Bringert */ 67816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private boolean shouldPublish(Suggestions suggestions) { 68816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (suggestions.isDone()) return true; 69816804b67619af133860a1e28e92f58bc642260dBjorn Bringert SuggestionCursor cursor = getSourceCursor(suggestions, getSource()); 70816804b67619af133860a1e28e92f58bc642260dBjorn Bringert return cursor != null && cursor.getCount() > 0; 71816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 72816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 73816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private void setPendingSuggestions(Suggestions suggestions) { 74816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (mPendingSuggestions == suggestions) { 75816804b67619af133860a1e28e92f58bc642260dBjorn Bringert return; 76816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 77816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (isClosed()) { 78816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (suggestions != null) { 79816804b67619af133860a1e28e92f58bc642260dBjorn Bringert suggestions.close(); 80816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 81816804b67619af133860a1e28e92f58bc642260dBjorn Bringert return; 82816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 83816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (mPendingDataSetObserver == null) { 84816804b67619af133860a1e28e92f58bc642260dBjorn Bringert mPendingDataSetObserver = new PendingSuggestionsObserver(); 85816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 86816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (mPendingSuggestions != null) { 87816804b67619af133860a1e28e92f58bc642260dBjorn Bringert mPendingSuggestions.unregisterDataSetObserver(mPendingDataSetObserver); 88816804b67619af133860a1e28e92f58bc642260dBjorn Bringert // Close old suggestions, but only if they are not also the current 89816804b67619af133860a1e28e92f58bc642260dBjorn Bringert // suggestions. 90816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (mPendingSuggestions != getSuggestions()) { 91816804b67619af133860a1e28e92f58bc642260dBjorn Bringert mPendingSuggestions.close(); 92816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 93816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 94816804b67619af133860a1e28e92f58bc642260dBjorn Bringert mPendingSuggestions = suggestions; 95816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (mPendingSuggestions != null) { 96816804b67619af133860a1e28e92f58bc642260dBjorn Bringert mPendingSuggestions.registerDataSetObserver(mPendingDataSetObserver); 97816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 98816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 99816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 100816804b67619af133860a1e28e92f58bc642260dBjorn Bringert protected void onPendingSuggestionsChanged() { 101816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (DBG) { 102816804b67619af133860a1e28e92f58bc642260dBjorn Bringert Log.d(TAG, "onPendingSuggestionsChanged(), mPendingSuggestions=" 103816804b67619af133860a1e28e92f58bc642260dBjorn Bringert + mPendingSuggestions); 104816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 105816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (shouldPublish(mPendingSuggestions)) { 106816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (DBG) Log.d(TAG, "Suggestions now available, publishing: " + mPendingSuggestions); 107816804b67619af133860a1e28e92f58bc642260dBjorn Bringert super.setSuggestions(mPendingSuggestions); 108816804b67619af133860a1e28e92f58bc642260dBjorn Bringert // The suggestions are no longer pending. 109816804b67619af133860a1e28e92f58bc642260dBjorn Bringert setPendingSuggestions(null); 110816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 111816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 112816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 113816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private class PendingSuggestionsObserver extends DataSetObserver { 114816804b67619af133860a1e28e92f58bc642260dBjorn Bringert @Override 115816804b67619af133860a1e28e92f58bc642260dBjorn Bringert public void onChanged() { 116816804b67619af133860a1e28e92f58bc642260dBjorn Bringert onPendingSuggestionsChanged(); 117816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 118816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 119816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 120816804b67619af133860a1e28e92f58bc642260dBjorn Bringert} 121