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 android.os.Bundle;
20import android.preference.Preference;
21import android.preference.PreferenceActivity;
22import android.preference.PreferenceGroup;
23import android.preference.Preference.OnPreferenceChangeListener;
24import android.util.Log;
25
26/**
27 * Activity for selecting searchable items.
28 */
29public class SearchableItemsSettings extends PreferenceActivity
30        implements OnPreferenceChangeListener {
31
32    private static final boolean DBG = false;
33    private static final String TAG = "QSB.SearchableItemsSettings";
34
35    // Only used to find the preferences after inflating
36    private static final String SEARCH_CORPORA_PREF = "search_corpora";
37
38    // References to the top-level preference objects
39    private PreferenceGroup mCorporaPreferences;
40
41    @Override
42    protected void onCreate(Bundle savedInstanceState) {
43        super.onCreate(savedInstanceState);
44
45        getPreferenceManager().setSharedPreferencesName(SearchSettings.PREFERENCES_NAME);
46
47        addPreferencesFromResource(R.xml.preferences_searchable_items);
48
49        mCorporaPreferences = (PreferenceGroup) getPreferenceScreen().findPreference(
50                SEARCH_CORPORA_PREF);
51
52        populateSourcePreference();
53    }
54
55    private Corpora getCorpora() {
56        return QsbApplication.get(this).getCorpora();
57    }
58
59    /**
60     * Fills the suggestion source list.
61     */
62    private void populateSourcePreference() {
63        mCorporaPreferences.setOrderingAsAdded(false);
64        for (Corpus corpus : getCorpora().getAllCorpora()) {
65            Preference pref = createCorpusPreference(corpus);
66            if (pref != null) {
67                if (DBG) Log.d(TAG, "Adding corpus: " + corpus);
68                mCorporaPreferences.addPreference(pref);
69            }
70        }
71    }
72
73    /**
74     * Adds a suggestion source to the list of suggestion source checkbox preferences.
75     */
76    private Preference createCorpusPreference(Corpus corpus) {
77        SearchableItemPreference sourcePref = new SearchableItemPreference(this);
78        sourcePref.setKey(SearchSettings.getCorpusEnabledPreference(corpus));
79        // Put web corpus first. The rest are alphabetical.
80        if (corpus.isWebCorpus()) {
81            sourcePref.setOrder(0);
82        }
83        sourcePref.setDefaultValue(corpus.isCorpusDefaultEnabled());
84        sourcePref.setOnPreferenceChangeListener(this);
85        CharSequence label = corpus.getLabel();
86        sourcePref.setTitle(label);
87        CharSequence description = corpus.getSettingsDescription();
88        sourcePref.setSummaryOn(description);
89        sourcePref.setSummaryOff(description);
90        sourcePref.setIcon(corpus.getCorpusIcon());
91        return sourcePref;
92    }
93
94    public boolean onPreferenceChange(Preference preference, Object newValue) {
95        SearchSettings.broadcastSettingsChanged(this);
96        return true;
97    }
98
99}
100