SearchableCorpora.java revision c29c9f854db8fa0c85f17cc32bae33dc17c6b127
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.ComponentName; 20import android.content.Context; 21import android.content.SharedPreferences; 22import android.database.DataSetObservable; 23import android.database.DataSetObserver; 24import android.util.Log; 25 26import java.util.ArrayList; 27import java.util.Collection; 28import java.util.Collections; 29import java.util.HashMap; 30import java.util.List; 31 32/** 33 * Maintains the list of all suggestion sources. 34 */ 35public class SearchableCorpora implements Corpora { 36 37 // set to true to enable the more verbose debug logging for this file 38 private static final boolean DBG = true; 39 private static final String TAG = "QSB.DefaultCorpora"; 40 41 private final DataSetObservable mDataSetObservable = new DataSetObservable(); 42 43 private final Context mContext; 44 private final Config mConfig; 45 private final CorpusFactory mCorpusFactory; 46 private final SharedPreferences mPreferences; 47 48 private boolean mLoaded = false; 49 50 private Sources mSources; 51 // Maps corpus names to corpora 52 private HashMap<String,Corpus> mCorporaByName; 53 // Maps sources to the corpus that contains them 54 private HashMap<Source,Corpus> mCorporaBySource; 55 // Enabled corpora 56 private List<Corpus> mEnabledCorpora; 57 // Web corpus 58 private Corpus mWebCorpus; 59 60 /** 61 * 62 * @param context Used for looking up source information etc. 63 */ 64 public SearchableCorpora(Context context, Config config, Sources sources, 65 CorpusFactory corpusFactory) { 66 mContext = context; 67 mConfig = config; 68 mCorpusFactory = corpusFactory; 69 mPreferences = SearchSettings.getSearchPreferences(context); 70 mSources = sources; 71 } 72 73 protected Context getContext() { 74 return mContext; 75 } 76 77 private void checkLoaded() { 78 if (!mLoaded) { 79 throw new IllegalStateException("corpora not loaded."); 80 } 81 } 82 83 public Collection<Corpus> getAllCorpora() { 84 checkLoaded(); 85 return Collections.unmodifiableCollection(mCorporaByName.values()); 86 } 87 88 public Collection<Corpus> getEnabledCorpora() { 89 checkLoaded(); 90 return mEnabledCorpora; 91 } 92 93 public Corpus getCorpus(String name) { 94 checkLoaded(); 95 return mCorporaByName.get(name); 96 } 97 98 public Corpus getWebCorpus() { 99 return mWebCorpus; 100 } 101 102 public Corpus getCorpusForSource(Source source) { 103 checkLoaded(); 104 return mCorporaBySource.get(source); 105 } 106 107 public Source getSource(ComponentName name) { 108 checkLoaded(); 109 return mSources.getSource(name); 110 } 111 112 /** 113 * After calling, clients must call {@link #close()} when done with this object. 114 */ 115 public void load() { 116 if (mLoaded) { 117 throw new IllegalStateException("load(): Already loaded."); 118 } 119 120 mSources.registerDataSetObserver(new DataSetObserver() { 121 @Override 122 public void onChanged() { 123 updateCorpora(); 124 } 125 }); 126 127 // will cause a callback to updateCorpora() 128 mSources.load(); 129 mLoaded = true; 130 } 131 132 /** 133 * Releases all resources used by this object. It is possible to call 134 * {@link #load()} again after calling this method. 135 */ 136 public void close() { 137 checkLoaded(); 138 139 mSources.close(); 140 mSources = null; 141 mLoaded = false; 142 } 143 144 private void updateCorpora() { 145 Collection<Corpus> corpora = mCorpusFactory.createCorpora(mSources); 146 147 mCorporaByName = new HashMap<String,Corpus>(corpora.size()); 148 mCorporaBySource = new HashMap<Source,Corpus>(corpora.size()); 149 mEnabledCorpora = new ArrayList<Corpus>(corpora.size()); 150 mWebCorpus = null; 151 152 for (Corpus corpus : corpora) { 153 mCorporaByName.put(corpus.getName(), corpus); 154 for (Source source : corpus.getSources()) { 155 mCorporaBySource.put(source, corpus); 156 } 157 if (isCorpusEnabled(corpus)) { 158 mEnabledCorpora.add(corpus); 159 } 160 if (corpus.isWebCorpus()) { 161 if (mWebCorpus != null) { 162 Log.w(TAG, "Multiple web corpora: " + mWebCorpus + ", " + corpus); 163 } 164 mWebCorpus = corpus; 165 } 166 } 167 168 if (DBG) Log.d(TAG, "Updated corpora: " + mCorporaBySource.values()); 169 170 mEnabledCorpora = Collections.unmodifiableList(mEnabledCorpora); 171 172 notifyDataSetChanged(); 173 } 174 175 public boolean isCorpusEnabled(Corpus corpus) { 176 if (corpus == null) return false; 177 boolean defaultEnabled = isCorpusDefaultEnabled(corpus); 178 String sourceEnabledPref = SearchSettings.getCorpusEnabledPreference(corpus); 179 return mPreferences.getBoolean(sourceEnabledPref, defaultEnabled); 180 } 181 182 public boolean isCorpusDefaultEnabled(Corpus corpus) { 183 String name = corpus.getName(); 184 return mConfig.isCorpusEnabledByDefault(name); 185 } 186 187 public void registerDataSetObserver(DataSetObserver observer) { 188 mDataSetObservable.registerObserver(observer); 189 } 190 191 public void unregisterDataSetObserver(DataSetObserver observer) { 192 mDataSetObservable.unregisterObserver(observer); 193 } 194 195 protected void notifyDataSetChanged() { 196 mDataSetObservable.notifyChanged(); 197 } 198} 199