SearchableCorpusFactory.java revision d5cd9612d6937dfb174bfe6f4e486f283ef557e9
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
17package com.android.quicksearchbox;
18
19import com.android.quicksearchbox.util.Factory;
20
21import android.content.Context;
22import android.util.Log;
23
24import java.util.ArrayList;
25import java.util.Collection;
26import java.util.HashSet;
27import java.util.concurrent.Executor;
28
29/**
30 * Creates corpora.
31 */
32public class SearchableCorpusFactory implements CorpusFactory {
33
34    private static final String TAG = "QSB.SearchableCorpusFactory";
35
36    private final Context mContext;
37
38    private final Config mConfig;
39
40    private final Factory<Executor> mWebCorpusExecutorFactory;
41
42    public SearchableCorpusFactory(Context context, Config config,
43            Factory<Executor> webCorpusExecutorFactory) {
44        mContext = context;
45        mConfig = config;
46        mWebCorpusExecutorFactory = webCorpusExecutorFactory;
47    }
48
49    public Collection<Corpus> createCorpora(Sources sources) {
50        ArrayList<Corpus> corpora = new ArrayList<Corpus>();
51        addSpecialCorpora(corpora, sources);
52        addSingleSourceCorpora(corpora, sources);
53        return corpora;
54    }
55
56    protected Context getContext() {
57        return mContext;
58    }
59
60    protected Config getConfig() {
61        return mConfig;
62    }
63
64    protected Executor createWebCorpusExecutor() {
65        return mWebCorpusExecutorFactory.create();
66    }
67
68    /**
69     * Adds any corpora that are not simple single source corpora.
70     *
71     * @param corpora List to add corpora to.
72     * @param sources All available sources.
73     */
74    protected void addSpecialCorpora(ArrayList<Corpus> corpora, Sources sources) {
75        addCorpus(corpora, createWebCorpus(sources));
76        addCorpus(corpora, createAppsCorpus(sources));
77    }
78
79    /**
80     * Adds corpora for all sources that are not already used by a corpus.
81     *
82     * @param corpora List to add the new corpora to. Corpora will not be created for the sources
83     *        used by corpora already in this list.
84     * @param sources Sources to create corpora for.
85     */
86    protected void addSingleSourceCorpora(ArrayList<Corpus> corpora, Sources sources) {
87        // Set of all sources that are already used
88        HashSet<Source> claimedSources = new HashSet<Source>();
89        for (Corpus specialCorpus : corpora) {
90            claimedSources.addAll(specialCorpus.getSources());
91        }
92
93        // Creates corpora for all unclaimed sources
94        for (Source source : sources.getSources()) {
95            if (!claimedSources.contains(source)) {
96                addCorpus(corpora, createSingleSourceCorpus(source));
97            }
98        }
99    }
100
101    private void addCorpus(ArrayList<Corpus> corpora, Corpus corpus) {
102        if (corpus != null) corpora.add(corpus);
103    }
104
105    protected Corpus createWebCorpus(Sources sources) {
106        Source webSource = getWebSource(sources);
107        if (webSource != null && !webSource.canRead()) {
108            Log.w(TAG, "Can't read web source " + webSource.getName());
109            webSource = null;
110        }
111        Source browserSource = getBrowserSource(sources);
112        if (browserSource != null && !browserSource.canRead()) {
113            Log.w(TAG, "Can't read browser source " + browserSource.getName());
114            browserSource = null;
115        }
116        Executor executor = createWebCorpusExecutor();
117        return new WebCorpus(mContext, mConfig, executor, webSource, browserSource);
118    }
119
120    protected Corpus createAppsCorpus(Sources sources) {
121        Source appsSource = getAppsSource(sources);
122        return new AppsCorpus(mContext, mConfig, appsSource);
123    }
124
125    protected Corpus createSingleSourceCorpus(Source source) {
126        if (!source.canRead()) return null;
127        return new SingleSourceCorpus(mContext, mConfig, source);
128    }
129
130    protected Source getWebSource(Sources sources) {
131        return sources.getWebSearchSource();
132    }
133
134    protected Source getBrowserSource(Sources sources) {
135        String name = getContext().getString(R.string.browser_search_component);
136        return sources.getSource(name);
137    }
138
139    protected Source getAppsSource(Sources sources) {
140        String name = getContext().getString(R.string.installed_apps_component);
141        return sources.getSource(name);
142    }
143
144}
145