CorporaAdapter.java revision ce4cdcf739b57563ddcdbed6944128b8c1f7522a
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.CorpusRanker; 21 22import android.database.DataSetObserver; 23import android.graphics.drawable.Drawable; 24import android.util.Log; 25import android.view.View; 26import android.view.ViewGroup; 27import android.widget.BaseAdapter; 28 29import java.util.List; 30 31/** 32 * Adapter for showing a list of sources in the source selection activity. 33 */ 34public class CorporaAdapter extends BaseAdapter { 35 36 private static final String TAG = "CorporaAdapter"; 37 private static final boolean DBG = true; 38 39 private final CorpusViewFactory mViewFactory; 40 41 private final CorpusRanker mRanker; 42 43 private final DataSetObserver mCorporaObserver = new CorporaObserver(); 44 45 private List<Corpus> mRankedEnabledCorpora; 46 47 private boolean mGridView; 48 49 private CorporaAdapter(CorpusViewFactory viewFactory, 50 CorpusRanker ranker, boolean gridView) { 51 mViewFactory = viewFactory; 52 mRanker = ranker; 53 mGridView = gridView; 54 mRanker.registerDataSetObserver(mCorporaObserver); 55 updateCorpora(); 56 } 57 58 public static CorporaAdapter createListAdapter(CorpusViewFactory viewFactory, 59 CorpusRanker ranker) { 60 return new CorporaAdapter(viewFactory, ranker, false); 61 } 62 63 public static CorporaAdapter createGridAdapter(CorpusViewFactory viewFactory, 64 CorpusRanker ranker) { 65 return new CorporaAdapter(viewFactory, ranker, true); 66 } 67 68 private void updateCorpora() { 69 mRankedEnabledCorpora = mRanker.getRankedCorpora(); 70 notifyDataSetChanged(); 71 } 72 73 public void close() { 74 mRanker.unregisterDataSetObserver(mCorporaObserver); 75 } 76 77 public int getCount() { 78 return 1 + mRankedEnabledCorpora.size(); 79 } 80 81 public Corpus getItem(int position) { 82 if (position == 0) { 83 return null; 84 } else { 85 return mRankedEnabledCorpora.get(position - 1); 86 } 87 } 88 89 public long getItemId(int position) { 90 return position; 91 } 92 93 /** 94 * Gets the position of the given corpus. 95 */ 96 public int getCorpusPosition(Corpus corpus) { 97 if (corpus == null) { 98 return 0; 99 } 100 int count = getCount(); 101 for (int i = 0; i < count; i++) { 102 if (corpus.equals(getItem(i))) { 103 return i; 104 } 105 } 106 Log.w(TAG, "Corpus not in adapter: " + corpus); 107 return 0; 108 } 109 110 public View getView(int position, View convertView, ViewGroup parent) { 111 CorpusView view = (CorpusView) convertView; 112 if (view == null) { 113 view = createView(parent); 114 } 115 Corpus corpus = getItem(position); 116 Drawable icon; 117 CharSequence label; 118 if (corpus == null) { 119 icon = mViewFactory.getGlobalSearchIcon(); 120 label = mViewFactory.getGlobalSearchLabel(); 121 } else { 122 icon = corpus.getCorpusIcon(); 123 label = corpus.getLabel(); 124 } 125 if (DBG) Log.d(TAG, "Binding " + position + ", label=" + label); 126 view.setIcon(icon); 127 view.setLabel(label); 128 return view; 129 } 130 131 protected CorpusView createView(ViewGroup parent) { 132 if (mGridView) { 133 return mViewFactory.createGridCorpusView(parent); 134 } else { 135 return mViewFactory.createListCorpusView(parent); 136 } 137 } 138 139 private class CorporaObserver extends DataSetObserver { 140 @Override 141 public void onChanged() { 142 updateCorpora(); 143 } 144 145 @Override 146 public void onInvalidated() { 147 updateCorpora(); 148 } 149 } 150 151} 152