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 package com.android.quicksearchbox.preferences; 17 18 import com.android.quicksearchbox.Corpora; 19 import com.android.quicksearchbox.Corpus; 20 import com.android.quicksearchbox.R; 21 import com.android.quicksearchbox.SearchSettings; 22 import com.android.quicksearchbox.SearchSettingsImpl; 23 24 import android.content.Context; 25 import android.content.res.Resources; 26 import android.preference.Preference; 27 import android.preference.Preference.OnPreferenceChangeListener; 28 import android.preference.PreferenceGroup; 29 import android.util.Log; 30 31 /** 32 * Logic backing the searchable items activity or fragment. 33 */ 34 public class SearchableItemsController implements PreferenceController, OnPreferenceChangeListener { 35 36 private static final boolean DBG = false; 37 private static final String TAG = "QSB.SearchableItemsSettings"; 38 39 public static final String SEARCH_CORPORA_PREF = "search_corpora"; 40 41 private final SearchSettings mSearchSettings; 42 private final Corpora mCorpora; 43 private final Context mContext; 44 45 // References to the top-level preference objects 46 private PreferenceGroup mCorporaPreferences; 47 SearchableItemsController(SearchSettings searchSettings, Corpora corpora, Context context)48 public SearchableItemsController(SearchSettings searchSettings, Corpora corpora, 49 Context context) { 50 mSearchSettings = searchSettings; 51 mCorpora = corpora; 52 mContext = context; 53 } 54 handlePreference(Preference corporaPreferences)55 public void handlePreference(Preference corporaPreferences) { 56 mCorporaPreferences = (PreferenceGroup) corporaPreferences; 57 populateSourcePreference(); 58 } 59 getCorporaPreferenceKey()60 public String getCorporaPreferenceKey() { 61 return SEARCH_CORPORA_PREF; 62 } 63 getSettings()64 private SearchSettings getSettings() { 65 return mSearchSettings; 66 } 67 getCorpora()68 private Corpora getCorpora() { 69 return mCorpora; 70 } 71 getContext()72 private Context getContext() { 73 return mContext; 74 } 75 getResources()76 private Resources getResources() { 77 return getContext().getResources(); 78 } 79 80 81 /** 82 * Fills the suggestion source list. 83 */ populateSourcePreference()84 private void populateSourcePreference() { 85 boolean includeNonAllCorpora = 86 getResources().getBoolean(R.bool.show_non_all_corpora_in_settings); 87 mCorporaPreferences.setOrderingAsAdded(false); 88 for (Corpus corpus : getCorpora().getAllCorpora()) { 89 if (includeNonAllCorpora || corpus.includeInAll()) { 90 Preference pref = createCorpusPreference(corpus); 91 if (pref != null) { 92 if (DBG) Log.d(TAG, "Adding corpus: " + corpus); 93 mCorporaPreferences.addPreference(pref); 94 } 95 } 96 } 97 } 98 99 /** 100 * Adds a suggestion source to the list of suggestion source checkbox preferences. 101 */ createCorpusPreference(Corpus corpus)102 private Preference createCorpusPreference(Corpus corpus) { 103 SearchableItemPreference sourcePref = new SearchableItemPreference(getContext()); 104 sourcePref.setKey(SearchSettingsImpl.getCorpusEnabledPreference(corpus)); 105 // Put web corpus first. The rest are alphabetical. 106 if (corpus.isWebCorpus()) { 107 sourcePref.setOrder(0); 108 } 109 sourcePref.setDefaultValue(corpus.isCorpusDefaultEnabled()); 110 sourcePref.setOnPreferenceChangeListener(this); 111 CharSequence label = corpus.getLabel(); 112 sourcePref.setTitle(label); 113 CharSequence description = corpus.getSettingsDescription(); 114 sourcePref.setSummaryOn(description); 115 sourcePref.setSummaryOff(description); 116 sourcePref.setIcon(corpus.getCorpusIcon()); 117 return sourcePref; 118 } 119 onPreferenceChange(Preference preference, Object newValue)120 public boolean onPreferenceChange(Preference preference, Object newValue) { 121 getSettings().broadcastSettingsChanged(); 122 return true; 123 } 124 onCreateComplete()125 public void onCreateComplete() { 126 } 127 onStop()128 public void onStop() { 129 } 130 onDestroy()131 public void onDestroy() { 132 } 133 onResume()134 public void onResume() { 135 } 136 137 } 138