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