SuggestionsAdapter.java revision e06b7cbf55301a24cfd7525a91107e3cd2c9f48e
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.Corpus; 20import com.android.quicksearchbox.SuggestionCursor; 21import com.android.quicksearchbox.SuggestionPosition; 22import com.android.quicksearchbox.Suggestions; 23 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 Corpus mCorpus = 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 mCorpus = 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 public Suggestions getSuggestions() { 90 return mSuggestions; 91 } 92 93 /** 94 * Gets the source whose results are displayed. 95 */ 96 public Corpus getCorpus() { 97 return mCorpus; 98 } 99 100 /** 101 * Sets the source whose results are displayed. 102 */ 103 public void setCorpus(Corpus corpus) { 104 mCorpus = corpus; 105 onSuggestionsChanged(); 106 } 107 108 public int getCount() { 109 return mCursor == null ? 0 : mCursor.getCount(); 110 } 111 112 public SuggestionPosition getItem(int position) { 113 if (mCursor == null) return null; 114 return new SuggestionPosition(mCursor, position); 115 } 116 117 public long getItemId(int position) { 118 return position; 119 } 120 121 @Override 122 public int getViewTypeCount() { 123 return mViewFactory.getSuggestionViewTypeCount(); 124 } 125 126 @Override 127 public int getItemViewType(int position) { 128 if (mCursor == null) { 129 return 0; 130 } 131 mCursor.moveTo(position); 132 return mViewFactory.getSuggestionViewType(mCursor); 133 } 134 135 public View getView(int position, View convertView, ViewGroup parent) { 136 if (mCursor == null) { 137 throw new IllegalStateException("getView() called with null cursor"); 138 } 139 mCursor.moveTo(position); 140 int viewType = mViewFactory.getSuggestionViewType(mCursor); 141 SuggestionView view = mViewFactory.getSuggestionView(viewType, convertView, parent); 142 view.bindAsSuggestion(mCursor); 143 return (View) view; 144 } 145 146 protected void onSuggestionsChanged() { 147 if (DBG) Log.d(TAG, "onSuggestionsChanged(), mSuggestions=" + mSuggestions); 148 SuggestionCursor cursor = getCorpusCursor(mSuggestions, mCorpus); 149 changeCursor(cursor); 150 } 151 152 /** 153 * Gets the cursor containing the currently shown suggestions. The caller should not hold 154 * on to or modify the returned cursor. 155 */ 156 public SuggestionCursor getCurrentSuggestions() { 157 return mCursor; 158 } 159 160 /** 161 * Gets the cursor for the given source. 162 */ 163 protected SuggestionCursor getCorpusCursor(Suggestions suggestions, Corpus corpus) { 164 if (suggestions == null) return null; 165 if (corpus == null) return suggestions.getPromoted(); 166 return suggestions.getCorpusResult(corpus); 167 } 168 169 /** 170 * Replace the cursor. 171 * 172 * This does not close the old cursor. Instead, all the cursors are closed in 173 * {@link #setSuggestions(Suggestions)}. 174 */ 175 private void changeCursor(SuggestionCursor newCursor) { 176 if (DBG) Log.d(TAG, "changeCursor(" + newCursor + ")"); 177 if (newCursor == mCursor) { 178 // Shortcuts may have changed without the cursor changing. 179 notifyDataSetChanged(); 180 return; 181 } 182 mCursor = newCursor; 183 if (mCursor != null) { 184 // TODO: Register observers here to watch for 185 // changes in the cursor, e.g. shortcut refreshes? 186 notifyDataSetChanged(); 187 } else { 188 notifyDataSetInvalidated(); 189 } 190 } 191 192 private class MySuggestionsObserver extends DataSetObserver { 193 @Override 194 public void onChanged() { 195 onSuggestionsChanged(); 196 } 197 } 198 199} 200