SearchableCorpusFactory.java revision f252dc7a25ba08b973ecc1cfbbce58eb78d42167
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.NamedTaskExecutor;
20
21import android.content.ComponentName;
22import android.content.Context;
23import android.text.TextUtils;
24
25import java.util.ArrayList;
26import java.util.Collection;
27
28/**
29 * Creates corpora.
30 */
31public class SearchableCorpusFactory implements CorpusFactory {
32
33    private final Context mContext;
34
35    private final NamedTaskExecutor mExecutor;
36
37    public SearchableCorpusFactory(Context context, NamedTaskExecutor executor) {
38        mContext = context;
39        mExecutor = executor;
40    }
41
42    public Collection<Corpus> createCorpora(Sources sources) {
43        Collection<Source> sourceList = sources.getSources();
44        ArrayList<Corpus> corpora = new ArrayList<Corpus>(sourceList.size());
45
46        Source webSource = sources.getWebSearchSource();
47        Source browserSource = sources.getSource(getBrowserSearchComponent());
48        Corpus webCorpus = createWebCorpus(webSource, browserSource);
49        corpora.add(webCorpus);
50
51        // Creates corpora for all unclaimed sources
52        for (Source source : sourceList) {
53            if (source != webSource && source != browserSource) {
54                corpora.add(new SingleSourceCorpus(source));
55            }
56        }
57
58        return corpora;
59    }
60
61    protected Context getContext() {
62        return mContext;
63    }
64
65    protected Corpus createWebCorpus(Source webSource, Source browserSource) {
66        return new WebCorpus(mContext, mExecutor, webSource, browserSource);
67    }
68
69    private ComponentName getBrowserSearchComponent() {
70        String name = mContext.getString(R.string.browser_search_component);
71        return TextUtils.isEmpty(name) ? null : ComponentName.unflattenFromString(name);
72    }
73
74}
75