SuggestionsAdapter.java revision 94e8a2be78530170f50e7895a558bf8011bbf8e8
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 View getView(int position, View convertView, ViewGroup parent) { 125 if (mCursor == null) { 126 throw new IllegalStateException("getView() called with null cursor"); 127 } 128 SuggestionView view; 129 if (convertView == null) { 130 view = mViewFactory.createSuggestionView(parent); 131 } else { 132 view = (SuggestionView) convertView; 133 } 134 mCursor.moveTo(position); 135 view.bindAsSuggestion(mCursor); 136 return view; 137 } 138 139 protected void onSuggestionsChanged() { 140 if (DBG) Log.d(TAG, "onSuggestionsChanged(), mSuggestions=" + mSuggestions); 141 SuggestionCursor cursor = getSourceCursor(mSuggestions, mSource); 142 changeCursor(cursor); 143 } 144 145 /** 146 * Gets the cursor for the given source. 147 */ 148 protected SuggestionCursor getSourceCursor(Suggestions suggestions, ComponentName source) { 149 if (suggestions == null) return null; 150 if (source == null) return suggestions.getPromoted(); 151 return suggestions.getSourceResult(source); 152 } 153 154 /** 155 * Replace the cursor. 156 * 157 * This does not close the old cursor. Instead, all the cursors are closed in 158 * {@link #setSuggestions(Suggestions)}. 159 */ 160 private void changeCursor(SuggestionCursor newCursor) { 161 if (DBG) Log.d(TAG, "changeCursor(" + newCursor + ")"); 162 if (newCursor == mCursor) { 163 // Shortcuts may have changed without the cursor changing. 164 notifyDataSetChanged(); 165 return; 166 } 167 mCursor = newCursor; 168 if (mCursor != null) { 169 // TODO: Register observers here to watch for 170 // changes in the cursor, e.g. shortcut refreshes? 171 notifyDataSetChanged(); 172 } else { 173 notifyDataSetInvalidated(); 174 } 175 } 176 177 private class MySuggestionsObserver extends DataSetObserver { 178 @Override 179 public void onChanged() { 180 onSuggestionsChanged(); 181 } 182 } 183 184} 185