PreferenceActivity.java revision 72dc780f57b4396b808032d592c41d35644a3277
1/*
2 * Copyright (C) 2007 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 com.android.internal.util.XmlUtils;
20
21import org.xmlpull.v1.XmlPullParser;
22import org.xmlpull.v1.XmlPullParserException;
23
24import android.app.Fragment;
25import android.app.ListActivity;
26import android.content.Context;
27import android.content.Intent;
28import android.content.res.Configuration;
29import android.content.res.TypedArray;
30import android.content.res.XmlResourceParser;
31import android.graphics.drawable.Drawable;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.Message;
35import android.text.TextUtils;
36import android.util.AttributeSet;
37import android.util.Log;
38import android.util.Xml;
39import android.view.LayoutInflater;
40import android.view.View;
41import android.view.ViewGroup;
42import android.view.View.OnClickListener;
43import android.widget.ArrayAdapter;
44import android.widget.Button;
45import android.widget.ImageView;
46import android.widget.ListView;
47import android.widget.TextView;
48
49import java.io.IOException;
50import java.util.ArrayList;
51import java.util.List;
52
53/**
54 * This is the base class for an activity to show a hierarchy of preferences
55 * to the user.  Prior to {@link android.os.Build.VERSION_CODES#HONEYCOMB}
56 * this class only allowed the display of a single set of preference; this
57 * functionality should now be found in the new {@link PreferenceFragment}
58 * class.  If you are using PreferenceActivity in its old mode, the documentation
59 * there applies to the deprecated APIs here.
60 *
61 * <p>This activity shows one or more headers of preferences, each of with
62 * is associated with a {@link PreferenceFragment} to display the preferences
63 * of that header.  The actual layout and display of these associations can
64 * however vary; currently there are two major approaches it may take:
65 *
66 * <ul>
67 * <li>On a small screen it may display only the headers as a single list
68 * when first launched.  Selecting one of the header items will re-launch
69 * the activity with it only showing the PreferenceFragment of that header.
70 * <li>On a large screen in may display both the headers and current
71 * PreferenceFragment together as panes.  Selecting a header item switches
72 * to showing the correct PreferenceFragment for that item.
73 * </ul>
74 *
75 * <p>Subclasses of PreferenceActivity should implement
76 * {@link #onBuildHeaders} to populate the header list with the desired
77 * items.  Doing this implicitly switches the class into its new "headers
78 * + fragments" mode rather than the old style of just showing a single
79 * preferences list.
80 *
81 * <a name="SampleCode"></a>
82 * <h3>Sample Code</h3>
83 *
84 * <p>The following sample code shows a simple preference activity that
85 * has two different sets of preferences.  The implementation, consisting
86 * of the activity itself as well as its two preference fragments is:</p>
87 *
88 * {@sample development/samples/ApiDemos/src/com/example/android/apis/preference/PreferenceWithHeaders.java
89 *      activity}
90 *
91 * <p>The preference_headers resource describes the headers to be displayed
92 * and the fragments associated with them.  It is:
93 *
94 * {@sample development/samples/ApiDemos/res/xml/preference_headers.xml headers}
95 *
96 * <p>The first header is shown by Prefs1Fragment, which populates itself
97 * from the following XML resource:</p>
98 *
99 * {@sample development/samples/ApiDemos/res/xml/fragmented_preferences.xml preferences}
100 *
101 * <p>Note that this XML resource contains a preference screen holding another
102 * fragment, the Prefs1FragmentInner implemented here.  This allows the user
103 * to traverse down a hierarchy of preferences; pressing back will pop each
104 * fragment off the stack to return to the previous preferences.
105 *
106 * <p>See {@link PreferenceFragment} for information on implementing the
107 * fragments themselves.
108 */
109public abstract class PreferenceActivity extends ListActivity implements
110        PreferenceManager.OnPreferenceTreeClickListener,
111        PreferenceFragment.OnPreferenceStartFragmentCallback {
112    private static final String TAG = "PreferenceActivity";
113
114    private static final String PREFERENCES_TAG = "android:preferences";
115
116    /**
117     * When starting this activity, the invoking Intent can contain this extra
118     * string to specify which fragment should be initially displayed.
119     */
120    public static final String EXTRA_SHOW_FRAGMENT = ":android:show_fragment";
121
122    /**
123     * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
124     * this extra can also be specify to supply a Bundle of arguments to pass
125     * to that fragment when it is instantiated during the initial creation
126     * of PreferenceActivity.
127     */
128    public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":android:show_fragment_args";
129
130    /**
131     * When starting this activity, the invoking Intent can contain this extra
132     * boolean that the header list should not be displayed.  This is most often
133     * used in conjunction with {@link #EXTRA_SHOW_FRAGMENT} to launch
134     * the activity to display a specific fragment that the user has navigated
135     * to.
136     */
137    public static final String EXTRA_NO_HEADERS = ":android:no_headers";
138
139    private static final String BACK_STACK_PREFS = ":android:prefs";
140
141    // extras that allow any preference activity to be launched as part of a wizard
142
143    // show Back and Next buttons? takes boolean parameter
144    // Back will then return RESULT_CANCELED and Next RESULT_OK
145    private static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
146
147    // add a Skip button?
148    private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
149
150    // specify custom text for the Back or Next buttons, or cause a button to not appear
151    // at all by setting it to null
152    private static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
153    private static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
154
155    // --- State for new mode when showing a list of headers + prefs fragment
156
157    private final ArrayList<Header> mHeaders = new ArrayList<Header>();
158
159    private HeaderAdapter mAdapter;
160
161    private View mPrefsContainer;
162
163    private boolean mSinglePane;
164
165    // --- State for old mode when showing a single preference list
166
167    private PreferenceManager mPreferenceManager;
168
169    private Bundle mSavedInstanceState;
170
171    // --- Common state
172
173    private Button mNextButton;
174
175    /**
176     * The starting request code given out to preference framework.
177     */
178    private static final int FIRST_REQUEST_CODE = 100;
179
180    private static final int MSG_BIND_PREFERENCES = 0;
181    private static final int MSG_BUILD_HEADERS = 1;
182    private Handler mHandler = new Handler() {
183        @Override
184        public void handleMessage(Message msg) {
185            switch (msg.what) {
186                case MSG_BIND_PREFERENCES:
187                    bindPreferences();
188                    break;
189                case MSG_BUILD_HEADERS:
190                    onBuildHeaders(mHeaders);
191                    mAdapter.notifyDataSetChanged();
192                    break;
193            }
194        }
195    };
196
197    private class HeaderViewHolder {
198        ImageView icon;
199        TextView title;
200        TextView summary;
201    }
202
203    private class HeaderAdapter extends ArrayAdapter<Header> {
204        private LayoutInflater mInflater;
205
206        public HeaderAdapter(Context context, List<Header> objects) {
207            super(context, 0, objects);
208            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
209        }
210
211        @Override
212        public View getView(int position, View convertView, ViewGroup parent) {
213            HeaderViewHolder holder;
214            View view;
215
216            if (convertView == null) {
217                view = mInflater.inflate(com.android.internal.R.layout.preference_list_item,
218                        parent, false);
219                holder = new HeaderViewHolder();
220                holder.icon = (ImageView)view.findViewById(
221                        com.android.internal.R.id.icon);
222                holder.title = (TextView)view.findViewById(
223                        com.android.internal.R.id.title);
224                holder.summary = (TextView)view.findViewById(
225                        com.android.internal.R.id.summary);
226                view.setTag(holder);
227            } else {
228                view = convertView;
229                holder = (HeaderViewHolder)view.getTag();
230            }
231
232            Header header = getItem(position);
233            if (header.icon != null) holder.icon.setImageDrawable(header.icon);
234            else if (header.iconRes != 0) holder.icon.setImageResource(header.iconRes);
235            if (header.title != null) holder.title.setText(header.title);
236            if (header.summary != null) holder.summary.setText(header.summary);
237
238            return view;
239        }
240    }
241
242    /**
243     * Description of a single Header item that the user can select.
244     */
245    public static class Header {
246        /**
247         * Title of the header that is shown to the user.
248         * @attr ref android.R.styleable#PreferenceHeader_title
249         */
250        CharSequence title;
251
252        /**
253         * Optional summary describing what this header controls.
254         * @attr ref android.R.styleable#PreferenceHeader_summary
255         */
256        CharSequence summary;
257
258        /**
259         * Optional icon resource to show for this header.
260         * @attr ref android.R.styleable#PreferenceHeader_icon
261         */
262        int iconRes;
263
264        /**
265         * Optional icon drawable to show for this header.  (If this is non-null,
266         * the iconRes will be ignored.)
267         */
268        Drawable icon;
269
270        /**
271         * Full class name of the fragment to display when this header is
272         * selected.
273         * @attr ref android.R.styleable#PreferenceHeader_fragment
274         */
275        String fragment;
276
277        /**
278         * Optional arguments to supply to the fragment when it is
279         * instantiated.
280         */
281        Bundle fragmentArguments;
282    }
283
284    @Override
285    protected void onCreate(Bundle savedInstanceState) {
286        super.onCreate(savedInstanceState);
287
288        setContentView(com.android.internal.R.layout.preference_list_content);
289
290        mPrefsContainer = findViewById(com.android.internal.R.id.prefs);
291        boolean hidingHeaders = onIsHidingHeaders();
292        mSinglePane = hidingHeaders || !onIsMultiPane();
293        String initialFragment = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
294        Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
295
296        if (initialFragment != null && mSinglePane) {
297            // If we are just showing a fragment, we want to run in
298            // new fragment mode, but don't need to compute and show
299            // the headers.
300            getListView().setVisibility(View.GONE);
301            mPrefsContainer.setVisibility(View.VISIBLE);
302            switchToHeader(initialFragment, initialArguments);
303
304        } else {
305            // We need to try to build the headers.
306            onBuildHeaders(mHeaders);
307
308            // If there are headers, then at this point we need to show
309            // them and, depending on the screen, we may also show in-line
310            // the currently selected preference fragment.
311            if (mHeaders.size() > 0) {
312                mAdapter = new HeaderAdapter(this, mHeaders);
313                setListAdapter(mAdapter);
314                if (!mSinglePane) {
315                    mPrefsContainer.setVisibility(View.VISIBLE);
316                    if (initialFragment != null) {
317                        Header h = onGetInitialHeader();
318                        initialFragment = h.fragment;
319                        initialArguments = h.fragmentArguments;
320                    }
321                    switchToHeader(initialFragment, initialArguments);
322                }
323
324            // If there are no headers, we are in the old "just show a screen
325            // of preferences" mode.
326            } else {
327                mPreferenceManager = new PreferenceManager(this, FIRST_REQUEST_CODE);
328                mPreferenceManager.setOnPreferenceTreeClickListener(this);
329            }
330        }
331
332        getListView().setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
333
334        // see if we should show Back/Next buttons
335        Intent intent = getIntent();
336        if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
337
338            findViewById(com.android.internal.R.id.button_bar).setVisibility(View.VISIBLE);
339
340            Button backButton = (Button)findViewById(com.android.internal.R.id.back_button);
341            backButton.setOnClickListener(new OnClickListener() {
342                public void onClick(View v) {
343                    setResult(RESULT_CANCELED);
344                    finish();
345                }
346            });
347            Button skipButton = (Button)findViewById(com.android.internal.R.id.skip_button);
348            skipButton.setOnClickListener(new OnClickListener() {
349                public void onClick(View v) {
350                    setResult(RESULT_OK);
351                    finish();
352                }
353            });
354            mNextButton = (Button)findViewById(com.android.internal.R.id.next_button);
355            mNextButton.setOnClickListener(new OnClickListener() {
356                public void onClick(View v) {
357                    setResult(RESULT_OK);
358                    finish();
359                }
360            });
361
362            // set our various button parameters
363            if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
364                String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
365                if (TextUtils.isEmpty(buttonText)) {
366                    mNextButton.setVisibility(View.GONE);
367                }
368                else {
369                    mNextButton.setText(buttonText);
370                }
371            }
372            if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
373                String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
374                if (TextUtils.isEmpty(buttonText)) {
375                    backButton.setVisibility(View.GONE);
376                }
377                else {
378                    backButton.setText(buttonText);
379                }
380            }
381            if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
382                skipButton.setVisibility(View.VISIBLE);
383            }
384        }
385    }
386
387    /**
388     * Called to determine if the activity should run in multi-pane mode.
389     * The default implementation returns true if the screen is large
390     * enough.
391     */
392    public boolean onIsMultiPane() {
393        Configuration config = getResources().getConfiguration();
394        if ((config.screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK)
395                == Configuration.SCREENLAYOUT_SIZE_XLARGE
396                && config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
397            return true;
398        }
399        return false;
400    }
401
402    /**
403     * Called to determine whether the header list should be hidden.  The
404     * default implementation hides the list if the activity is being re-launched
405     * when not in multi-pane mode.
406     */
407    public boolean onIsHidingHeaders() {
408        return getIntent().getBooleanExtra(EXTRA_NO_HEADERS, false);
409    }
410
411    /**
412     * Called to determine the initial header to be shown.  The default
413     * implementation simply returns the fragment of the first header.  Note
414     * that the returned Header object does not actually need to exist in
415     * your header list -- whatever its fragment is will simply be used to
416     * show for the initial UI.
417     */
418    public Header onGetInitialHeader() {
419        return mHeaders.get(0);
420    }
421
422    /**
423     * Called when the activity needs its list of headers build.  By
424     * implementing this and adding at least one item to the list, you
425     * will cause the activity to run in its modern fragment mode.  Note
426     * that this function may not always be called; for example, if the
427     * activity has been asked to display a particular fragment without
428     * the header list, there is no need to build the headers.
429     *
430     * <p>Typical implementations will use {@link #loadHeadersFromResource}
431     * to fill in the list from a resource.
432     *
433     * @param target The list in which to place the headers.
434     */
435    public void onBuildHeaders(List<Header> target) {
436    }
437
438    /**
439     * Call when you need to change the headers being displayed.  Will result
440     * in onBuildHeaders() later being called to retrieve the new list.
441     */
442    public void invalidateHeaders() {
443        if (!mHandler.hasMessages(MSG_BUILD_HEADERS)) {
444            mHandler.sendEmptyMessage(MSG_BUILD_HEADERS);
445        }
446    }
447
448    /**
449     * Parse the given XML file as a header description, adding each
450     * parsed Header into the target list.
451     *
452     * @param resid The XML resource to load and parse.
453     * @param target The list in which the parsed headers should be placed.
454     */
455    public void loadHeadersFromResource(int resid, List<Header> target) {
456        XmlResourceParser parser = null;
457        try {
458            parser = getResources().getXml(resid);
459            AttributeSet attrs = Xml.asAttributeSet(parser);
460
461            int type;
462            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
463                    && type != XmlPullParser.START_TAG) {
464            }
465
466            String nodeName = parser.getName();
467            if (!"PreferenceHeaders".equals(nodeName)) {
468                throw new RuntimeException(
469                        "XML document must start with <PreferenceHeaders> tag; found"
470                        + nodeName + " at " + parser.getPositionDescription());
471            }
472
473            int outerDepth = parser.getDepth();
474            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
475                   && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
476                if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
477                    continue;
478                }
479
480                nodeName = parser.getName();
481                if ("Header".equals(nodeName)) {
482                    Header header = new Header();
483
484                    TypedArray sa = getResources().obtainAttributes(attrs,
485                            com.android.internal.R.styleable.PreferenceHeader);
486                    header.title = sa.getText(
487                            com.android.internal.R.styleable.PreferenceHeader_title);
488                    header.summary = sa.getText(
489                            com.android.internal.R.styleable.PreferenceHeader_summary);
490                    header.iconRes = sa.getResourceId(
491                            com.android.internal.R.styleable.PreferenceHeader_icon, 0);
492                    header.fragment = sa.getString(
493                            com.android.internal.R.styleable.PreferenceHeader_fragment);
494                    sa.recycle();
495
496                    target.add(header);
497
498                    XmlUtils.skipCurrentTag(parser);
499                } else {
500                    XmlUtils.skipCurrentTag(parser);
501                }
502            }
503
504        } catch (XmlPullParserException e) {
505            throw new RuntimeException("Error parsing headers", e);
506        } catch (IOException e) {
507            throw new RuntimeException("Error parsing headers", e);
508        } finally {
509            if (parser != null) parser.close();
510        }
511
512    }
513
514    @Override
515    protected void onStop() {
516        super.onStop();
517
518        if (mPreferenceManager != null) {
519            mPreferenceManager.dispatchActivityStop();
520        }
521    }
522
523    @Override
524    protected void onDestroy() {
525        super.onDestroy();
526
527        if (mPreferenceManager != null) {
528            mPreferenceManager.dispatchActivityDestroy();
529        }
530    }
531
532    @Override
533    protected void onSaveInstanceState(Bundle outState) {
534        super.onSaveInstanceState(outState);
535
536        if (mPreferenceManager != null) {
537            final PreferenceScreen preferenceScreen = getPreferenceScreen();
538            if (preferenceScreen != null) {
539                Bundle container = new Bundle();
540                preferenceScreen.saveHierarchyState(container);
541                outState.putBundle(PREFERENCES_TAG, container);
542            }
543        }
544    }
545
546    @Override
547    protected void onRestoreInstanceState(Bundle state) {
548        if (mPreferenceManager != null) {
549            Bundle container = state.getBundle(PREFERENCES_TAG);
550            if (container != null) {
551                final PreferenceScreen preferenceScreen = getPreferenceScreen();
552                if (preferenceScreen != null) {
553                    preferenceScreen.restoreHierarchyState(container);
554                    mSavedInstanceState = state;
555                    return;
556                }
557            }
558        }
559
560        // Only call this if we didn't save the instance state for later.
561        // If we did save it, it will be restored when we bind the adapter.
562        super.onRestoreInstanceState(state);
563    }
564
565    @Override
566    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
567        super.onActivityResult(requestCode, resultCode, data);
568
569        if (mPreferenceManager != null) {
570            mPreferenceManager.dispatchActivityResult(requestCode, resultCode, data);
571        }
572    }
573
574    @Override
575    public void onContentChanged() {
576        super.onContentChanged();
577
578        if (mPreferenceManager != null) {
579            postBindPreferences();
580        }
581    }
582
583    @Override
584    protected void onListItemClick(ListView l, View v, int position, long id) {
585        super.onListItemClick(l, v, position, id);
586
587        if (mAdapter != null) {
588            onHeaderClick(mHeaders.get(position), position);
589        }
590    }
591
592    /**
593     * Called when the user selects an item in the header list.  The default
594     * implementation will call either {@link #startWithFragment(String, Bundle)}
595     * or {@link #switchToHeader(String, Bundle)} as appropriate.
596     *
597     * @param header The header that was selected.
598     * @param position The header's position in the list.
599     */
600    public void onHeaderClick(Header header, int position) {
601        if (mSinglePane) {
602            startWithFragment(header.fragment, header.fragmentArguments);
603        } else {
604            switchToHeader(header.fragment, header.fragmentArguments);
605        }
606    }
607
608    /**
609     * Start a new instance of this activity, showing only the given
610     * preference fragment.  When launched in this mode, the header list
611     * will be hidden and the given preference fragment will be instantiated
612     * and fill the entire activity.
613     *
614     * @param fragmentName The name of the fragment to display.
615     * @param args Optional arguments to supply to the fragment.
616     */
617    public void startWithFragment(String fragmentName, Bundle args) {
618        Intent intent = new Intent(Intent.ACTION_MAIN);
619        intent.setClass(this, getClass());
620        intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentName);
621        intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
622        intent.putExtra(EXTRA_NO_HEADERS, true);
623        startActivity(intent);
624    }
625
626    /**
627     * When in two-pane mode, switch the fragment pane to show the given
628     * preference fragment.
629     *
630     * @param fragmentName The name of the fragment to display.
631     * @param args Optional arguments to supply to the fragment.
632     */
633    public void switchToHeader(String fragmentName, Bundle args) {
634        popBackStack(BACK_STACK_PREFS, POP_BACK_STACK_INCLUSIVE);
635
636        Fragment f = Fragment.instantiate(this, fragmentName, args);
637        openFragmentTransaction().replace(com.android.internal.R.id.prefs, f).commit();
638    }
639
640    @Override
641    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
642        Fragment f = Fragment.instantiate(this, pref.getFragment());
643        openFragmentTransaction().replace(com.android.internal.R.id.prefs, f)
644                .addToBackStack(BACK_STACK_PREFS).commit();
645        return true;
646    }
647
648    /**
649     * Posts a message to bind the preferences to the list view.
650     * <p>
651     * Binding late is preferred as any custom preference types created in
652     * {@link #onCreate(Bundle)} are able to have their views recycled.
653     */
654    private void postBindPreferences() {
655        if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) return;
656        mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget();
657    }
658
659    private void bindPreferences() {
660        final PreferenceScreen preferenceScreen = getPreferenceScreen();
661        if (preferenceScreen != null) {
662            preferenceScreen.bind(getListView());
663            if (mSavedInstanceState != null) {
664                super.onRestoreInstanceState(mSavedInstanceState);
665                mSavedInstanceState = null;
666            }
667        }
668    }
669
670    /**
671     * Returns the {@link PreferenceManager} used by this activity.
672     * @return The {@link PreferenceManager}.
673     *
674     * @deprecated This function is not relevant for a modern fragment-based
675     * PreferenceActivity.
676     */
677    @Deprecated
678    public PreferenceManager getPreferenceManager() {
679        return mPreferenceManager;
680    }
681
682    private void requirePreferenceManager() {
683        if (mPreferenceManager == null) {
684            if (mAdapter == null) {
685                throw new RuntimeException("This should be called after super.onCreate.");
686            }
687            throw new RuntimeException(
688                    "Modern two-pane PreferenceActivity requires use of a PreferenceFragment");
689        }
690    }
691
692    /**
693     * Sets the root of the preference hierarchy that this activity is showing.
694     *
695     * @param preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy.
696     *
697     * @deprecated This function is not relevant for a modern fragment-based
698     * PreferenceActivity.
699     */
700    @Deprecated
701    public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
702        requirePreferenceManager();
703
704        if (mPreferenceManager.setPreferences(preferenceScreen) && preferenceScreen != null) {
705            postBindPreferences();
706            CharSequence title = getPreferenceScreen().getTitle();
707            // Set the title of the activity
708            if (title != null) {
709                setTitle(title);
710            }
711        }
712    }
713
714    /**
715     * Gets the root of the preference hierarchy that this activity is showing.
716     *
717     * @return The {@link PreferenceScreen} that is the root of the preference
718     *         hierarchy.
719     *
720     * @deprecated This function is not relevant for a modern fragment-based
721     * PreferenceActivity.
722     */
723    @Deprecated
724    public PreferenceScreen getPreferenceScreen() {
725        if (mPreferenceManager != null) {
726            return mPreferenceManager.getPreferenceScreen();
727        }
728        return null;
729    }
730
731    /**
732     * Adds preferences from activities that match the given {@link Intent}.
733     *
734     * @param intent The {@link Intent} to query activities.
735     *
736     * @deprecated This function is not relevant for a modern fragment-based
737     * PreferenceActivity.
738     */
739    @Deprecated
740    public void addPreferencesFromIntent(Intent intent) {
741        requirePreferenceManager();
742
743        setPreferenceScreen(mPreferenceManager.inflateFromIntent(intent, getPreferenceScreen()));
744    }
745
746    /**
747     * Inflates the given XML resource and adds the preference hierarchy to the current
748     * preference hierarchy.
749     *
750     * @param preferencesResId The XML resource ID to inflate.
751     *
752     * @deprecated This function is not relevant for a modern fragment-based
753     * PreferenceActivity.
754     */
755    @Deprecated
756    public void addPreferencesFromResource(int preferencesResId) {
757        requirePreferenceManager();
758
759        setPreferenceScreen(mPreferenceManager.inflateFromResource(this, preferencesResId,
760                getPreferenceScreen()));
761    }
762
763    /**
764     * {@inheritDoc}
765     *
766     * @deprecated This function is not relevant for a modern fragment-based
767     * PreferenceActivity.
768     */
769    @Deprecated
770    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
771        return false;
772    }
773
774    /**
775     * Finds a {@link Preference} based on its key.
776     *
777     * @param key The key of the preference to retrieve.
778     * @return The {@link Preference} with the key, or null.
779     * @see PreferenceGroup#findPreference(CharSequence)
780     *
781     * @deprecated This function is not relevant for a modern fragment-based
782     * PreferenceActivity.
783     */
784    @Deprecated
785    public Preference findPreference(CharSequence key) {
786
787        if (mPreferenceManager == null) {
788            return null;
789        }
790
791        return mPreferenceManager.findPreference(key);
792    }
793
794    @Override
795    protected void onNewIntent(Intent intent) {
796        if (mPreferenceManager != null) {
797            mPreferenceManager.dispatchNewIntent(intent);
798        }
799    }
800
801    // give subclasses access to the Next button
802    /** @hide */
803    protected boolean hasNextButton() {
804        return mNextButton != null;
805    }
806    /** @hide */
807    protected Button getNextButton() {
808        return mNextButton;
809    }
810}
811