SearchableCorpora.java revision dbdffd8316e75bc2813dbbcbef13d357970e8c84
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.DataSetObserver; 23import android.os.Handler; 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 Context mContext; 42 private final Config mConfig; 43 private final CorpusFactory mCorpusFactory; 44 private final SharedPreferences mPreferences; 45 46 private boolean mLoaded = false; 47 48 private Sources mSources; 49 // Maps corpus names to corpora 50 private HashMap<String,Corpus> mCorporaByName; 51 // Maps sources to the corpus that contains them 52 private HashMap<Source,Corpus> mCorporaBySource; 53 // Enabled corpora 54 private List<Corpus> mEnabledCorpora; 55 56 /** 57 * 58 * @param context Used for looking up source information etc. 59 */ 60 public SearchableCorpora(Context context, Config config, Sources sources, 61 CorpusFactory corpusFactory) { 62 mContext = context; 63 mConfig = config; 64 mCorpusFactory = corpusFactory; 65 mPreferences = SearchSettings.getSearchPreferences(context); 66 mSources = sources; 67 } 68 69 protected Context getContext() { 70 return mContext; 71 } 72 73 private void checkLoaded() { 74 if (!mLoaded) { 75 throw new IllegalStateException("corpora not loaded."); 76 } 77 } 78 79 public Collection<Corpus> getAllCorpora() { 80 checkLoaded(); 81 return Collections.unmodifiableCollection(mCorporaByName.values()); 82 } 83 84 public Collection<Corpus> getEnabledCorpora() { 85 checkLoaded(); 86 return mEnabledCorpora; 87 } 88 89 public Corpus getCorpus(String name) { 90 checkLoaded(); 91 return mCorporaByName.get(name); 92 } 93 94 public Corpus getCorpusForSource(Source source) { 95 checkLoaded(); 96 return mCorporaBySource.get(source); 97 } 98 99 public Source getSource(ComponentName name) { 100 checkLoaded(); 101 return mSources.getSource(name); 102 } 103 104 /** 105 * After calling, clients must call {@link #close()} when done with this object. 106 */ 107 public void load() { 108 if (mLoaded) { 109 throw new IllegalStateException("load(): Already loaded."); 110 } 111 112 mSources.registerDataSetObserver(new DataSetObserver() { 113 @Override 114 public void onChanged() { 115 updateCorpora(); 116 } 117 }); 118 119 // will cause a callback to updateCorpora() 120 mSources.load(); 121 mLoaded = true; 122 } 123 124 /** 125 * Releases all resources used by this object. It is possible to call 126 * {@link #load()} again after calling this method. 127 */ 128 public void close() { 129 checkLoaded(); 130 131 mSources.close(); 132 mSources = null; 133 mLoaded = false; 134 } 135 136 private void updateCorpora() { 137 Collection<Corpus> corpora = mCorpusFactory.createCorpora(mSources); 138 139 mCorporaByName = new HashMap<String,Corpus>(corpora.size()); 140 mCorporaBySource = new HashMap<Source,Corpus>(corpora.size()); 141 mEnabledCorpora = new ArrayList<Corpus>(corpora.size()); 142 143 for (Corpus corpus : corpora) { 144 mCorporaByName.put(corpus.getName(), corpus); 145 for (Source source : corpus.getSources()) { 146 mCorporaBySource.put(source, corpus); 147 } 148 if (isCorpusEnabled(corpus)) { 149 mEnabledCorpora.add(corpus); 150 } 151 } 152 153 if (DBG) Log.d(TAG, "Updated corpora: " + mCorporaBySource.values()); 154 155 mEnabledCorpora = Collections.unmodifiableList(mEnabledCorpora); 156 } 157 158 public boolean isCorpusEnabled(Corpus corpus) { 159 if (corpus == null) return false; 160 boolean defaultEnabled = isCorpusDefaultEnabled(corpus); 161 String sourceEnabledPref = SearchSettings.getCorpusEnabledPreference(corpus); 162 return mPreferences.getBoolean(sourceEnabledPref, defaultEnabled); 163 } 164 165 public boolean isCorpusDefaultEnabled(Corpus corpus) { 166 String name = corpus.getName(); 167 return mConfig.isCorpusEnabledByDefault(name); 168 } 169 170} 171