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