CorporaAdapter.java revision 138c0a8d821f02d13e73bfc579ad9bded7d4d683
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.R; 22 23import android.content.Context; 24import android.database.DataSetObserver; 25import android.graphics.drawable.Drawable; 26import android.util.Log; 27import android.view.LayoutInflater; 28import android.view.View; 29import android.view.ViewGroup; 30import android.widget.BaseAdapter; 31 32import java.util.ArrayList; 33import java.util.Collections; 34import java.util.Comparator; 35import java.util.List; 36 37/** 38 * Adapter for showing a list of sources in the source selection activity. 39 */ 40public class CorporaAdapter extends BaseAdapter { 41 42 private static final String TAG = "CorporaAdapter"; 43 private static final boolean DBG = false; 44 45 private final Context mContext; 46 47 private final Corpora mCorpora; 48 49 private final int mCorpusViewRes; 50 51 private final DataSetObserver mCorporaObserver = new CorporaObserver(); 52 53 private List<Corpus> mSortedCorpora; 54 55 private String mCurrentCorpusName; 56 57 public CorporaAdapter(Context context, Corpora corpora, int corpusViewRes) { 58 mContext = context; 59 mCorpora = corpora; 60 mCorpusViewRes = corpusViewRes; 61 mCorpora.registerDataSetObserver(mCorporaObserver); 62 updateCorpora(); 63 } 64 65 public void setCurrentCorpus(Corpus corpus) { 66 mCurrentCorpusName = corpus == null ? null : corpus.getName(); 67 notifyDataSetChanged(); 68 } 69 70 private void updateCorpora() { 71 List<Corpus> enabledCorpora = mCorpora.getEnabledCorpora(); 72 ArrayList<Corpus> sorted = new ArrayList<Corpus>(enabledCorpora.size()); 73 for (Corpus corpus : enabledCorpora) { 74 if (!corpus.isCorpusHidden()) { 75 sorted.add(corpus); 76 } 77 } 78 Collections.sort(sorted, new CorpusComparator()); 79 mSortedCorpora = sorted; 80 notifyDataSetChanged(); 81 } 82 83 private static class CorpusComparator implements Comparator<Corpus> { 84 public int compare(Corpus corpus1, Corpus corpus2) { 85 // Comparing a corpus against itself 86 if (corpus1 == corpus2) return 0; 87 // Web always comes first 88 if (corpus1.isWebCorpus()) return -1; 89 if (corpus2.isWebCorpus()) return 1; 90 // Alphabetically by name 91 return corpus1.getLabel().toString().compareTo(corpus2.getLabel().toString()); 92 } 93 } 94 95 public void close() { 96 mCorpora.unregisterDataSetObserver(mCorporaObserver); 97 } 98 99 public int getCount() { 100 return 1 + (mSortedCorpora == null ? 0 : mSortedCorpora.size()); 101 } 102 103 public Corpus getItem(int position) { 104 if (position == 0) { 105 return null; 106 } else { 107 return mSortedCorpora.get(position - 1); 108 } 109 } 110 111 public long getItemId(int position) { 112 return position; 113 } 114 115 /** 116 * Gets the position of the given corpus. 117 */ 118 public int getCorpusPosition(Corpus corpus) { 119 if (corpus == null) { 120 return 0; 121 } 122 int count = getCount(); 123 for (int i = 0; i < count; i++) { 124 if (corpus.equals(getItem(i))) { 125 return i; 126 } 127 } 128 Log.w(TAG, "Corpus not in adapter: " + corpus); 129 return 0; 130 } 131 132 public View getView(int position, View convertView, ViewGroup parent) { 133 CorpusView view = (CorpusView) convertView; 134 if (view == null) { 135 view = createView(parent); 136 } 137 Corpus corpus = getItem(position); 138 if (DBG) Log.d(TAG, "Binding " + position + ", corpus=" + corpus); 139 bindView(view, corpus); 140 return view; 141 } 142 143 protected void bindView(CorpusView view, Corpus corpus) { 144 Drawable icon = getCorpusIcon(corpus); 145 CharSequence label = getCorpusLabel(corpus); 146 boolean isCurrent = isCurrentCorpus(corpus); 147 if (DBG) Log.d(TAG, "bind:name=" + corpus + ",label=" + label + ",current=" + isCurrent); 148 view.setIcon(icon); 149 view.setLabel(label); 150 view.setChecked(isCurrent); 151 } 152 153 protected Drawable getCorpusIcon(Corpus corpus) { 154 if (corpus == null) { 155 return mContext.getResources().getDrawable(R.mipmap.search_app_icon); 156 } else { 157 return corpus.getCorpusIcon(); 158 } 159 } 160 161 protected CharSequence getCorpusLabel(Corpus corpus) { 162 if (corpus == null) { 163 return mContext.getText(R.string.corpus_label_global); 164 } else { 165 return corpus.getLabel(); 166 } 167 } 168 169 protected boolean isCurrentCorpus(Corpus corpus) { 170 if (corpus == null) { 171 return mCurrentCorpusName == null; 172 } else { 173 return corpus.getName().equals(mCurrentCorpusName); 174 } 175 } 176 177 protected CorpusView createView(ViewGroup parent) { 178 return (CorpusView) LayoutInflater.from(mContext).inflate(mCorpusViewRes, parent, false); 179 } 180 181 protected LayoutInflater getInflater() { 182 return (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 183 } 184 185 private class CorporaObserver extends DataSetObserver { 186 @Override 187 public void onChanged() { 188 updateCorpora(); 189 } 190 191 @Override 192 public void onInvalidated() { 193 updateCorpora(); 194 } 195 } 196 197} 198