Home | History | Annotate | Download | only in quicksearchbox
      1 /*
      2  * Copyright (C) 2010 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 com.android.quicksearchbox.util.Factory;
     20 
     21 import android.content.Context;
     22 
     23 import java.util.ArrayList;
     24 import java.util.Collection;
     25 import java.util.HashSet;
     26 import java.util.concurrent.Executor;
     27 
     28 /**
     29  * Creates corpora.
     30  */
     31 public class SearchableCorpusFactory implements CorpusFactory {
     32 
     33     private final Context mContext;
     34 
     35     private final Factory<Executor> mWebCorpusExecutorFactory;
     36 
     37     public SearchableCorpusFactory(Context context, Factory<Executor> webCorpusExecutorFactory) {
     38         mContext = context;
     39         mWebCorpusExecutorFactory = webCorpusExecutorFactory;
     40     }
     41 
     42     public Collection<Corpus> createCorpora(Sources sources) {
     43         ArrayList<Corpus> corpora = new ArrayList<Corpus>();
     44         addSpecialCorpora(corpora, sources);
     45         addSingleSourceCorpora(corpora, sources);
     46         return corpora;
     47     }
     48 
     49     protected Context getContext() {
     50         return mContext;
     51     }
     52 
     53     protected Executor createWebCorpusExecutor() {
     54         return mWebCorpusExecutorFactory.create();
     55     }
     56 
     57     /**
     58      * Adds any corpora that are not simple single source corpora.
     59      *
     60      * @param corpora List to add corpora to.
     61      * @param sources All available sources.
     62      */
     63     protected void addSpecialCorpora(ArrayList<Corpus> corpora, Sources sources) {
     64         corpora.add(createWebCorpus(sources));
     65         corpora.add(createAppsCorpus(sources));
     66     }
     67 
     68     /**
     69      * Adds corpora for all sources that are not already used by a corpus.
     70      *
     71      * @param corpora List to add the new corpora to. Corpora will not be created for the sources
     72      *        used by corpora already in this list.
     73      * @param sources Sources to create corpora for.
     74      */
     75     protected void addSingleSourceCorpora(ArrayList<Corpus> corpora, Sources sources) {
     76         // Set of all sources that are already used
     77         HashSet<Source> claimedSources = new HashSet<Source>();
     78         for (Corpus specialCorpus : corpora) {
     79             claimedSources.addAll(specialCorpus.getSources());
     80         }
     81 
     82         // Creates corpora for all unclaimed sources
     83         for (Source source : sources.getSources()) {
     84             if (!claimedSources.contains(source)) {
     85                 corpora.add(createSingleSourceCorpus(source));
     86             }
     87         }
     88     }
     89 
     90     protected Corpus createWebCorpus(Sources sources) {
     91         Source webSource = sources.getWebSearchSource();
     92         Source browserSource = getBrowserSource(sources);
     93         Executor executor = createWebCorpusExecutor();
     94         return new WebCorpus(mContext, executor, webSource, browserSource);
     95     }
     96 
     97     protected Corpus createAppsCorpus(Sources sources) {
     98         Source appsSource = getAppsSource(sources);
     99         return new AppsCorpus(mContext, appsSource);
    100     }
    101 
    102     protected Corpus createSingleSourceCorpus(Source source) {
    103         return new SingleSourceCorpus(mContext, source);
    104     }
    105 
    106     protected Source getBrowserSource(Sources sources) {
    107         String name = getContext().getString(R.string.browser_search_component);
    108         return sources.getSource(name);
    109     }
    110 
    111     protected Source getAppsSource(Sources sources) {
    112         String name = getContext().getString(R.string.installed_apps_component);
    113         return sources.getSource(name);
    114     }
    115 
    116 }
    117