PreferenceFragment.java revision 0daf40ca9a764088c3eb59be956674ac67b91384
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 android.preference;
18
19import android.app.Activity;
20import android.app.Fragment;
21import android.content.Intent;
22import android.content.SharedPreferences;
23import android.content.res.Configuration;
24import android.os.Bundle;
25import android.os.Handler;
26import android.os.Message;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.ListView;
31
32/**
33 * Shows a hierarchy of {@link Preference} objects as
34 * lists. These preferences will
35 * automatically save to {@link SharedPreferences} as the user interacts with
36 * them. To retrieve an instance of {@link SharedPreferences} that the
37 * preference hierarchy in this fragment will use, call
38 * {@link PreferenceManager#getDefaultSharedPreferences(android.content.Context)}
39 * with a context in the same package as this fragment.
40 * <p>
41 * Furthermore, the preferences shown will follow the visual style of system
42 * preferences. It is easy to create a hierarchy of preferences (that can be
43 * shown on multiple screens) via XML. For these reasons, it is recommended to
44 * use this fragment (as a superclass) to deal with preferences in applications.
45 * <p>
46 * A {@link PreferenceScreen} object should be at the top of the preference
47 * hierarchy. Furthermore, subsequent {@link PreferenceScreen} in the hierarchy
48 * denote a screen break--that is the preferences contained within subsequent
49 * {@link PreferenceScreen} should be shown on another screen. The preference
50 * framework handles showing these other screens from the preference hierarchy.
51 * <p>
52 * The preference hierarchy can be formed in multiple ways:
53 * <li> From an XML file specifying the hierarchy
54 * <li> From different {@link Activity Activities} that each specify its own
55 * preferences in an XML file via {@link Activity} meta-data
56 * <li> From an object hierarchy rooted with {@link PreferenceScreen}
57 * <p>
58 * To inflate from XML, use the {@link #addPreferencesFromResource(int)}. The
59 * root element should be a {@link PreferenceScreen}. Subsequent elements can point
60 * to actual {@link Preference} subclasses. As mentioned above, subsequent
61 * {@link PreferenceScreen} in the hierarchy will result in the screen break.
62 * <p>
63 * To specify an {@link Intent} to query {@link Activity Activities} that each
64 * have preferences, use {@link #addPreferencesFromIntent}. Each
65 * {@link Activity} can specify meta-data in the manifest (via the key
66 * {@link PreferenceManager#METADATA_KEY_PREFERENCES}) that points to an XML
67 * resource. These XML resources will be inflated into a single preference
68 * hierarchy and shown by this fragment.
69 * <p>
70 * To specify an object hierarchy rooted with {@link PreferenceScreen}, use
71 * {@link #setPreferenceScreen(PreferenceScreen)}.
72 * <p>
73 * As a convenience, this fragment implements a click listener for any
74 * preference in the current hierarchy, see
75 * {@link #onPreferenceTreeClick(PreferenceScreen, Preference)}.
76 *
77 * <a name="SampleCode"></a>
78 * <h3>Sample Code</h3>
79 *
80 * <p>The following sample code shows a simple preference fragment that is
81 * populated from a resource.  The resource it loads is:</p>
82 *
83 * {@sample development/samples/ApiDemos/res/xml/preferences.xml preferences}
84 *
85 * <p>The fragment implementation itself simply populates the preferences
86 * when created.  Note that the preferences framework takes care of loading
87 * the current values out of the app preferences and writing them when changed:</p>
88 *
89 * {@sample development/samples/ApiDemos/src/com/example/android/apis/preference/FragmentPreferences.java
90 *      fragment}
91 *
92 * @see Preference
93 * @see PreferenceScreen
94 */
95public abstract class PreferenceFragment extends Fragment implements
96        PreferenceManager.OnPreferenceTreeClickListener {
97
98    private static final String PREFERENCES_TAG = "android:preferences";
99
100    private PreferenceManager mPreferenceManager;
101    private ListView mList;
102    private boolean mHavePrefs;
103    private boolean mInitDone;
104
105    /**
106     * The starting request code given out to preference framework.
107     */
108    private static final int FIRST_REQUEST_CODE = 100;
109
110    private static final int MSG_BIND_PREFERENCES = 1;
111    private Handler mHandler = new Handler() {
112        @Override
113        public void handleMessage(Message msg) {
114            switch (msg.what) {
115
116                case MSG_BIND_PREFERENCES:
117                    bindPreferences();
118                    break;
119            }
120        }
121    };
122
123    final private Runnable mRequestFocus = new Runnable() {
124        public void run() {
125            mList.focusableViewAvailable(mList);
126        }
127    };
128
129    /**
130     * Interface that PreferenceFragment's containing activity should
131     * implement to be able to process preference items that wish to
132     * switch to a new fragment.
133     */
134    public interface OnPreferenceStartFragmentCallback {
135        /**
136         * Called when the user has clicked on a Preference that has
137         * a fragment class name associated with it.  The implementation
138         * to should instantiate and switch to an instance of the given
139         * fragment.
140         */
141        boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref);
142    }
143
144    @Override
145    public void onCreate(Bundle savedInstanceState) {
146        super.onCreate(savedInstanceState);
147        mPreferenceManager = new PreferenceManager(getActivity(), FIRST_REQUEST_CODE);
148        mPreferenceManager.setFragment(this);
149    }
150
151    @Override
152    public View onCreateView(LayoutInflater inflater, ViewGroup container,
153            Bundle savedInstanceState) {
154        return inflater.inflate(com.android.internal.R.layout.preference_list_fragment, container,
155                false);
156    }
157
158    @Override
159    public void onActivityCreated(Bundle savedInstanceState) {
160        super.onActivityCreated(savedInstanceState);
161        getListView().setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
162
163        if (mHavePrefs) {
164            bindPreferences();
165        }
166
167        mInitDone = true;
168
169        if (savedInstanceState != null) {
170            Bundle container = savedInstanceState.getBundle(PREFERENCES_TAG);
171            if (container != null) {
172                final PreferenceScreen preferenceScreen = getPreferenceScreen();
173                if (preferenceScreen != null) {
174                    preferenceScreen.restoreHierarchyState(container);
175                }
176            }
177        }
178    }
179
180    @Override
181    public void onStart() {
182        super.onStart();
183        mPreferenceManager.setOnPreferenceTreeClickListener(this);
184    }
185
186    @Override
187    public void onStop() {
188        super.onStop();
189        mPreferenceManager.dispatchActivityStop();
190        mPreferenceManager.setOnPreferenceTreeClickListener(null);
191    }
192
193    @Override
194    public void onDestroyView() {
195        mList = null;
196        mHandler.removeCallbacks(mRequestFocus);
197        mHandler.removeMessages(MSG_BIND_PREFERENCES);
198        super.onDestroyView();
199    }
200
201    @Override
202    public void onDestroy() {
203        super.onDestroy();
204        mPreferenceManager.dispatchActivityDestroy();
205    }
206
207    @Override
208    public void onSaveInstanceState(Bundle outState) {
209        super.onSaveInstanceState(outState);
210
211        final PreferenceScreen preferenceScreen = getPreferenceScreen();
212        if (preferenceScreen != null) {
213            Bundle container = new Bundle();
214            preferenceScreen.saveHierarchyState(container);
215            outState.putBundle(PREFERENCES_TAG, container);
216        }
217    }
218
219    @Override
220    public void onActivityResult(int requestCode, int resultCode, Intent data) {
221        super.onActivityResult(requestCode, resultCode, data);
222
223        mPreferenceManager.dispatchActivityResult(requestCode, resultCode, data);
224    }
225
226    /**
227     * Returns the {@link PreferenceManager} used by this fragment.
228     * @return The {@link PreferenceManager}.
229     */
230    public PreferenceManager getPreferenceManager() {
231        return mPreferenceManager;
232    }
233
234    /**
235     * Sets the root of the preference hierarchy that this fragment is showing.
236     *
237     * @param preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy.
238     */
239    public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
240        if (mPreferenceManager.setPreferences(preferenceScreen) && preferenceScreen != null) {
241            mHavePrefs = true;
242            if (mInitDone) {
243                postBindPreferences();
244            }
245        }
246    }
247
248    /**
249     * Gets the root of the preference hierarchy that this fragment is showing.
250     *
251     * @return The {@link PreferenceScreen} that is the root of the preference
252     *         hierarchy.
253     */
254    public PreferenceScreen getPreferenceScreen() {
255        return mPreferenceManager.getPreferenceScreen();
256    }
257
258    /**
259     * Adds preferences from activities that match the given {@link Intent}.
260     *
261     * @param intent The {@link Intent} to query activities.
262     */
263    public void addPreferencesFromIntent(Intent intent) {
264        requirePreferenceManager();
265
266        setPreferenceScreen(mPreferenceManager.inflateFromIntent(intent, getPreferenceScreen()));
267    }
268
269    /**
270     * Inflates the given XML resource and adds the preference hierarchy to the current
271     * preference hierarchy.
272     *
273     * @param preferencesResId The XML resource ID to inflate.
274     */
275    public void addPreferencesFromResource(int preferencesResId) {
276        requirePreferenceManager();
277
278        setPreferenceScreen(mPreferenceManager.inflateFromResource(getActivity(),
279                preferencesResId, getPreferenceScreen()));
280    }
281
282    /**
283     * {@inheritDoc}
284     */
285    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
286            Preference preference) {
287        if (preference.getFragment() != null &&
288                getActivity() instanceof OnPreferenceStartFragmentCallback) {
289            return ((OnPreferenceStartFragmentCallback)getActivity()).onPreferenceStartFragment(
290                    this, preference);
291        }
292        return false;
293    }
294
295    /**
296     * Finds a {@link Preference} based on its key.
297     *
298     * @param key The key of the preference to retrieve.
299     * @return The {@link Preference} with the key, or null.
300     * @see PreferenceGroup#findPreference(CharSequence)
301     */
302    public Preference findPreference(CharSequence key) {
303        if (mPreferenceManager == null) {
304            return null;
305        }
306        return mPreferenceManager.findPreference(key);
307    }
308
309    private void requirePreferenceManager() {
310        if (mPreferenceManager == null) {
311            throw new RuntimeException("This should be called after super.onCreate.");
312        }
313    }
314
315    private void postBindPreferences() {
316        if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) return;
317        mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget();
318    }
319
320    private void bindPreferences() {
321        final PreferenceScreen preferenceScreen = getPreferenceScreen();
322        if (preferenceScreen != null) {
323            preferenceScreen.bind(getListView());
324        }
325    }
326
327    /** @hide */
328    public ListView getListView() {
329        ensureList();
330        return mList;
331    }
332
333    private void ensureList() {
334        if (mList != null) {
335            return;
336        }
337        View root = getView();
338        if (root == null) {
339            throw new IllegalStateException("Content view not yet created");
340        }
341        View rawListView = root.findViewById(android.R.id.list);
342        if (!(rawListView instanceof ListView)) {
343            throw new RuntimeException(
344                    "Content has view with id attribute 'android.R.id.list' "
345                    + "that is not a ListView class");
346        }
347        mList = (ListView)rawListView;
348        if (mList == null) {
349            throw new RuntimeException(
350                    "Your content must have a ListView whose id attribute is " +
351                    "'android.R.id.list'");
352        }
353        mHandler.post(mRequestFocus);
354    }
355}
356