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 */
16package com.android.quicksearchbox.preferences;
17
18import com.android.quicksearchbox.QsbApplication;
19
20import android.os.Bundle;
21import android.preference.Preference;
22import android.preference.PreferenceCategory;
23import android.preference.PreferenceFragment;
24import android.preference.PreferenceGroup;
25
26/**
27 * System search settings fragment.
28 */
29public abstract class SettingsFragmentBase extends PreferenceFragment {
30
31    // Name of the preferences file used to store search preference
32    public static final String PREFERENCES_NAME = "SearchSettings";
33
34    private PreferenceController mController;
35
36    @Override
37    public void onCreate(Bundle savedInstanceState) {
38        super.onCreate(savedInstanceState);
39
40        mController = createController();
41
42        getPreferenceManager().setSharedPreferencesName(getPreferencesName());
43
44        addPreferencesFromResource(getPreferencesResourceId());
45
46        handlePreferenceGroup(getPreferenceScreen());
47
48        mController.onCreateComplete();
49    }
50
51    protected PreferenceController getController() {
52        return mController;
53    }
54
55    protected PreferenceControllerFactory createController() {
56        QsbApplication app = QsbApplication.get(getActivity());
57        return app.createPreferenceControllerFactory(getActivity());
58    }
59
60    protected String getPreferencesName() {
61        return PREFERENCES_NAME;
62    }
63
64    protected abstract int getPreferencesResourceId();
65
66    protected void handlePreferenceGroup(PreferenceGroup group) {
67        for (int i = 0; i < group.getPreferenceCount(); ++i) {
68            Preference p = group.getPreference(i);
69            if (p instanceof PreferenceCategory) {
70                handlePreferenceGroup((PreferenceCategory) p);
71            } else {
72                mController.handlePreference(group.getPreference(i));
73            }
74        }
75    }
76
77    @Override
78    public void onResume() {
79        super.onResume();
80        mController.onResume();
81    }
82
83    @Override
84    public void onStop() {
85        mController.onStop();
86        super.onStop();
87    }
88
89    @Override
90    public void onDestroy() {
91        mController.onDestroy();
92        super.onDestroy();
93    }
94}
95