DelayingSuggestionsAdapter.java revision b83882b9efa37ec0f20a0f1c85cf5ccc93194aee
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.Promoter;
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(Promoter promoter, SuggestionViewFactory viewFactory,
40            int maxPromoted) {
41        super(promoter, viewFactory, maxPromoted);
42    }
43
44    @Override
45    public void close() {
46        setPendingSuggestions(null);
47        super.close();
48    }
49
50    @Override
51    public void setSuggestions(Suggestions suggestions) {
52        if (suggestions == null) {
53            super.setSuggestions(null);
54            setPendingSuggestions(null);
55            return;
56        }
57        if (shouldPublish(suggestions)) {
58            if (DBG) Log.d(TAG, "Publishing suggestions immediately: " + suggestions);
59            super.setSuggestions(suggestions);
60            // Clear any old pending suggestions.
61            setPendingSuggestions(null);
62        } else {
63            if (DBG) Log.d(TAG, "Delaying suggestions publishing: " + suggestions);
64            setPendingSuggestions(suggestions);
65        }
66    }
67
68    /**
69     * Gets whether the given suggestions are non-empty for the selected source.
70     */
71    private boolean shouldPublish(Suggestions suggestions) {
72        if (suggestions.isDone()) return true;
73        SuggestionCursor cursor = getCorpusCursor(suggestions, getCorpus());
74        return cursor != null && cursor.getCount() > 0;
75    }
76
77    private void setPendingSuggestions(Suggestions suggestions) {
78        if (mPendingSuggestions == suggestions) {
79            return;
80        }
81        if (isClosed()) {
82            if (suggestions != null) {
83                suggestions.release();
84            }
85            return;
86        }
87        if (mPendingDataSetObserver == null) {
88            mPendingDataSetObserver = new PendingSuggestionsObserver();
89        }
90        if (mPendingSuggestions != null) {
91            mPendingSuggestions.unregisterDataSetObserver(mPendingDataSetObserver);
92            // Close old suggestions, but only if they are not also the current
93            // suggestions.
94            if (mPendingSuggestions != getSuggestions()) {
95                mPendingSuggestions.release();
96            }
97        }
98        mPendingSuggestions = suggestions;
99        if (mPendingSuggestions != null) {
100            mPendingSuggestions.registerDataSetObserver(mPendingDataSetObserver);
101        }
102    }
103
104    protected void onPendingSuggestionsChanged() {
105        if (DBG) {
106            Log.d(TAG, "onPendingSuggestionsChanged(), mPendingSuggestions="
107                    + mPendingSuggestions);
108        }
109        if (shouldPublish(mPendingSuggestions)) {
110            if (DBG) Log.d(TAG, "Suggestions now available, publishing: " + mPendingSuggestions);
111            super.setSuggestions(mPendingSuggestions);
112            // The suggestions are no longer pending.
113            setPendingSuggestions(null);
114        }
115    }
116
117    private class PendingSuggestionsObserver extends DataSetObserver {
118        @Override
119        public void onChanged() {
120            onPendingSuggestionsChanged();
121        }
122    }
123
124}
125