SuggestionsAdapter.java revision ca78085bb2127559e6f55276a307bfa857018eca
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.SuggestionCursor;
20import com.android.quicksearchbox.SuggestionPosition;
21import com.android.quicksearchbox.Suggestions;
22
23import android.content.ComponentName;
24import android.database.DataSetObserver;
25import android.util.Log;
26import android.view.View;
27import android.view.ViewGroup;
28import android.widget.BaseAdapter;
29
30/**
31 * Uses a {@link Suggestions} object to back a {@link SuggestionsView}.
32 */
33public class SuggestionsAdapter extends BaseAdapter {
34
35    private static final boolean DBG = false;
36    private static final String TAG = "QSB.SuggestionsAdapter";
37
38    private DataSetObserver mDataSetObserver;
39
40    private final SuggestionViewFactory mViewFactory;
41
42    private SuggestionCursor mCursor;
43
44    private ComponentName mSource = null;
45
46    private Suggestions mSuggestions;
47
48    private boolean mClosed = false;
49
50    public SuggestionsAdapter(SuggestionViewFactory viewFactory) {
51        mViewFactory = viewFactory;
52    }
53
54    public boolean isClosed() {
55        return mClosed;
56    }
57
58    public void close() {
59        setSuggestions(null);
60        mSource = null;
61        mClosed = true;
62    }
63
64    public void setSuggestions(Suggestions suggestions) {
65        if (mSuggestions == suggestions) {
66            return;
67        }
68        if (mClosed) {
69            if (suggestions != null) {
70                suggestions.close();
71            }
72            return;
73        }
74        if (mDataSetObserver == null) {
75            mDataSetObserver = new MySuggestionsObserver();
76        }
77        // TODO: delay the change if there are no suggestions for the currently visible tab.
78        if (mSuggestions != null) {
79            mSuggestions.unregisterDataSetObserver(mDataSetObserver);
80            mSuggestions.close();
81        }
82        mSuggestions = suggestions;
83        if (mSuggestions != null) {
84            mSuggestions.registerDataSetObserver(mDataSetObserver);
85        }
86        onSuggestionsChanged();
87    }
88
89    protected Suggestions getSuggestions() {
90        return mSuggestions;
91    }
92
93    /**
94     * Gets the source whose results are displayed.
95     */
96    public ComponentName getSource() {
97        return mSource;
98    }
99
100    /**
101     * Sets the source whose results are displayed.
102     *
103     * @param source The name of a source, or {@code null} to show
104     *        the promoted results.
105     */
106    public void setSource(ComponentName source) {
107        mSource = source;
108        onSuggestionsChanged();
109    }
110
111    public int getCount() {
112        return mCursor == null ? 0 : mCursor.getCount();
113    }
114
115    public SuggestionPosition getItem(int position) {
116        if (mCursor == null) return null;
117        return new SuggestionPosition(mCursor, position);
118    }
119
120    public long getItemId(int position) {
121        return position;
122    }
123
124    public int getViewTypeCount() {
125        return mViewFactory.getSuggestionViewTypeCount();
126    }
127
128    public int getItemViewType(int position) {
129        if (mCursor == null) {
130            return 0;
131        }
132        mCursor.moveTo(position);
133        return mViewFactory.getSuggestionViewType(mCursor);
134    }
135
136    public View getView(int position, View convertView, ViewGroup parent) {
137        if (mCursor == null) {
138            throw new IllegalStateException("getView() called with null cursor");
139        }
140        mCursor.moveTo(position);
141        int viewType = mViewFactory.getSuggestionViewType(mCursor);
142        SuggestionView view = mViewFactory.getSuggestionView(viewType, convertView, parent);
143        view.bindAsSuggestion(mCursor);
144        return (View) view;
145    }
146
147    protected void onSuggestionsChanged() {
148        if (DBG) Log.d(TAG, "onSuggestionsChanged(), mSuggestions=" + mSuggestions);
149        SuggestionCursor cursor = getSourceCursor(mSuggestions, mSource);
150        changeCursor(cursor);
151    }
152
153    /**
154     * Gets the cursor containing the currently shown suggestions. The caller should not hold
155     * on to or modify the returned cursor.
156     */
157    public SuggestionCursor getCurrentSuggestions() {
158        return mCursor;
159    }
160
161    /**
162     * Gets the cursor for the given source.
163     */
164    protected SuggestionCursor getSourceCursor(Suggestions suggestions, ComponentName source) {
165        if (suggestions == null) return null;
166        if (source == null) return suggestions.getPromoted();
167        return suggestions.getSourceResult(source);
168    }
169
170    /**
171     * Replace the cursor.
172     *
173     * This does not close the old cursor. Instead, all the cursors are closed in
174     * {@link #setSuggestions(Suggestions)}.
175     */
176    private void changeCursor(SuggestionCursor newCursor) {
177        if (DBG) Log.d(TAG, "changeCursor(" + newCursor + ")");
178        if (newCursor == mCursor) {
179            // Shortcuts may have changed without the cursor changing.
180            notifyDataSetChanged();
181            return;
182        }
183        mCursor = newCursor;
184        if (mCursor != null) {
185            // TODO: Register observers here to watch for
186            // changes in the cursor, e.g. shortcut refreshes?
187            notifyDataSetChanged();
188        } else {
189            notifyDataSetInvalidated();
190        }
191    }
192
193    private class MySuggestionsObserver extends DataSetObserver {
194        @Override
195        public void onChanged() {
196            onSuggestionsChanged();
197        }
198    }
199
200}
201