SearchableCorpora.java revision 81a0897ff9685f3313c58294bf7973700c468b2b
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
17package com.android.quicksearchbox;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.database.DataSetObservable;
22import android.database.DataSetObserver;
23import android.text.TextUtils;
24import android.util.Log;
25
26import java.util.ArrayList;
27import java.util.Collection;
28import java.util.Collections;
29import java.util.HashMap;
30import java.util.List;
31
32/**
33 * Maintains the list of all suggestion sources.
34 */
35public class SearchableCorpora implements Corpora {
36
37    // set to true to enable the more verbose debug logging for this file
38    private static final boolean DBG = false;
39    private static final String TAG = "QSB.DefaultCorpora";
40
41    private final DataSetObservable mDataSetObservable = new DataSetObservable();
42
43    private final Context mContext;
44    private final Config mConfig;
45    private final CorpusFactory mCorpusFactory;
46    private final SharedPreferences mPreferences;
47
48    private boolean mLoaded = false;
49
50    private Sources mSources;
51    // Maps corpus names to corpora
52    private HashMap<String,Corpus> mCorporaByName;
53    // Maps sources to the corpus that contains them
54    private HashMap<Source,Corpus> mCorporaBySource;
55    // Enabled corpora
56    private List<Corpus> mEnabledCorpora;
57    // Web corpus
58    private Corpus mWebCorpus;
59
60    /**
61     *
62     * @param context Used for looking up source information etc.
63     */
64    public SearchableCorpora(Context context, Config config, Sources sources,
65            CorpusFactory corpusFactory) {
66        mContext = context;
67        mConfig = config;
68        mCorpusFactory = corpusFactory;
69        mPreferences = SearchSettings.getSearchPreferences(context);
70        mSources = sources;
71    }
72
73    protected Context getContext() {
74        return mContext;
75    }
76
77    private void checkLoaded() {
78        if (!mLoaded) {
79            throw new IllegalStateException("corpora not loaded.");
80        }
81    }
82
83    public Collection<Corpus> getAllCorpora() {
84        checkLoaded();
85        return Collections.unmodifiableCollection(mCorporaByName.values());
86    }
87
88    public Collection<Corpus> getEnabledCorpora() {
89        checkLoaded();
90        return mEnabledCorpora;
91    }
92
93    public Corpus getCorpus(String name) {
94        checkLoaded();
95        return mCorporaByName.get(name);
96    }
97
98    public Corpus getWebCorpus() {
99        return mWebCorpus;
100    }
101
102    public Corpus getCorpusForSource(Source source) {
103        checkLoaded();
104        return mCorporaBySource.get(source);
105    }
106
107    public Source getSource(String name) {
108        checkLoaded();
109        if (TextUtils.isEmpty(name)) {
110            Log.w(TAG, "Empty source name");
111            return null;
112        }
113        return mSources.getSource(name);
114    }
115
116    /**
117     * After calling, clients must call {@link #close()} when done with this object.
118     */
119    public void load() {
120        if (mLoaded) {
121            throw new IllegalStateException("load(): Already loaded.");
122        }
123
124        mSources.registerDataSetObserver(new DataSetObserver() {
125            @Override
126            public void onChanged() {
127                updateCorpora();
128            }
129        });
130
131        // will cause a callback to updateCorpora()
132        mSources.load();
133        mLoaded = true;
134    }
135
136    /**
137     * Releases all resources used by this object. It is possible to call
138     * {@link #load()} again after calling this method.
139     */
140    public void close() {
141        checkLoaded();
142
143        mSources.close();
144        mSources = null;
145        mLoaded = false;
146    }
147
148    private void updateCorpora() {
149        Collection<Corpus> corpora = mCorpusFactory.createCorpora(mSources);
150
151        mCorporaByName = new HashMap<String,Corpus>(corpora.size());
152        mCorporaBySource = new HashMap<Source,Corpus>(corpora.size());
153        mEnabledCorpora = new ArrayList<Corpus>(corpora.size());
154        mWebCorpus = null;
155
156        for (Corpus corpus : corpora) {
157            mCorporaByName.put(corpus.getName(), corpus);
158            for (Source source : corpus.getSources()) {
159                mCorporaBySource.put(source, corpus);
160            }
161            if (isCorpusEnabled(corpus)) {
162                mEnabledCorpora.add(corpus);
163            }
164            if (corpus.isWebCorpus()) {
165                if (mWebCorpus != null) {
166                    Log.w(TAG, "Multiple web corpora: " + mWebCorpus + ", " + corpus);
167                }
168                mWebCorpus = corpus;
169            }
170        }
171
172        if (DBG) Log.d(TAG, "Updated corpora: " + mCorporaBySource.values());
173
174        mEnabledCorpora = Collections.unmodifiableList(mEnabledCorpora);
175
176        notifyDataSetChanged();
177    }
178
179    public boolean isCorpusEnabled(Corpus corpus) {
180        if (corpus == null) return false;
181        boolean defaultEnabled = isCorpusDefaultEnabled(corpus);
182        String sourceEnabledPref = SearchSettings.getCorpusEnabledPreference(corpus);
183        return mPreferences.getBoolean(sourceEnabledPref, defaultEnabled);
184    }
185
186    public boolean isCorpusDefaultEnabled(Corpus corpus) {
187        String name = corpus.getName();
188        return mConfig.isCorpusEnabledByDefault(name);
189    }
190
191    public void registerDataSetObserver(DataSetObserver observer) {
192        mDataSetObservable.registerObserver(observer);
193    }
194
195    public void unregisterDataSetObserver(DataSetObserver observer) {
196        mDataSetObservable.unregisterObserver(observer);
197    }
198
199    protected void notifyDataSetChanged() {
200        mDataSetObservable.notifyChanged();
201    }
202}
203