PreferenceActivity.java revision def1537e9e8d0dd190cde5310ddae8b921088c9b
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 (!"preference-headers".equals(nodeName)) {
468                throw new RuntimeException(
469                        "XML document must start with <preference-headers> tag; found"
470                        + nodeName + " at " + parser.getPositionDescription());
471            }
472
473            Bundle curBundle = null;
474
475            int outerDepth = parser.getDepth();
476            while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
477                   && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
478                if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
479                    continue;
480                }
481
482                nodeName = parser.getName();
483                if ("header".equals(nodeName)) {
484                    Header header = new Header();
485
486                    TypedArray sa = getResources().obtainAttributes(attrs,
487                            com.android.internal.R.styleable.PreferenceHeader);
488                    header.title = sa.getText(
489                            com.android.internal.R.styleable.PreferenceHeader_title);
490                    header.summary = sa.getText(
491                            com.android.internal.R.styleable.PreferenceHeader_summary);
492                    header.iconRes = sa.getResourceId(
493                            com.android.internal.R.styleable.PreferenceHeader_icon, 0);
494                    header.fragment = sa.getString(
495                            com.android.internal.R.styleable.PreferenceHeader_fragment);
496                    sa.recycle();
497
498                    if (curBundle == null) {
499                        curBundle = new Bundle();
500                    }
501                    getResources().parseBundleExtras(parser, curBundle);
502                    if (curBundle.size() > 0) {
503                        header.fragmentArguments = curBundle;
504                        curBundle = null;
505                    }
506
507                    target.add(header);
508                } else {
509                    XmlUtils.skipCurrentTag(parser);
510                }
511            }
512
513        } catch (XmlPullParserException e) {
514            throw new RuntimeException("Error parsing headers", e);
515        } catch (IOException e) {
516            throw new RuntimeException("Error parsing headers", e);
517        } finally {
518            if (parser != null) parser.close();
519        }
520
521    }
522
523    @Override
524    protected void onStop() {
525        super.onStop();
526
527        if (mPreferenceManager != null) {
528            mPreferenceManager.dispatchActivityStop();
529        }
530    }
531
532    @Override
533    protected void onDestroy() {
534        super.onDestroy();
535
536        if (mPreferenceManager != null) {
537            mPreferenceManager.dispatchActivityDestroy();
538        }
539    }
540
541    @Override
542    protected void onSaveInstanceState(Bundle outState) {
543        super.onSaveInstanceState(outState);
544
545        if (mPreferenceManager != null) {
546            final PreferenceScreen preferenceScreen = getPreferenceScreen();
547            if (preferenceScreen != null) {
548                Bundle container = new Bundle();
549                preferenceScreen.saveHierarchyState(container);
550                outState.putBundle(PREFERENCES_TAG, container);
551            }
552        }
553    }
554
555    @Override
556    protected void onRestoreInstanceState(Bundle state) {
557        if (mPreferenceManager != null) {
558            Bundle container = state.getBundle(PREFERENCES_TAG);
559            if (container != null) {
560                final PreferenceScreen preferenceScreen = getPreferenceScreen();
561                if (preferenceScreen != null) {
562                    preferenceScreen.restoreHierarchyState(container);
563                    mSavedInstanceState = state;
564                    return;
565                }
566            }
567        }
568
569        // Only call this if we didn't save the instance state for later.
570        // If we did save it, it will be restored when we bind the adapter.
571        super.onRestoreInstanceState(state);
572    }
573
574    @Override
575    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
576        super.onActivityResult(requestCode, resultCode, data);
577
578        if (mPreferenceManager != null) {
579            mPreferenceManager.dispatchActivityResult(requestCode, resultCode, data);
580        }
581    }
582
583    @Override
584    public void onContentChanged() {
585        super.onContentChanged();
586
587        if (mPreferenceManager != null) {
588            postBindPreferences();
589        }
590    }
591
592    @Override
593    protected void onListItemClick(ListView l, View v, int position, long id) {
594        super.onListItemClick(l, v, position, id);
595
596        if (mAdapter != null) {
597            onHeaderClick(mHeaders.get(position), position);
598        }
599    }
600
601    /**
602     * Called when the user selects an item in the header list.  The default
603     * implementation will call either {@link #startWithFragment(String, Bundle)}
604     * or {@link #switchToHeader(String, Bundle)} as appropriate.
605     *
606     * @param header The header that was selected.
607     * @param position The header's position in the list.
608     */
609    public void onHeaderClick(Header header, int position) {
610        if (mSinglePane) {
611            startWithFragment(header.fragment, header.fragmentArguments);
612        } else {
613            switchToHeader(header.fragment, header.fragmentArguments);
614        }
615    }
616
617    /**
618     * Start a new instance of this activity, showing only the given
619     * preference fragment.  When launched in this mode, the header list
620     * will be hidden and the given preference fragment will be instantiated
621     * and fill the entire activity.
622     *
623     * @param fragmentName The name of the fragment to display.
624     * @param args Optional arguments to supply to the fragment.
625     */
626    public void startWithFragment(String fragmentName, Bundle args) {
627        Intent intent = new Intent(Intent.ACTION_MAIN);
628        intent.setClass(this, getClass());
629        intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentName);
630        intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
631        intent.putExtra(EXTRA_NO_HEADERS, true);
632        startActivity(intent);
633    }
634
635    /**
636     * When in two-pane mode, switch the fragment pane to show the given
637     * preference fragment.
638     *
639     * @param fragmentName The name of the fragment to display.
640     * @param args Optional arguments to supply to the fragment.
641     */
642    public void switchToHeader(String fragmentName, Bundle args) {
643        getFragmentManager().popBackStack(BACK_STACK_PREFS, POP_BACK_STACK_INCLUSIVE);
644
645        Fragment f = Fragment.instantiate(this, fragmentName, args);
646        getFragmentManager().openTransaction().replace(
647                com.android.internal.R.id.prefs, f).commit();
648    }
649
650    @Override
651    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
652        Fragment f = Fragment.instantiate(this, pref.getFragment(), pref.getExtras());
653        getFragmentManager().openTransaction().replace(com.android.internal.R.id.prefs, f)
654                .addToBackStack(BACK_STACK_PREFS).commit();
655        return true;
656    }
657
658    /**
659     * Posts a message to bind the preferences to the list view.
660     * <p>
661     * Binding late is preferred as any custom preference types created in
662     * {@link #onCreate(Bundle)} are able to have their views recycled.
663     */
664    private void postBindPreferences() {
665        if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) return;
666        mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget();
667    }
668
669    private void bindPreferences() {
670        final PreferenceScreen preferenceScreen = getPreferenceScreen();
671        if (preferenceScreen != null) {
672            preferenceScreen.bind(getListView());
673            if (mSavedInstanceState != null) {
674                super.onRestoreInstanceState(mSavedInstanceState);
675                mSavedInstanceState = null;
676            }
677        }
678    }
679
680    /**
681     * Returns the {@link PreferenceManager} used by this activity.
682     * @return The {@link PreferenceManager}.
683     *
684     * @deprecated This function is not relevant for a modern fragment-based
685     * PreferenceActivity.
686     */
687    @Deprecated
688    public PreferenceManager getPreferenceManager() {
689        return mPreferenceManager;
690    }
691
692    private void requirePreferenceManager() {
693        if (mPreferenceManager == null) {
694            if (mAdapter == null) {
695                throw new RuntimeException("This should be called after super.onCreate.");
696            }
697            throw new RuntimeException(
698                    "Modern two-pane PreferenceActivity requires use of a PreferenceFragment");
699        }
700    }
701
702    /**
703     * Sets the root of the preference hierarchy that this activity is showing.
704     *
705     * @param preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy.
706     *
707     * @deprecated This function is not relevant for a modern fragment-based
708     * PreferenceActivity.
709     */
710    @Deprecated
711    public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
712        requirePreferenceManager();
713
714        if (mPreferenceManager.setPreferences(preferenceScreen) && preferenceScreen != null) {
715            postBindPreferences();
716            CharSequence title = getPreferenceScreen().getTitle();
717            // Set the title of the activity
718            if (title != null) {
719                setTitle(title);
720            }
721        }
722    }
723
724    /**
725     * Gets the root of the preference hierarchy that this activity is showing.
726     *
727     * @return The {@link PreferenceScreen} that is the root of the preference
728     *         hierarchy.
729     *
730     * @deprecated This function is not relevant for a modern fragment-based
731     * PreferenceActivity.
732     */
733    @Deprecated
734    public PreferenceScreen getPreferenceScreen() {
735        if (mPreferenceManager != null) {
736            return mPreferenceManager.getPreferenceScreen();
737        }
738        return null;
739    }
740
741    /**
742     * Adds preferences from activities that match the given {@link Intent}.
743     *
744     * @param intent The {@link Intent} to query activities.
745     *
746     * @deprecated This function is not relevant for a modern fragment-based
747     * PreferenceActivity.
748     */
749    @Deprecated
750    public void addPreferencesFromIntent(Intent intent) {
751        requirePreferenceManager();
752
753        setPreferenceScreen(mPreferenceManager.inflateFromIntent(intent, getPreferenceScreen()));
754    }
755
756    /**
757     * Inflates the given XML resource and adds the preference hierarchy to the current
758     * preference hierarchy.
759     *
760     * @param preferencesResId The XML resource ID to inflate.
761     *
762     * @deprecated This function is not relevant for a modern fragment-based
763     * PreferenceActivity.
764     */
765    @Deprecated
766    public void addPreferencesFromResource(int preferencesResId) {
767        requirePreferenceManager();
768
769        setPreferenceScreen(mPreferenceManager.inflateFromResource(this, preferencesResId,
770                getPreferenceScreen()));
771    }
772
773    /**
774     * {@inheritDoc}
775     *
776     * @deprecated This function is not relevant for a modern fragment-based
777     * PreferenceActivity.
778     */
779    @Deprecated
780    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
781        return false;
782    }
783
784    /**
785     * Finds a {@link Preference} based on its key.
786     *
787     * @param key The key of the preference to retrieve.
788     * @return The {@link Preference} with the key, or null.
789     * @see PreferenceGroup#findPreference(CharSequence)
790     *
791     * @deprecated This function is not relevant for a modern fragment-based
792     * PreferenceActivity.
793     */
794    @Deprecated
795    public Preference findPreference(CharSequence key) {
796
797        if (mPreferenceManager == null) {
798            return null;
799        }
800
801        return mPreferenceManager.findPreference(key);
802    }
803
804    @Override
805    protected void onNewIntent(Intent intent) {
806        if (mPreferenceManager != null) {
807            mPreferenceManager.dispatchNewIntent(intent);
808        }
809    }
810
811    // give subclasses access to the Next button
812    /** @hide */
813    protected boolean hasNextButton() {
814        return mNextButton != null;
815    }
816    /** @hide */
817    protected Button getNextButton() {
818        return mNextButton;
819    }
820}
821