DelayingSuggestionsAdapter.java revision b83882b9efa37ec0f20a0f1c85cf5ccc93194aee
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 19b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport com.android.quicksearchbox.Promoter; 20816804b67619af133860a1e28e92f58bc642260dBjorn Bringertimport com.android.quicksearchbox.SuggestionCursor; 21816804b67619af133860a1e28e92f58bc642260dBjorn Bringertimport com.android.quicksearchbox.Suggestions; 22816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 23816804b67619af133860a1e28e92f58bc642260dBjorn Bringertimport android.database.DataSetObserver; 24816804b67619af133860a1e28e92f58bc642260dBjorn Bringertimport android.util.Log; 25816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 26816804b67619af133860a1e28e92f58bc642260dBjorn Bringert/** 27816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * A {@link SuggestionsAdapter} that doesn't expose the new suggestions 28816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * until there are some results to show. 29816804b67619af133860a1e28e92f58bc642260dBjorn Bringert */ 30816804b67619af133860a1e28e92f58bc642260dBjorn Bringertpublic class DelayingSuggestionsAdapter extends SuggestionsAdapter { 31816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 32b5fc08b7f16a32d3865f44b7f26d8aaa5304a2adBjorn Bringert private static final boolean DBG = false; 33816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private static final String TAG = "QSB.DelayingSuggestionsAdapter"; 34816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 35816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private DataSetObserver mPendingDataSetObserver; 36816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 37816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private Suggestions mPendingSuggestions; 38816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 39b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert public DelayingSuggestionsAdapter(Promoter promoter, SuggestionViewFactory viewFactory, 40b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert int maxPromoted) { 41b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert super(promoter, viewFactory, maxPromoted); 42816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 43816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 44fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert @Override 45816804b67619af133860a1e28e92f58bc642260dBjorn Bringert public void close() { 46816804b67619af133860a1e28e92f58bc642260dBjorn Bringert setPendingSuggestions(null); 47816804b67619af133860a1e28e92f58bc642260dBjorn Bringert super.close(); 48816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 49816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 50fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert @Override 51816804b67619af133860a1e28e92f58bc642260dBjorn Bringert public void setSuggestions(Suggestions suggestions) { 52816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (suggestions == null) { 53816804b67619af133860a1e28e92f58bc642260dBjorn Bringert super.setSuggestions(null); 54816804b67619af133860a1e28e92f58bc642260dBjorn Bringert setPendingSuggestions(null); 55816804b67619af133860a1e28e92f58bc642260dBjorn Bringert return; 56816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 57816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (shouldPublish(suggestions)) { 58816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (DBG) Log.d(TAG, "Publishing suggestions immediately: " + suggestions); 59816804b67619af133860a1e28e92f58bc642260dBjorn Bringert super.setSuggestions(suggestions); 60816804b67619af133860a1e28e92f58bc642260dBjorn Bringert // Clear any old pending suggestions. 61816804b67619af133860a1e28e92f58bc642260dBjorn Bringert setPendingSuggestions(null); 62816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } else { 63816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (DBG) Log.d(TAG, "Delaying suggestions publishing: " + suggestions); 64816804b67619af133860a1e28e92f58bc642260dBjorn Bringert setPendingSuggestions(suggestions); 65816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 66816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 67816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 68816804b67619af133860a1e28e92f58bc642260dBjorn Bringert /** 69816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * Gets whether the given suggestions are non-empty for the selected source. 70816804b67619af133860a1e28e92f58bc642260dBjorn Bringert */ 71816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private boolean shouldPublish(Suggestions suggestions) { 72816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (suggestions.isDone()) return true; 73fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert SuggestionCursor cursor = getCorpusCursor(suggestions, getCorpus()); 74816804b67619af133860a1e28e92f58bc642260dBjorn Bringert return cursor != null && cursor.getCount() > 0; 75816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 76816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 77816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private void setPendingSuggestions(Suggestions suggestions) { 78816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (mPendingSuggestions == suggestions) { 79816804b67619af133860a1e28e92f58bc642260dBjorn Bringert return; 80816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 81816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (isClosed()) { 82816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (suggestions != null) { 83b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert suggestions.release(); 84816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 85816804b67619af133860a1e28e92f58bc642260dBjorn Bringert return; 86816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 87816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (mPendingDataSetObserver == null) { 88816804b67619af133860a1e28e92f58bc642260dBjorn Bringert mPendingDataSetObserver = new PendingSuggestionsObserver(); 89816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 90816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (mPendingSuggestions != null) { 91816804b67619af133860a1e28e92f58bc642260dBjorn Bringert mPendingSuggestions.unregisterDataSetObserver(mPendingDataSetObserver); 92816804b67619af133860a1e28e92f58bc642260dBjorn Bringert // Close old suggestions, but only if they are not also the current 93816804b67619af133860a1e28e92f58bc642260dBjorn Bringert // suggestions. 94816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (mPendingSuggestions != getSuggestions()) { 95b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert mPendingSuggestions.release(); 96816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 97816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 98816804b67619af133860a1e28e92f58bc642260dBjorn Bringert mPendingSuggestions = suggestions; 99816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (mPendingSuggestions != null) { 100816804b67619af133860a1e28e92f58bc642260dBjorn Bringert mPendingSuggestions.registerDataSetObserver(mPendingDataSetObserver); 101816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 102816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 103816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 104816804b67619af133860a1e28e92f58bc642260dBjorn Bringert protected void onPendingSuggestionsChanged() { 105816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (DBG) { 106816804b67619af133860a1e28e92f58bc642260dBjorn Bringert Log.d(TAG, "onPendingSuggestionsChanged(), mPendingSuggestions=" 107816804b67619af133860a1e28e92f58bc642260dBjorn Bringert + mPendingSuggestions); 108816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 109816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (shouldPublish(mPendingSuggestions)) { 110816804b67619af133860a1e28e92f58bc642260dBjorn Bringert if (DBG) Log.d(TAG, "Suggestions now available, publishing: " + mPendingSuggestions); 111816804b67619af133860a1e28e92f58bc642260dBjorn Bringert super.setSuggestions(mPendingSuggestions); 112816804b67619af133860a1e28e92f58bc642260dBjorn Bringert // The suggestions are no longer pending. 113816804b67619af133860a1e28e92f58bc642260dBjorn Bringert setPendingSuggestions(null); 114816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 115816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 116816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 117816804b67619af133860a1e28e92f58bc642260dBjorn Bringert private class PendingSuggestionsObserver extends DataSetObserver { 118816804b67619af133860a1e28e92f58bc642260dBjorn Bringert @Override 119816804b67619af133860a1e28e92f58bc642260dBjorn Bringert public void onChanged() { 120816804b67619af133860a1e28e92f58bc642260dBjorn Bringert onPendingSuggestionsChanged(); 121816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 122816804b67619af133860a1e28e92f58bc642260dBjorn Bringert } 123816804b67619af133860a1e28e92f58bc642260dBjorn Bringert 124816804b67619af133860a1e28e92f58bc642260dBjorn Bringert} 125