SuggestionsAdapter.java revision 0484fb4d652bfa9d5c7fb238a7cec1a6f2244e44
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 = true; 36 private static final String TAG = "QSB.SuggestionsAdapter"; 37 38 private long mSourceResultPublishDelayMillis; 39 private long mInitialSourceResultWaitMillis; 40 41 private DataSetObserver mDataSetObserver; 42 43 private final SuggestionViewFactory mViewFactory; 44 45 private SuggestionCursor mCursor; 46 47 private ComponentName mSource = null; 48 49 private Suggestions mSuggestions; 50 51 private boolean mClosed = false; 52 53 public SuggestionsAdapter(SuggestionViewFactory viewFactory) { 54 mViewFactory = viewFactory; 55 } 56 57 public void setSourceResultPublishDelayMillis(long millis) { 58 mSourceResultPublishDelayMillis = millis; 59 } 60 61 public void setInitialSourceResultWaitMillis(long millis) { 62 mInitialSourceResultWaitMillis = millis; 63 } 64 65 public void close() { 66 setSuggestions(null); 67 mSource = null; 68 mClosed = true; 69 } 70 71 public void setSuggestions(Suggestions suggestions) { 72 if (mSuggestions == suggestions) { 73 return; 74 } 75 if (mClosed) { 76 if (suggestions != null) { 77 suggestions.close(); 78 } 79 return; 80 } 81 if (mDataSetObserver == null) { 82 mDataSetObserver = new MySuggestionsObserver(); 83 } 84 // TODO: delay the change if there are no suggestions for the currently visible tab. 85 if (mSuggestions != null) { 86 mSuggestions.unregisterDataSetObserver(mDataSetObserver); 87 mSuggestions.close(); 88 } 89 mSuggestions = suggestions; 90 if (mSuggestions != null) { 91 mSuggestions.registerDataSetObserver(mDataSetObserver); 92 } 93 onSuggestionsChanged(); 94 } 95 96 /** 97 * Sets the source whose results are displayed. 98 * 99 * @param source The name of a source, or {@code null} to show 100 * the promoted results. 101 */ 102 public void setSource(ComponentName source) { 103 mSource = source; 104 onSuggestionsChanged(); 105 } 106 107 public int getCount() { 108 return mCursor == null ? 0 : mCursor.getCount(); 109 } 110 111 public SuggestionPosition getItem(int position) { 112 if (mCursor == null) return null; 113 return new SuggestionPosition(mCursor, position); 114 } 115 116 public long getItemId(int position) { 117 return position; 118 } 119 120 public View getView(int position, View convertView, ViewGroup parent) { 121 if (mCursor == null) { 122 throw new IllegalStateException("getView() called with null cursor"); 123 } 124 SuggestionView view; 125 if (convertView == null) { 126 view = mViewFactory.createSuggestionView(parent); 127 } else { 128 view = (SuggestionView) convertView; 129 } 130 mCursor.moveTo(position); 131 view.bindAsSuggestion(mCursor); 132 return view; 133 } 134 135 protected void onSuggestionsChanged() { 136 if (DBG) Log.d(TAG, "onSuggestionsChanged(), mSuggestions=" + mSuggestions); 137 SuggestionCursor cursor = getCursor(); 138 changeCursor(cursor); 139 } 140 141 /** 142 * Gets the cursor for the selected source. 143 */ 144 private SuggestionCursor getCursor() { 145 if (mSuggestions == null) return null; 146 if (mSource == null) return mSuggestions.getPromoted(); 147 return mSuggestions.getSourceResult(mSource); 148 } 149 150 /** 151 * Replace the cursor. 152 * 153 * This does not close the old cursor. Instead, all the cursors are closed in 154 * {@link #setSuggestions(Suggestions)}. 155 */ 156 private void changeCursor(SuggestionCursor newCursor) { 157 if (DBG) Log.d(TAG, "changeCursor(" + newCursor + ")"); 158 if (newCursor == mCursor) { 159 return; 160 } 161 mCursor = newCursor; 162 if (mCursor != null) { 163 // TODO: Register observers here to watch for 164 // changes in the cursor, e.g. shortcut refreshes? 165 notifyDataSetChanged(); 166 } else { 167 notifyDataSetInvalidated(); 168 } 169 } 170 171 private class MySuggestionsObserver extends DataSetObserver { 172 @Override 173 public void onChanged() { 174 onSuggestionsChanged(); 175 } 176 } 177 178} 179