SearchableCorpusFactory.java revision 574cc4c7b12a766644353800e749719085badcf6
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;
27import java.util.HashSet;
28
29/**
30 * Creates corpora.
31 */
32public class SearchableCorpusFactory implements CorpusFactory {
33
34    private final Context mContext;
35
36    private final NamedTaskExecutor mExecutor;
37
38    public SearchableCorpusFactory(Context context, NamedTaskExecutor executor) {
39        mContext = context;
40        mExecutor = executor;
41    }
42
43    public Collection<Corpus> createCorpora(Sources sources) {
44        Collection<Source> sourceList = sources.getSources();
45        ArrayList<Corpus> corpora = new ArrayList<Corpus>(sourceList.size());
46        HashSet<Source> claimedSources = new HashSet<Source>();
47
48        Source webSource = sources.getWebSearchSource();
49        Source browserSource = sources.getSource(getBrowserSearchComponent());
50        Corpus webCorpus = createWebCorpus(webSource, browserSource);
51        claimedSources.add(webSource);
52        claimedSources.add(browserSource);
53        corpora.add(webCorpus);
54
55        Source appsSource = sources.getSource(getAppsSearchComponent());
56        Corpus appsCorpus = createAppsCorpus(appsSource);
57        claimedSources.add(appsSource);
58        corpora.add(appsCorpus);
59
60        // Creates corpora for all unclaimed sources
61        for (Source source : sourceList) {
62            if (!claimedSources.contains(source)) {
63                corpora.add(new SingleSourceCorpus(source));
64            }
65        }
66
67        return corpora;
68    }
69
70    protected Context getContext() {
71        return mContext;
72    }
73
74    protected Corpus createWebCorpus(Source webSource, Source browserSource) {
75        return new WebCorpus(mContext, mExecutor, webSource, browserSource);
76    }
77
78    protected Corpus createAppsCorpus(Source appsSource) {
79        return new AppsCorpus(mContext, mExecutor, appsSource);
80    }
81
82    private ComponentName getBrowserSearchComponent() {
83        return getComponentNameResource(R.string.browser_search_component);
84    }
85
86    private ComponentName getAppsSearchComponent() {
87        return getComponentNameResource(R.string.installed_apps_component);
88    }
89
90    private ComponentName getComponentNameResource(int res) {
91        String name = mContext.getString(res);
92        return TextUtils.isEmpty(name) ? null : ComponentName.unflattenFromString(name);
93    }
94
95}
96