CorporaAdapter.java revision a576b1abd602c08dcf0d470facc43fac0243b07c
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.Corpora; 20import com.android.quicksearchbox.Corpus; 21import com.android.quicksearchbox.CorpusRanker; 22 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.ArrayList; 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 ArrayList<Corpus> mRankedEnabledCorpora; 42 43 private boolean mGridView; 44 45 private CorporaAdapter(CorpusViewFactory viewFactory, Corpora corpora, 46 CorpusRanker ranker, boolean gridView) { 47 mViewFactory = viewFactory; 48 mRankedEnabledCorpora = ranker.rankCorpora(corpora.getEnabledCorpora()); 49 mGridView = gridView; 50 } 51 52 public static CorporaAdapter createListAdapter(CorpusViewFactory viewFactory, Corpora corpora, 53 CorpusRanker ranker) { 54 return new CorporaAdapter(viewFactory, corpora, ranker, false); 55 } 56 57 public static CorporaAdapter createGridAdapter(CorpusViewFactory viewFactory, Corpora corpora, 58 CorpusRanker ranker) { 59 return new CorporaAdapter(viewFactory, corpora, ranker, true); 60 } 61 62 public int getCount() { 63 return 1 + mRankedEnabledCorpora.size(); 64 } 65 66 public Corpus getItem(int position) { 67 if (position == 0) { 68 return null; 69 } else { 70 return mRankedEnabledCorpora.get(position - 1); 71 } 72 } 73 74 public long getItemId(int position) { 75 return position; 76 } 77 78 public View getView(int position, View convertView, ViewGroup parent) { 79 CorpusView view = (CorpusView) convertView; 80 if (view == null) { 81 view = createView(parent); 82 } 83 Corpus corpus = getItem(position); 84 Drawable icon; 85 CharSequence label; 86 if (corpus == null) { 87 icon = mViewFactory.getGlobalSearchIcon(); 88 label = mViewFactory.getGlobalSearchLabel(); 89 } else { 90 icon = corpus.getCorpusIcon(); 91 label = corpus.getLabel(); 92 } 93 if (DBG) Log.d(TAG, "Binding " + position + ", label=" + label); 94 view.setIcon(icon); 95 view.setLabel(label); 96 return view; 97 } 98 99 protected CorpusView createView(ViewGroup parent) { 100 if (mGridView) { 101 return mViewFactory.createGridCorpusView(parent); 102 } else { 103 return mViewFactory.createListCorpusView(parent); 104 } 105 } 106 107} 108