Home | History | Annotate | Download | only in preferences
      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.QsbApplication;
     20 import com.android.quicksearchbox.SearchSettings;
     21 import com.android.quicksearchbox.ShortcutRepository;
     22 
     23 import android.content.Context;
     24 import android.preference.Preference;
     25 
     26 import java.util.ArrayList;
     27 
     28 /**
     29  * Class to handle logic behind the preferences in settings.
     30  */
     31 public class PreferenceControllerFactory implements PreferenceController {
     32 
     33     private final SearchSettings mSettings;
     34     private final Context mContext;
     35     private final ArrayList<PreferenceController> mControllers;
     36 
     37     public PreferenceControllerFactory(SearchSettings settings, Context context) {
     38         mSettings = settings;
     39         mContext = context;
     40         mControllers = new ArrayList<PreferenceController>();
     41     }
     42 
     43     protected Context getContext() {
     44         return mContext;
     45     }
     46 
     47     public void handlePreference(Preference p) {
     48         String key = p.getKey();
     49         if (key == null) return;
     50         if (SearchableItemsController.SEARCH_CORPORA_PREF.equals(key)) {
     51             Corpora corpora = QsbApplication.get(mContext).getCorpora();
     52             addController(new SearchableItemsController(mSettings, corpora, getContext()), p);
     53         } else if (ClearShortcutsController.CLEAR_SHORTCUTS_PREF.equals(key)) {
     54             ShortcutRepository shortcuts = QsbApplication.get(getContext()).getShortcutRepository();
     55             addController(new ClearShortcutsController(shortcuts), p);
     56         } else {
     57             throw new UnknownPreferenceException(p);
     58         }
     59     }
     60 
     61     public void onCreateComplete() {
     62         for (PreferenceController controller : mControllers) {
     63             controller.onCreateComplete();
     64         }
     65     }
     66 
     67     public void onResume() {
     68         for (PreferenceController controller : mControllers) {
     69             controller.onResume();
     70         }
     71     }
     72 
     73     public void onStop() {
     74         for (PreferenceController controller : mControllers) {
     75             controller.onStop();
     76         }
     77     }
     78 
     79     public void onDestroy() {
     80         for (PreferenceController controller : mControllers) {
     81             controller.onDestroy();
     82         }
     83     }
     84 
     85     protected void addController(PreferenceController controller, Preference forPreference) {
     86         mControllers.add(controller);
     87         controller.handlePreference(forPreference);
     88     }
     89 
     90     private static class UnknownPreferenceException extends RuntimeException {
     91         public UnknownPreferenceException(Preference p) {
     92             super("Preference key " + p.getKey() + "; class: " + p.getClass().toString());
     93         }
     94     }
     95 }
     96