DelayingSuggestionsAdapter.java revision 7a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24
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
19fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwoodimport com.android.quicksearchbox.Promoter;
20816804b67619af133860a1e28e92f58bc642260dBjorn Bringertimport com.android.quicksearchbox.SuggestionCursor;
217a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwoodimport com.android.quicksearchbox.SuggestionPosition;
22816804b67619af133860a1e28e92f58bc642260dBjorn Bringertimport com.android.quicksearchbox.Suggestions;
23816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
24816804b67619af133860a1e28e92f58bc642260dBjorn Bringertimport android.database.DataSetObserver;
25816804b67619af133860a1e28e92f58bc642260dBjorn Bringertimport android.util.Log;
26fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwoodimport android.view.View.OnFocusChangeListener;
27816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
28816804b67619af133860a1e28e92f58bc642260dBjorn Bringert/**
29fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood * A {@link SuggestionsListAdapter} that doesn't expose the new suggestions
30816804b67619af133860a1e28e92f58bc642260dBjorn Bringert * until there are some results to show.
31816804b67619af133860a1e28e92f58bc642260dBjorn Bringert */
32fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwoodpublic class DelayingSuggestionsAdapter<A> implements SuggestionsAdapter<A> {
33816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
34b5fc08b7f16a32d3865f44b7f26d8aaa5304a2adBjorn Bringert    private static final boolean DBG = false;
35816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    private static final String TAG = "QSB.DelayingSuggestionsAdapter";
36816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
37816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    private DataSetObserver mPendingDataSetObserver;
38816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
39816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    private Suggestions mPendingSuggestions;
40816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
41fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    private final SuggestionsAdapterBase<A> mDelayedAdapter;
42fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
43fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    public DelayingSuggestionsAdapter(SuggestionsAdapterBase<A> delayed) {
44fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        mDelayedAdapter = delayed;
45816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    }
46816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
47816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    public void close() {
48816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        setPendingSuggestions(null);
49fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        mDelayedAdapter.close();
50816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    }
51816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
52fde948e69f59589cf0d217ea414af7947de600bbBjorn Bringert    @Override
53816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    public void setSuggestions(Suggestions suggestions) {
54816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        if (suggestions == null) {
55fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood            mDelayedAdapter.setSuggestions(null);
56816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            setPendingSuggestions(null);
57816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            return;
58816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        }
59816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        if (shouldPublish(suggestions)) {
60816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            if (DBG) Log.d(TAG, "Publishing suggestions immediately: " + suggestions);
61fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood            mDelayedAdapter.setSuggestions(suggestions);
62816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            // Clear any old pending suggestions.
63816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            setPendingSuggestions(null);
64816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        } else {
65816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            if (DBG) Log.d(TAG, "Delaying suggestions publishing: " + suggestions);
66816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            setPendingSuggestions(suggestions);
67816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        }
68816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    }
69816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
70816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    /**
71816804b67619af133860a1e28e92f58bc642260dBjorn Bringert     * Gets whether the given suggestions are non-empty for the selected source.
72816804b67619af133860a1e28e92f58bc642260dBjorn Bringert     */
73816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    private boolean shouldPublish(Suggestions suggestions) {
74816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        if (suggestions.isDone()) return true;
75fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        SuggestionCursor cursor = mDelayedAdapter.getPromoted(suggestions);
76816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        return cursor != null && cursor.getCount() > 0;
77816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    }
78816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
79816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    private void setPendingSuggestions(Suggestions suggestions) {
80816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        if (mPendingSuggestions == suggestions) {
81816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            return;
82816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        }
83fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        if (mDelayedAdapter.isClosed()) {
84816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            if (suggestions != null) {
85b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert                suggestions.release();
86816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            }
87816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            return;
88816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        }
89816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        if (mPendingDataSetObserver == null) {
90816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            mPendingDataSetObserver = new PendingSuggestionsObserver();
91816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        }
92816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        if (mPendingSuggestions != null) {
93816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            mPendingSuggestions.unregisterDataSetObserver(mPendingDataSetObserver);
94816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            // Close old suggestions, but only if they are not also the current
95816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            // suggestions.
96816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            if (mPendingSuggestions != getSuggestions()) {
97b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert                mPendingSuggestions.release();
98816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            }
99816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        }
100816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        mPendingSuggestions = suggestions;
101816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        if (mPendingSuggestions != null) {
102816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            mPendingSuggestions.registerDataSetObserver(mPendingDataSetObserver);
103816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        }
104816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    }
105816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
106816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    protected void onPendingSuggestionsChanged() {
107816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        if (DBG) {
108816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            Log.d(TAG, "onPendingSuggestionsChanged(), mPendingSuggestions="
109816804b67619af133860a1e28e92f58bc642260dBjorn Bringert                    + mPendingSuggestions);
110816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        }
111816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        if (shouldPublish(mPendingSuggestions)) {
112816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            if (DBG) Log.d(TAG, "Suggestions now available, publishing: " + mPendingSuggestions);
113fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood            mDelayedAdapter.setSuggestions(mPendingSuggestions);
114816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            // The suggestions are no longer pending.
115816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            setPendingSuggestions(null);
116816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        }
117816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    }
118816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
119816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    private class PendingSuggestionsObserver extends DataSetObserver {
120816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        @Override
121816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        public void onChanged() {
122816804b67619af133860a1e28e92f58bc642260dBjorn Bringert            onPendingSuggestionsChanged();
123816804b67619af133860a1e28e92f58bc642260dBjorn Bringert        }
124816804b67619af133860a1e28e92f58bc642260dBjorn Bringert    }
125816804b67619af133860a1e28e92f58bc642260dBjorn Bringert
126fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    public A getListAdapter() {
127fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        return mDelayedAdapter.getListAdapter();
128fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
129fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
130fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    public SuggestionCursor getCurrentPromotedSuggestions() {
131fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        return mDelayedAdapter.getCurrentPromotedSuggestions();
132fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
133fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
134fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    public Suggestions getSuggestions() {
135fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        return mDelayedAdapter.getSuggestions();
136fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
137fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
1387a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood    public SuggestionPosition getSuggestion(long suggestionId) {
1397a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood        return mDelayedAdapter.getSuggestion(suggestionId);
140fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
141fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
1427a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood    public void onSuggestionClicked(long suggestionId) {
1437a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood        mDelayedAdapter.onSuggestionClicked(suggestionId);
144fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
145fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
1467a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood    public void onSuggestionQueryRefineClicked(long suggestionId) {
1477a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood        mDelayedAdapter.onSuggestionQueryRefineClicked(suggestionId);
148fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
149fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
1507a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood    public void onSuggestionQuickContactClicked(long suggestionId) {
1517a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood        mDelayedAdapter.onSuggestionQuickContactClicked(suggestionId);
1527a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood    }
1537a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood
1547a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood    public void onSuggestionRemoveFromHistoryClicked(long suggestionId) {
1557a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood        mDelayedAdapter.onSuggestionRemoveFromHistoryClicked(suggestionId);
156fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
157fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
158fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    public void setMaxPromoted(int maxPromoted) {
159fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        mDelayedAdapter.setMaxPromoted(maxPromoted);
160fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
161fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
162fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    public void setOnFocusChangeListener(OnFocusChangeListener l) {
163fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        mDelayedAdapter.setOnFocusChangeListener(l);
164fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
165fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
166fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    @Override
167fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    public void setPromoter(Promoter promoter) {
168fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        mDelayedAdapter.setPromoter(promoter);
169fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
170fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
171fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    public void setSuggestionAdapterChangeListener(SuggestionsAdapterChangeListener l) {
172fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        mDelayedAdapter.setSuggestionAdapterChangeListener(l);
173fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
174fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
175fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    public void setSuggestionClickListener(SuggestionClickListener listener) {
176fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        mDelayedAdapter.setSuggestionClickListener(listener);
177fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
178fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
179fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    public void setIcon1Enabled(boolean enabled) {
180fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood        mDelayedAdapter.setIcon1Enabled(enabled);
181fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood    }
182fd4a4cbc1143a734d357897531daa7105db6459bMathew Inwood
1837a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood    @Override
1847a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood    public boolean isEmpty() {
1857a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood        return mDelayedAdapter.isEmpty();
1867a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood    }
1877a0c3a7c6fdabce949b59e0a2c6ec1d44a140c24Mathew Inwood
188816804b67619af133860a1e28e92f58bc642260dBjorn Bringert}
189