SearchableCorpora.java revision 96c7058210699c82445169048b7c0fdfb16f59ee
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; 18 19import android.content.Context; 20import android.database.DataSetObservable; 21import android.database.DataSetObserver; 22import android.text.TextUtils; 23import android.util.Log; 24 25import java.util.ArrayList; 26import java.util.Collection; 27import java.util.Collections; 28import java.util.HashMap; 29import java.util.List; 30 31/** 32 * Maintains the list of all corpora. 33 */ 34public class SearchableCorpora implements Corpora { 35 36 // set to true to enable the more verbose debug logging for this file 37 private static final boolean DBG = false; 38 private static final String TAG = "QSB.DefaultCorpora"; 39 40 private final DataSetObservable mDataSetObservable = new DataSetObservable(); 41 42 private final Context mContext; 43 private final CorpusFactory mCorpusFactory; 44 45 private Sources mSources; 46 // Maps corpus names to corpora 47 private HashMap<String,Corpus> mCorporaByName; 48 // Maps sources to the corpus that contains them 49 private HashMap<Source,Corpus> mCorporaBySource; 50 // Enabled corpora 51 private List<Corpus> mEnabledCorpora; 52 // Web corpus 53 private Corpus mWebCorpus; 54 55 /** 56 * 57 * @param context Used for looking up source information etc. 58 */ 59 public SearchableCorpora(Context context, Sources sources, CorpusFactory corpusFactory) { 60 mContext = context; 61 mCorpusFactory = corpusFactory; 62 mSources = sources; 63 } 64 65 protected Context getContext() { 66 return mContext; 67 } 68 69 public Collection<Corpus> getAllCorpora() { 70 return Collections.unmodifiableCollection(mCorporaByName.values()); 71 } 72 73 public Collection<Corpus> getEnabledCorpora() { 74 return mEnabledCorpora; 75 } 76 77 public Corpus getCorpus(String name) { 78 return mCorporaByName.get(name); 79 } 80 81 public Corpus getWebCorpus() { 82 return mWebCorpus; 83 } 84 85 public Corpus getCorpusForSource(Source source) { 86 return mCorporaBySource.get(source); 87 } 88 89 public Source getSource(String name) { 90 if (TextUtils.isEmpty(name)) { 91 Log.w(TAG, "Empty source name"); 92 return null; 93 } 94 return mSources.getSource(name); 95 } 96 97 public void update() { 98 mSources.update(); 99 100 Collection<Corpus> corpora = mCorpusFactory.createCorpora(mSources); 101 102 mCorporaByName = new HashMap<String,Corpus>(corpora.size()); 103 mCorporaBySource = new HashMap<Source,Corpus>(corpora.size()); 104 mEnabledCorpora = new ArrayList<Corpus>(corpora.size()); 105 mWebCorpus = null; 106 107 for (Corpus corpus : corpora) { 108 mCorporaByName.put(corpus.getName(), corpus); 109 for (Source source : corpus.getSources()) { 110 mCorporaBySource.put(source, corpus); 111 } 112 if (corpus.isCorpusEnabled()) { 113 mEnabledCorpora.add(corpus); 114 } 115 if (corpus.isWebCorpus()) { 116 if (mWebCorpus != null) { 117 Log.w(TAG, "Multiple web corpora: " + mWebCorpus + ", " + corpus); 118 } 119 mWebCorpus = corpus; 120 } 121 } 122 123 if (DBG) Log.d(TAG, "Updated corpora: " + mCorporaBySource.values()); 124 125 mEnabledCorpora = Collections.unmodifiableList(mEnabledCorpora); 126 127 notifyDataSetChanged(); 128 } 129 130 public void registerDataSetObserver(DataSetObserver observer) { 131 mDataSetObservable.registerObserver(observer); 132 } 133 134 public void unregisterDataSetObserver(DataSetObserver observer) { 135 mDataSetObservable.unregisterObserver(observer); 136 } 137 138 protected void notifyDataSetChanged() { 139 mDataSetObservable.notifyChanged(); 140 } 141} 142