PreferenceActivity.java revision 423d48bbba074ee1d0d7b09f4e9e074a6944edba
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 android.app.Fragment; 20import android.app.FragmentBreadCrumbs; 21import android.app.FragmentManager; 22import android.app.FragmentTransaction; 23import android.app.ListActivity; 24import android.content.Context; 25import android.content.Intent; 26import android.content.res.Resources; 27import android.content.res.TypedArray; 28import android.content.res.XmlResourceParser; 29import android.os.Bundle; 30import android.os.Handler; 31import android.os.Message; 32import android.os.Parcel; 33import android.os.Parcelable; 34import android.text.TextUtils; 35import android.util.AttributeSet; 36import android.util.TypedValue; 37import android.util.Xml; 38import android.view.LayoutInflater; 39import android.view.View; 40import android.view.View.OnClickListener; 41import android.view.ViewGroup; 42import android.widget.AbsListView; 43import android.widget.ArrayAdapter; 44import android.widget.BaseAdapter; 45import android.widget.Button; 46import android.widget.FrameLayout; 47import android.widget.ImageView; 48import android.widget.ListView; 49import android.widget.TextView; 50 51import com.android.internal.util.XmlUtils; 52 53import org.xmlpull.v1.XmlPullParser; 54import org.xmlpull.v1.XmlPullParserException; 55 56import java.io.IOException; 57import java.util.ArrayList; 58import java.util.List; 59 60/** 61 * This is the base class for an activity to show a hierarchy of preferences 62 * to the user. Prior to {@link android.os.Build.VERSION_CODES#HONEYCOMB} 63 * this class only allowed the display of a single set of preference; this 64 * functionality should now be found in the new {@link PreferenceFragment} 65 * class. If you are using PreferenceActivity in its old mode, the documentation 66 * there applies to the deprecated APIs here. 67 * 68 * <p>This activity shows one or more headers of preferences, each of which 69 * is associated with a {@link PreferenceFragment} to display the preferences 70 * of that header. The actual layout and display of these associations can 71 * however vary; currently there are two major approaches it may take: 72 * 73 * <ul> 74 * <li>On a small screen it may display only the headers as a single list 75 * when first launched. Selecting one of the header items will re-launch 76 * the activity with it only showing the PreferenceFragment of that header. 77 * <li>On a large screen in may display both the headers and current 78 * PreferenceFragment together as panes. Selecting a header item switches 79 * to showing the correct PreferenceFragment for that item. 80 * </ul> 81 * 82 * <p>Subclasses of PreferenceActivity should implement 83 * {@link #onBuildHeaders} to populate the header list with the desired 84 * items. Doing this implicitly switches the class into its new "headers 85 * + fragments" mode rather than the old style of just showing a single 86 * preferences list. 87 * 88 * <a name="SampleCode"></a> 89 * <h3>Sample Code</h3> 90 * 91 * <p>The following sample code shows a simple preference activity that 92 * has two different sets of preferences. The implementation, consisting 93 * of the activity itself as well as its two preference fragments is:</p> 94 * 95 * {@sample development/samples/ApiDemos/src/com/example/android/apis/preference/PreferenceWithHeaders.java 96 * activity} 97 * 98 * <p>The preference_headers resource describes the headers to be displayed 99 * and the fragments associated with them. It is: 100 * 101 * {@sample development/samples/ApiDemos/res/xml/preference_headers.xml headers} 102 * 103 * <p>The first header is shown by Prefs1Fragment, which populates itself 104 * from the following XML resource:</p> 105 * 106 * {@sample development/samples/ApiDemos/res/xml/fragmented_preferences.xml preferences} 107 * 108 * <p>Note that this XML resource contains a preference screen holding another 109 * fragment, the Prefs1FragmentInner implemented here. This allows the user 110 * to traverse down a hierarchy of preferences; pressing back will pop each 111 * fragment off the stack to return to the previous preferences. 112 * 113 * <p>See {@link PreferenceFragment} for information on implementing the 114 * fragments themselves. 115 */ 116public abstract class PreferenceActivity extends ListActivity implements 117 PreferenceManager.OnPreferenceTreeClickListener, 118 PreferenceFragment.OnPreferenceStartFragmentCallback { 119 120 // Constants for state save/restore 121 private static final String HEADERS_TAG = ":android:headers"; 122 private static final String CUR_HEADER_TAG = ":android:cur_header"; 123 private static final String PREFERENCES_TAG = ":android:preferences"; 124 125 /** 126 * When starting this activity, the invoking Intent can contain this extra 127 * string to specify which fragment should be initially displayed. 128 */ 129 public static final String EXTRA_SHOW_FRAGMENT = ":android:show_fragment"; 130 131 /** 132 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT}, 133 * this extra can also be specified to supply a Bundle of arguments to pass 134 * to that fragment when it is instantiated during the initial creation 135 * of PreferenceActivity. 136 */ 137 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":android:show_fragment_args"; 138 139 /** 140 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT}, 141 * this extra can also be specify to supply the title to be shown for 142 * that fragment. 143 */ 144 public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":android:show_fragment_title"; 145 146 /** 147 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT}, 148 * this extra can also be specify to supply the short title to be shown for 149 * that fragment. 150 */ 151 public static final String EXTRA_SHOW_FRAGMENT_SHORT_TITLE 152 = ":android:show_fragment_short_title"; 153 154 /** 155 * When starting this activity, the invoking Intent can contain this extra 156 * boolean that the header list should not be displayed. This is most often 157 * used in conjunction with {@link #EXTRA_SHOW_FRAGMENT} to launch 158 * the activity to display a specific fragment that the user has navigated 159 * to. 160 */ 161 public static final String EXTRA_NO_HEADERS = ":android:no_headers"; 162 163 private static final String BACK_STACK_PREFS = ":android:prefs"; 164 165 // extras that allow any preference activity to be launched as part of a wizard 166 167 // show Back and Next buttons? takes boolean parameter 168 // Back will then return RESULT_CANCELED and Next RESULT_OK 169 private static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar"; 170 171 // add a Skip button? 172 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip"; 173 174 // specify custom text for the Back or Next buttons, or cause a button to not appear 175 // at all by setting it to null 176 private static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text"; 177 private static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text"; 178 179 // --- State for new mode when showing a list of headers + prefs fragment 180 181 private final ArrayList<Header> mHeaders = new ArrayList<Header>(); 182 183 private FrameLayout mListFooter; 184 185 private ViewGroup mPrefsContainer; 186 187 private FragmentBreadCrumbs mFragmentBreadCrumbs; 188 189 private boolean mSinglePane; 190 191 private Header mCurHeader; 192 193 // --- State for old mode when showing a single preference list 194 195 private PreferenceManager mPreferenceManager; 196 197 private Bundle mSavedInstanceState; 198 199 // --- Common state 200 201 private Button mNextButton; 202 203 /** 204 * The starting request code given out to preference framework. 205 */ 206 private static final int FIRST_REQUEST_CODE = 100; 207 208 private static final int MSG_BIND_PREFERENCES = 1; 209 private static final int MSG_BUILD_HEADERS = 2; 210 private Handler mHandler = new Handler() { 211 @Override 212 public void handleMessage(Message msg) { 213 switch (msg.what) { 214 case MSG_BIND_PREFERENCES: { 215 bindPreferences(); 216 } break; 217 case MSG_BUILD_HEADERS: { 218 ArrayList<Header> oldHeaders = new ArrayList<Header>(mHeaders); 219 mHeaders.clear(); 220 onBuildHeaders(mHeaders); 221 if (mAdapter instanceof BaseAdapter) { 222 ((BaseAdapter) mAdapter).notifyDataSetChanged(); 223 } 224 Header header = onGetNewHeader(); 225 if (header != null && header.fragment != null) { 226 Header mappedHeader = findBestMatchingHeader(header, oldHeaders); 227 if (mappedHeader == null || mCurHeader != mappedHeader) { 228 switchToHeader(header); 229 } 230 } else if (mCurHeader != null) { 231 Header mappedHeader = findBestMatchingHeader(mCurHeader, mHeaders); 232 if (mappedHeader != null) { 233 setSelectedHeader(mappedHeader); 234 } 235 } 236 } break; 237 } 238 } 239 }; 240 241 private static class HeaderAdapter extends ArrayAdapter<Header> { 242 private static class HeaderViewHolder { 243 ImageView icon; 244 TextView title; 245 TextView summary; 246 } 247 248 private LayoutInflater mInflater; 249 250 public HeaderAdapter(Context context, List<Header> objects) { 251 super(context, 0, objects); 252 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 253 } 254 255 @Override 256 public View getView(int position, View convertView, ViewGroup parent) { 257 HeaderViewHolder holder; 258 View view; 259 260 if (convertView == null) { 261 view = mInflater.inflate(com.android.internal.R.layout.preference_header_item, 262 parent, false); 263 holder = new HeaderViewHolder(); 264 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon); 265 holder.title = (TextView) view.findViewById(com.android.internal.R.id.title); 266 holder.summary = (TextView) view.findViewById(com.android.internal.R.id.summary); 267 view.setTag(holder); 268 } else { 269 view = convertView; 270 holder = (HeaderViewHolder) view.getTag(); 271 } 272 273 // All view fields must be updated every time, because the view may be recycled 274 Header header = getItem(position); 275 holder.icon.setImageResource(header.iconRes); 276 holder.title.setText(header.getTitle(getContext().getResources())); 277 CharSequence summary = header.getSummary(getContext().getResources()); 278 if (!TextUtils.isEmpty(summary)) { 279 holder.summary.setVisibility(View.VISIBLE); 280 holder.summary.setText(summary); 281 } else { 282 holder.summary.setVisibility(View.GONE); 283 } 284 285 return view; 286 } 287 } 288 289 /** 290 * Default value for {@link Header#id Header.id} indicating that no 291 * identifier value is set. All other values (including those below -1) 292 * are valid. 293 */ 294 public static final long HEADER_ID_UNDEFINED = -1; 295 296 /** 297 * Description of a single Header item that the user can select. 298 */ 299 public static final class Header implements Parcelable { 300 /** 301 * Identifier for this header, to correlate with a new list when 302 * it is updated. The default value is 303 * {@link PreferenceActivity#HEADER_ID_UNDEFINED}, meaning no id. 304 * @attr ref android.R.styleable#PreferenceHeader_id 305 */ 306 public long id = HEADER_ID_UNDEFINED; 307 308 /** 309 * Resource ID of title of the header that is shown to the user. 310 * @attr ref android.R.styleable#PreferenceHeader_title 311 */ 312 public int titleRes; 313 314 /** 315 * Title of the header that is shown to the user. 316 * @attr ref android.R.styleable#PreferenceHeader_title 317 */ 318 public CharSequence title; 319 320 /** 321 * Resource ID of optional summary describing what this header controls. 322 * @attr ref android.R.styleable#PreferenceHeader_summary 323 */ 324 public int summaryRes; 325 326 /** 327 * Optional summary describing what this header controls. 328 * @attr ref android.R.styleable#PreferenceHeader_summary 329 */ 330 public CharSequence summary; 331 332 /** 333 * Resource ID of optional text to show as the title in the bread crumb. 334 * @attr ref android.R.styleable#PreferenceHeader_breadCrumbTitle 335 */ 336 public int breadCrumbTitleRes; 337 338 /** 339 * Optional text to show as the title in the bread crumb. 340 * @attr ref android.R.styleable#PreferenceHeader_breadCrumbTitle 341 */ 342 public CharSequence breadCrumbTitle; 343 344 /** 345 * Resource ID of optional text to show as the short title in the bread crumb. 346 * @attr ref android.R.styleable#PreferenceHeader_breadCrumbShortTitle 347 */ 348 public int breadCrumbShortTitleRes; 349 350 /** 351 * Optional text to show as the short title in the bread crumb. 352 * @attr ref android.R.styleable#PreferenceHeader_breadCrumbShortTitle 353 */ 354 public CharSequence breadCrumbShortTitle; 355 356 /** 357 * Optional icon resource to show for this header. 358 * @attr ref android.R.styleable#PreferenceHeader_icon 359 */ 360 public int iconRes; 361 362 /** 363 * Full class name of the fragment to display when this header is 364 * selected. 365 * @attr ref android.R.styleable#PreferenceHeader_fragment 366 */ 367 public String fragment; 368 369 /** 370 * Optional arguments to supply to the fragment when it is 371 * instantiated. 372 */ 373 public Bundle fragmentArguments; 374 375 /** 376 * Intent to launch when the preference is selected. 377 */ 378 public Intent intent; 379 380 /** 381 * Optional additional data for use by subclasses of PreferenceActivity. 382 */ 383 public Bundle extras; 384 385 public Header() { 386 // Empty 387 } 388 389 /** 390 * Return the currently set title. If {@link #titleRes} is set, 391 * this resource is loaded from <var>res</var> and returned. Otherwise 392 * {@link #title} is returned. 393 */ 394 public CharSequence getTitle(Resources res) { 395 if (titleRes != 0) { 396 return res.getText(titleRes); 397 } 398 return title; 399 } 400 401 /** 402 * Return the currently set summary. If {@link #summaryRes} is set, 403 * this resource is loaded from <var>res</var> and returned. Otherwise 404 * {@link #summary} is returned. 405 */ 406 public CharSequence getSummary(Resources res) { 407 if (summaryRes != 0) { 408 return res.getText(summaryRes); 409 } 410 return summary; 411 } 412 413 /** 414 * Return the currently set bread crumb title. If {@link #breadCrumbTitleRes} is set, 415 * this resource is loaded from <var>res</var> and returned. Otherwise 416 * {@link #breadCrumbTitle} is returned. 417 */ 418 public CharSequence getBreadCrumbTitle(Resources res) { 419 if (breadCrumbTitleRes != 0) { 420 return res.getText(breadCrumbTitleRes); 421 } 422 return breadCrumbTitle; 423 } 424 425 /** 426 * Return the currently set bread crumb short title. If 427 * {@link #breadCrumbShortTitleRes} is set, 428 * this resource is loaded from <var>res</var> and returned. Otherwise 429 * {@link #breadCrumbShortTitle} is returned. 430 */ 431 public CharSequence getBreadCrumbShortTitle(Resources res) { 432 if (breadCrumbShortTitleRes != 0) { 433 return res.getText(breadCrumbShortTitleRes); 434 } 435 return breadCrumbShortTitle; 436 } 437 438 @Override 439 public int describeContents() { 440 return 0; 441 } 442 443 @Override 444 public void writeToParcel(Parcel dest, int flags) { 445 dest.writeLong(id); 446 dest.writeInt(titleRes); 447 TextUtils.writeToParcel(title, dest, flags); 448 dest.writeInt(summaryRes); 449 TextUtils.writeToParcel(summary, dest, flags); 450 dest.writeInt(breadCrumbTitleRes); 451 TextUtils.writeToParcel(breadCrumbTitle, dest, flags); 452 dest.writeInt(breadCrumbShortTitleRes); 453 TextUtils.writeToParcel(breadCrumbShortTitle, dest, flags); 454 dest.writeInt(iconRes); 455 dest.writeString(fragment); 456 dest.writeBundle(fragmentArguments); 457 if (intent != null) { 458 dest.writeInt(1); 459 intent.writeToParcel(dest, flags); 460 } else { 461 dest.writeInt(0); 462 } 463 dest.writeBundle(extras); 464 } 465 466 public void readFromParcel(Parcel in) { 467 id = in.readLong(); 468 titleRes = in.readInt(); 469 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 470 summaryRes = in.readInt(); 471 summary = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 472 breadCrumbTitleRes = in.readInt(); 473 breadCrumbTitle = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 474 breadCrumbShortTitleRes = in.readInt(); 475 breadCrumbShortTitle = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 476 iconRes = in.readInt(); 477 fragment = in.readString(); 478 fragmentArguments = in.readBundle(); 479 if (in.readInt() != 0) { 480 intent = Intent.CREATOR.createFromParcel(in); 481 } 482 extras = in.readBundle(); 483 } 484 485 Header(Parcel in) { 486 readFromParcel(in); 487 } 488 489 public static final Creator<Header> CREATOR = new Creator<Header>() { 490 public Header createFromParcel(Parcel source) { 491 return new Header(source); 492 } 493 public Header[] newArray(int size) { 494 return new Header[size]; 495 } 496 }; 497 } 498 499 @Override 500 protected void onCreate(Bundle savedInstanceState) { 501 super.onCreate(savedInstanceState); 502 503 setContentView(com.android.internal.R.layout.preference_list_content); 504 505 mListFooter = (FrameLayout)findViewById(com.android.internal.R.id.list_footer); 506 mPrefsContainer = (ViewGroup) findViewById(com.android.internal.R.id.prefs_frame); 507 boolean hidingHeaders = onIsHidingHeaders(); 508 mSinglePane = hidingHeaders || !onIsMultiPane(); 509 String initialFragment = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT); 510 Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS); 511 int initialTitle = getIntent().getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE, 0); 512 int initialShortTitle = getIntent().getIntExtra(EXTRA_SHOW_FRAGMENT_SHORT_TITLE, 0); 513 514 if (savedInstanceState != null) { 515 // We are restarting from a previous saved state; used that to 516 // initialize, instead of starting fresh. 517 ArrayList<Header> headers = savedInstanceState.getParcelableArrayList(HEADERS_TAG); 518 if (headers != null) { 519 mHeaders.addAll(headers); 520 int curHeader = savedInstanceState.getInt(CUR_HEADER_TAG, 521 (int) HEADER_ID_UNDEFINED); 522 if (curHeader >= 0 && curHeader < mHeaders.size()) { 523 setSelectedHeader(mHeaders.get(curHeader)); 524 } 525 } 526 527 } else { 528 if (initialFragment != null && mSinglePane) { 529 // If we are just showing a fragment, we want to run in 530 // new fragment mode, but don't need to compute and show 531 // the headers. 532 switchToHeader(initialFragment, initialArguments); 533 if (initialTitle != 0) { 534 CharSequence initialTitleStr = getText(initialTitle); 535 CharSequence initialShortTitleStr = initialShortTitle != 0 536 ? getText(initialShortTitle) : null; 537 showBreadCrumbs(initialTitleStr, initialShortTitleStr); 538 } 539 540 } else { 541 // We need to try to build the headers. 542 onBuildHeaders(mHeaders); 543 544 // If there are headers, then at this point we need to show 545 // them and, depending on the screen, we may also show in-line 546 // the currently selected preference fragment. 547 if (mHeaders.size() > 0) { 548 if (!mSinglePane) { 549 if (initialFragment == null) { 550 Header h = onGetInitialHeader(); 551 switchToHeader(h); 552 } else { 553 switchToHeader(initialFragment, initialArguments); 554 } 555 } 556 } 557 } 558 } 559 560 // The default configuration is to only show the list view. Adjust 561 // visibility for other configurations. 562 if (initialFragment != null && mSinglePane) { 563 // Single pane, showing just a prefs fragment. 564 findViewById(com.android.internal.R.id.headers).setVisibility(View.GONE); 565 mPrefsContainer.setVisibility(View.VISIBLE); 566 if (initialTitle != 0) { 567 CharSequence initialTitleStr = getText(initialTitle); 568 CharSequence initialShortTitleStr = initialShortTitle != 0 569 ? getText(initialShortTitle) : null; 570 showBreadCrumbs(initialTitleStr, initialShortTitleStr); 571 } 572 } else if (mHeaders.size() > 0) { 573 setListAdapter(new HeaderAdapter(this, mHeaders)); 574 if (!mSinglePane) { 575 // Multi-pane. 576 getListView().setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); 577 if (mCurHeader != null) { 578 setSelectedHeader(mCurHeader); 579 } 580 mPrefsContainer.setVisibility(View.VISIBLE); 581 } 582 } else { 583 // If there are no headers, we are in the old "just show a screen 584 // of preferences" mode. 585 setContentView(com.android.internal.R.layout.preference_list_content_single); 586 mListFooter = (FrameLayout) findViewById(com.android.internal.R.id.list_footer); 587 mPrefsContainer = (ViewGroup) findViewById(com.android.internal.R.id.prefs); 588 mPreferenceManager = new PreferenceManager(this, FIRST_REQUEST_CODE); 589 mPreferenceManager.setOnPreferenceTreeClickListener(this); 590 } 591 592 // see if we should show Back/Next buttons 593 Intent intent = getIntent(); 594 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) { 595 596 findViewById(com.android.internal.R.id.button_bar).setVisibility(View.VISIBLE); 597 598 Button backButton = (Button)findViewById(com.android.internal.R.id.back_button); 599 backButton.setOnClickListener(new OnClickListener() { 600 public void onClick(View v) { 601 setResult(RESULT_CANCELED); 602 finish(); 603 } 604 }); 605 Button skipButton = (Button)findViewById(com.android.internal.R.id.skip_button); 606 skipButton.setOnClickListener(new OnClickListener() { 607 public void onClick(View v) { 608 setResult(RESULT_OK); 609 finish(); 610 } 611 }); 612 mNextButton = (Button)findViewById(com.android.internal.R.id.next_button); 613 mNextButton.setOnClickListener(new OnClickListener() { 614 public void onClick(View v) { 615 setResult(RESULT_OK); 616 finish(); 617 } 618 }); 619 620 // set our various button parameters 621 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) { 622 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT); 623 if (TextUtils.isEmpty(buttonText)) { 624 mNextButton.setVisibility(View.GONE); 625 } 626 else { 627 mNextButton.setText(buttonText); 628 } 629 } 630 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) { 631 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT); 632 if (TextUtils.isEmpty(buttonText)) { 633 backButton.setVisibility(View.GONE); 634 } 635 else { 636 backButton.setText(buttonText); 637 } 638 } 639 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) { 640 skipButton.setVisibility(View.VISIBLE); 641 } 642 } 643 } 644 645 /** 646 * Returns true if this activity is currently showing the header list. 647 */ 648 public boolean hasHeaders() { 649 return getListView().getVisibility() == View.VISIBLE 650 && mPreferenceManager == null; 651 } 652 653 /** 654 * Returns the Header list 655 * @hide 656 */ 657 public List<Header> getHeaders() { 658 return mHeaders; 659 } 660 661 /** 662 * Returns true if this activity is showing multiple panes -- the headers 663 * and a preference fragment. 664 */ 665 public boolean isMultiPane() { 666 return hasHeaders() && mPrefsContainer.getVisibility() == View.VISIBLE; 667 } 668 669 /** 670 * Called to determine if the activity should run in multi-pane mode. 671 * The default implementation returns true if the screen is large 672 * enough. 673 */ 674 public boolean onIsMultiPane() { 675 boolean preferMultiPane = getResources().getBoolean( 676 com.android.internal.R.bool.preferences_prefer_dual_pane); 677 return preferMultiPane; 678 } 679 680 /** 681 * Called to determine whether the header list should be hidden. 682 * The default implementation returns the 683 * value given in {@link #EXTRA_NO_HEADERS} or false if it is not supplied. 684 * This is set to false, for example, when the activity is being re-launched 685 * to show a particular preference activity. 686 */ 687 public boolean onIsHidingHeaders() { 688 return getIntent().getBooleanExtra(EXTRA_NO_HEADERS, false); 689 } 690 691 /** 692 * Called to determine the initial header to be shown. The default 693 * implementation simply returns the fragment of the first header. Note 694 * that the returned Header object does not actually need to exist in 695 * your header list -- whatever its fragment is will simply be used to 696 * show for the initial UI. 697 */ 698 public Header onGetInitialHeader() { 699 return mHeaders.get(0); 700 } 701 702 /** 703 * Called after the header list has been updated ({@link #onBuildHeaders} 704 * has been called and returned due to {@link #invalidateHeaders()}) to 705 * specify the header that should now be selected. The default implementation 706 * returns null to keep whatever header is currently selected. 707 */ 708 public Header onGetNewHeader() { 709 return null; 710 } 711 712 /** 713 * Called when the activity needs its list of headers build. By 714 * implementing this and adding at least one item to the list, you 715 * will cause the activity to run in its modern fragment mode. Note 716 * that this function may not always be called; for example, if the 717 * activity has been asked to display a particular fragment without 718 * the header list, there is no need to build the headers. 719 * 720 * <p>Typical implementations will use {@link #loadHeadersFromResource} 721 * to fill in the list from a resource. 722 * 723 * @param target The list in which to place the headers. 724 */ 725 public void onBuildHeaders(List<Header> target) { 726 // Should be overloaded by subclasses 727 } 728 729 /** 730 * Call when you need to change the headers being displayed. Will result 731 * in onBuildHeaders() later being called to retrieve the new list. 732 */ 733 public void invalidateHeaders() { 734 if (!mHandler.hasMessages(MSG_BUILD_HEADERS)) { 735 mHandler.sendEmptyMessage(MSG_BUILD_HEADERS); 736 } 737 } 738 739 /** 740 * Parse the given XML file as a header description, adding each 741 * parsed Header into the target list. 742 * 743 * @param resid The XML resource to load and parse. 744 * @param target The list in which the parsed headers should be placed. 745 */ 746 public void loadHeadersFromResource(int resid, List<Header> target) { 747 XmlResourceParser parser = null; 748 try { 749 parser = getResources().getXml(resid); 750 AttributeSet attrs = Xml.asAttributeSet(parser); 751 752 int type; 753 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 754 && type != XmlPullParser.START_TAG) { 755 // Parse next until start tag is found 756 } 757 758 String nodeName = parser.getName(); 759 if (!"preference-headers".equals(nodeName)) { 760 throw new RuntimeException( 761 "XML document must start with <preference-headers> tag; found" 762 + nodeName + " at " + parser.getPositionDescription()); 763 } 764 765 Bundle curBundle = null; 766 767 final int outerDepth = parser.getDepth(); 768 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 769 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 770 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 771 continue; 772 } 773 774 nodeName = parser.getName(); 775 if ("header".equals(nodeName)) { 776 Header header = new Header(); 777 778 TypedArray sa = getResources().obtainAttributes(attrs, 779 com.android.internal.R.styleable.PreferenceHeader); 780 header.id = sa.getResourceId( 781 com.android.internal.R.styleable.PreferenceHeader_id, 782 (int)HEADER_ID_UNDEFINED); 783 TypedValue tv = sa.peekValue( 784 com.android.internal.R.styleable.PreferenceHeader_title); 785 if (tv != null && tv.type == TypedValue.TYPE_STRING) { 786 if (tv.resourceId != 0) { 787 header.titleRes = tv.resourceId; 788 } else { 789 header.title = tv.string; 790 } 791 } 792 tv = sa.peekValue( 793 com.android.internal.R.styleable.PreferenceHeader_summary); 794 if (tv != null && tv.type == TypedValue.TYPE_STRING) { 795 if (tv.resourceId != 0) { 796 header.summaryRes = tv.resourceId; 797 } else { 798 header.summary = tv.string; 799 } 800 } 801 tv = sa.peekValue( 802 com.android.internal.R.styleable.PreferenceHeader_breadCrumbTitle); 803 if (tv != null && tv.type == TypedValue.TYPE_STRING) { 804 if (tv.resourceId != 0) { 805 header.breadCrumbTitleRes = tv.resourceId; 806 } else { 807 header.breadCrumbTitle = tv.string; 808 } 809 } 810 tv = sa.peekValue( 811 com.android.internal.R.styleable.PreferenceHeader_breadCrumbShortTitle); 812 if (tv != null && tv.type == TypedValue.TYPE_STRING) { 813 if (tv.resourceId != 0) { 814 header.breadCrumbShortTitleRes = tv.resourceId; 815 } else { 816 header.breadCrumbShortTitle = tv.string; 817 } 818 } 819 header.iconRes = sa.getResourceId( 820 com.android.internal.R.styleable.PreferenceHeader_icon, 0); 821 header.fragment = sa.getString( 822 com.android.internal.R.styleable.PreferenceHeader_fragment); 823 sa.recycle(); 824 825 if (curBundle == null) { 826 curBundle = new Bundle(); 827 } 828 829 final int innerDepth = parser.getDepth(); 830 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 831 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) { 832 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 833 continue; 834 } 835 836 String innerNodeName = parser.getName(); 837 if (innerNodeName.equals("extra")) { 838 getResources().parseBundleExtra("extra", attrs, curBundle); 839 XmlUtils.skipCurrentTag(parser); 840 841 } else if (innerNodeName.equals("intent")) { 842 header.intent = Intent.parseIntent(getResources(), parser, attrs); 843 844 } else { 845 XmlUtils.skipCurrentTag(parser); 846 } 847 } 848 849 if (curBundle.size() > 0) { 850 header.fragmentArguments = curBundle; 851 curBundle = null; 852 } 853 854 target.add(header); 855 } else { 856 XmlUtils.skipCurrentTag(parser); 857 } 858 } 859 860 } catch (XmlPullParserException e) { 861 throw new RuntimeException("Error parsing headers", e); 862 } catch (IOException e) { 863 throw new RuntimeException("Error parsing headers", e); 864 } finally { 865 if (parser != null) parser.close(); 866 } 867 868 } 869 870 /** 871 * Set a footer that should be shown at the bottom of the header list. 872 */ 873 public void setListFooter(View view) { 874 mListFooter.removeAllViews(); 875 mListFooter.addView(view, new FrameLayout.LayoutParams( 876 FrameLayout.LayoutParams.MATCH_PARENT, 877 FrameLayout.LayoutParams.WRAP_CONTENT)); 878 } 879 880 @Override 881 protected void onStop() { 882 super.onStop(); 883 884 if (mPreferenceManager != null) { 885 mPreferenceManager.dispatchActivityStop(); 886 } 887 } 888 889 @Override 890 protected void onDestroy() { 891 super.onDestroy(); 892 893 if (mPreferenceManager != null) { 894 mPreferenceManager.dispatchActivityDestroy(); 895 } 896 } 897 898 @Override 899 protected void onSaveInstanceState(Bundle outState) { 900 super.onSaveInstanceState(outState); 901 902 if (mHeaders.size() > 0) { 903 outState.putParcelableArrayList(HEADERS_TAG, mHeaders); 904 if (mCurHeader != null) { 905 int index = mHeaders.indexOf(mCurHeader); 906 if (index >= 0) { 907 outState.putInt(CUR_HEADER_TAG, index); 908 } 909 } 910 } 911 912 if (mPreferenceManager != null) { 913 final PreferenceScreen preferenceScreen = getPreferenceScreen(); 914 if (preferenceScreen != null) { 915 Bundle container = new Bundle(); 916 preferenceScreen.saveHierarchyState(container); 917 outState.putBundle(PREFERENCES_TAG, container); 918 } 919 } 920 } 921 922 @Override 923 protected void onRestoreInstanceState(Bundle state) { 924 if (mPreferenceManager != null) { 925 Bundle container = state.getBundle(PREFERENCES_TAG); 926 if (container != null) { 927 final PreferenceScreen preferenceScreen = getPreferenceScreen(); 928 if (preferenceScreen != null) { 929 preferenceScreen.restoreHierarchyState(container); 930 mSavedInstanceState = state; 931 return; 932 } 933 } 934 } 935 936 // Only call this if we didn't save the instance state for later. 937 // If we did save it, it will be restored when we bind the adapter. 938 super.onRestoreInstanceState(state); 939 } 940 941 @Override 942 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 943 super.onActivityResult(requestCode, resultCode, data); 944 945 if (mPreferenceManager != null) { 946 mPreferenceManager.dispatchActivityResult(requestCode, resultCode, data); 947 } 948 } 949 950 @Override 951 public void onContentChanged() { 952 super.onContentChanged(); 953 954 if (mPreferenceManager != null) { 955 postBindPreferences(); 956 } 957 } 958 959 @Override 960 protected void onListItemClick(ListView l, View v, int position, long id) { 961 super.onListItemClick(l, v, position, id); 962 963 if (mAdapter != null) { 964 Object item = mAdapter.getItem(position); 965 if (item instanceof Header) onHeaderClick((Header) item, position); 966 } 967 } 968 969 /** 970 * Called when the user selects an item in the header list. The default 971 * implementation will call either 972 * {@link #startWithFragment(String, Bundle, Fragment, int, int, int)} 973 * or {@link #switchToHeader(Header)} as appropriate. 974 * 975 * @param header The header that was selected. 976 * @param position The header's position in the list. 977 */ 978 public void onHeaderClick(Header header, int position) { 979 if (header.fragment != null) { 980 if (mSinglePane) { 981 int titleRes = header.breadCrumbTitleRes; 982 int shortTitleRes = header.breadCrumbShortTitleRes; 983 if (titleRes == 0) { 984 titleRes = header.titleRes; 985 shortTitleRes = 0; 986 } 987 startWithFragment(header.fragment, header.fragmentArguments, null, 0, 988 titleRes, shortTitleRes); 989 } else { 990 switchToHeader(header); 991 } 992 } else if (header.intent != null) { 993 startActivity(header.intent); 994 } 995 } 996 997 /** 998 * Called by {@link #startWithFragment(String, Bundle, Fragment, int, int, int)} when 999 * in single-pane mode, to build an Intent to launch a new activity showing 1000 * the selected fragment. The default implementation constructs an Intent 1001 * that re-launches the current activity with the appropriate arguments to 1002 * display the fragment. 1003 * 1004 * @param fragmentName The name of the fragment to display. 1005 * @param args Optional arguments to supply to the fragment. 1006 * @param titleRes Optional resource ID of title to show for this item. 1007 * @param shortTitleRes Optional resource ID of short title to show for this item. 1008 * @return Returns an Intent that can be launched to display the given 1009 * fragment. 1010 */ 1011 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args, 1012 int titleRes, int shortTitleRes) { 1013 Intent intent = new Intent(Intent.ACTION_MAIN); 1014 intent.setClass(this, getClass()); 1015 intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentName); 1016 intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args); 1017 intent.putExtra(EXTRA_SHOW_FRAGMENT_TITLE, titleRes); 1018 intent.putExtra(EXTRA_SHOW_FRAGMENT_SHORT_TITLE, shortTitleRes); 1019 intent.putExtra(EXTRA_NO_HEADERS, true); 1020 return intent; 1021 } 1022 1023 /** 1024 * Like {@link #startWithFragment(String, Bundle, Fragment, int, int, int)} 1025 * but uses a 0 titleRes. 1026 */ 1027 public void startWithFragment(String fragmentName, Bundle args, 1028 Fragment resultTo, int resultRequestCode) { 1029 startWithFragment(fragmentName, args, resultTo, resultRequestCode, 0, 0); 1030 } 1031 1032 /** 1033 * Start a new instance of this activity, showing only the given 1034 * preference fragment. When launched in this mode, the header list 1035 * will be hidden and the given preference fragment will be instantiated 1036 * and fill the entire activity. 1037 * 1038 * @param fragmentName The name of the fragment to display. 1039 * @param args Optional arguments to supply to the fragment. 1040 * @param resultTo Option fragment that should receive the result of 1041 * the activity launch. 1042 * @param resultRequestCode If resultTo is non-null, this is the request 1043 * code in which to report the result. 1044 * @param titleRes Resource ID of string to display for the title of 1045 * this set of preferences. 1046 * @param shortTitleRes Resource ID of string to display for the short title of 1047 * this set of preferences. 1048 */ 1049 public void startWithFragment(String fragmentName, Bundle args, 1050 Fragment resultTo, int resultRequestCode, int titleRes, int shortTitleRes) { 1051 Intent intent = onBuildStartFragmentIntent(fragmentName, args, titleRes, shortTitleRes); 1052 if (resultTo == null) { 1053 startActivity(intent); 1054 } else { 1055 resultTo.startActivityForResult(intent, resultRequestCode); 1056 } 1057 } 1058 1059 /** 1060 * Change the base title of the bread crumbs for the current preferences. 1061 * This will normally be called for you. See 1062 * {@link android.app.FragmentBreadCrumbs} for more information. 1063 */ 1064 public void showBreadCrumbs(CharSequence title, CharSequence shortTitle) { 1065 if (mFragmentBreadCrumbs == null) { 1066 View crumbs = findViewById(android.R.id.title); 1067 // For screens with a different kind of title, don't create breadcrumbs. 1068 try { 1069 mFragmentBreadCrumbs = (FragmentBreadCrumbs)crumbs; 1070 } catch (ClassCastException e) { 1071 return; 1072 } 1073 if (mFragmentBreadCrumbs == null) { 1074 if (title != null) { 1075 setTitle(title); 1076 } 1077 return; 1078 } 1079 mFragmentBreadCrumbs.setMaxVisible(2); 1080 mFragmentBreadCrumbs.setActivity(this); 1081 } 1082 mFragmentBreadCrumbs.setTitle(title, shortTitle); 1083 mFragmentBreadCrumbs.setParentTitle(null, null, null); 1084 } 1085 1086 /** 1087 * Should be called after onCreate to ensure that the breadcrumbs, if any, were created. 1088 * This prepends a title to the fragment breadcrumbs and attaches a listener to any clicks 1089 * on the parent entry. 1090 * @param title the title for the breadcrumb 1091 * @param shortTitle the short title for the breadcrumb 1092 */ 1093 public void setParentTitle(CharSequence title, CharSequence shortTitle, 1094 OnClickListener listener) { 1095 if (mFragmentBreadCrumbs != null) { 1096 mFragmentBreadCrumbs.setParentTitle(title, shortTitle, listener); 1097 } 1098 } 1099 1100 void setSelectedHeader(Header header) { 1101 mCurHeader = header; 1102 int index = mHeaders.indexOf(header); 1103 if (index >= 0) { 1104 getListView().setItemChecked(index, true); 1105 } else { 1106 getListView().clearChoices(); 1107 } 1108 showBreadCrumbs(header); 1109 } 1110 1111 void showBreadCrumbs(Header header) { 1112 if (header != null) { 1113 CharSequence title = header.getBreadCrumbTitle(getResources()); 1114 if (title == null) title = header.getTitle(getResources()); 1115 if (title == null) title = getTitle(); 1116 showBreadCrumbs(title, header.getBreadCrumbShortTitle(getResources())); 1117 } else { 1118 showBreadCrumbs(getTitle(), null); 1119 } 1120 } 1121 1122 private void switchToHeaderInner(String fragmentName, Bundle args, int direction) { 1123 getFragmentManager().popBackStack(BACK_STACK_PREFS, 1124 FragmentManager.POP_BACK_STACK_INCLUSIVE); 1125 Fragment f = Fragment.instantiate(this, fragmentName, args); 1126 FragmentTransaction transaction = getFragmentManager().beginTransaction(); 1127 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 1128 transaction.replace(com.android.internal.R.id.prefs, f); 1129 transaction.commitAllowingStateLoss(); 1130 } 1131 1132 /** 1133 * When in two-pane mode, switch the fragment pane to show the given 1134 * preference fragment. 1135 * 1136 * @param fragmentName The name of the fragment to display. 1137 * @param args Optional arguments to supply to the fragment. 1138 */ 1139 public void switchToHeader(String fragmentName, Bundle args) { 1140 setSelectedHeader(null); 1141 switchToHeaderInner(fragmentName, args, 0); 1142 } 1143 1144 /** 1145 * When in two-pane mode, switch to the fragment pane to show the given 1146 * preference fragment. 1147 * 1148 * @param header The new header to display. 1149 */ 1150 public void switchToHeader(Header header) { 1151 if (mCurHeader == header) { 1152 // This is the header we are currently displaying. Just make sure 1153 // to pop the stack up to its root state. 1154 getFragmentManager().popBackStack(BACK_STACK_PREFS, 1155 FragmentManager.POP_BACK_STACK_INCLUSIVE); 1156 } else { 1157 int direction = mHeaders.indexOf(header) - mHeaders.indexOf(mCurHeader); 1158 switchToHeaderInner(header.fragment, header.fragmentArguments, direction); 1159 setSelectedHeader(header); 1160 } 1161 } 1162 1163 Header findBestMatchingHeader(Header cur, ArrayList<Header> from) { 1164 ArrayList<Header> matches = new ArrayList<Header>(); 1165 for (int j=0; j<from.size(); j++) { 1166 Header oh = from.get(j); 1167 if (cur == oh || (cur.id != HEADER_ID_UNDEFINED && cur.id == oh.id)) { 1168 // Must be this one. 1169 matches.clear(); 1170 matches.add(oh); 1171 break; 1172 } 1173 if (cur.fragment != null) { 1174 if (cur.fragment.equals(oh.fragment)) { 1175 matches.add(oh); 1176 } 1177 } else if (cur.intent != null) { 1178 if (cur.intent.equals(oh.intent)) { 1179 matches.add(oh); 1180 } 1181 } else if (cur.title != null) { 1182 if (cur.title.equals(oh.title)) { 1183 matches.add(oh); 1184 } 1185 } 1186 } 1187 final int NM = matches.size(); 1188 if (NM == 1) { 1189 return matches.get(0); 1190 } else if (NM > 1) { 1191 for (int j=0; j<NM; j++) { 1192 Header oh = matches.get(j); 1193 if (cur.fragmentArguments != null && 1194 cur.fragmentArguments.equals(oh.fragmentArguments)) { 1195 return oh; 1196 } 1197 if (cur.extras != null && cur.extras.equals(oh.extras)) { 1198 return oh; 1199 } 1200 if (cur.title != null && cur.title.equals(oh.title)) { 1201 return oh; 1202 } 1203 } 1204 } 1205 return null; 1206 } 1207 1208 /** 1209 * Start a new fragment. 1210 * 1211 * @param fragment The fragment to start 1212 * @param push If true, the current fragment will be pushed onto the back stack. If false, 1213 * the current fragment will be replaced. 1214 */ 1215 public void startPreferenceFragment(Fragment fragment, boolean push) { 1216 FragmentTransaction transaction = getFragmentManager().beginTransaction(); 1217 transaction.replace(com.android.internal.R.id.prefs, fragment); 1218 if (push) { 1219 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 1220 transaction.addToBackStack(BACK_STACK_PREFS); 1221 } else { 1222 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 1223 } 1224 transaction.commitAllowingStateLoss(); 1225 } 1226 1227 /** 1228 * Start a new fragment containing a preference panel. If the prefences 1229 * are being displayed in multi-pane mode, the given fragment class will 1230 * be instantiated and placed in the appropriate pane. If running in 1231 * single-pane mode, a new activity will be launched in which to show the 1232 * fragment. 1233 * 1234 * @param fragmentClass Full name of the class implementing the fragment. 1235 * @param args Any desired arguments to supply to the fragment. 1236 * @param titleRes Optional resource identifier of the title of this 1237 * fragment. 1238 * @param titleText Optional text of the title of this fragment. 1239 * @param resultTo Optional fragment that result data should be sent to. 1240 * If non-null, resultTo.onActivityResult() will be called when this 1241 * preference panel is done. The launched panel must use 1242 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done. 1243 * @param resultRequestCode If resultTo is non-null, this is the caller's 1244 * request code to be received with the resut. 1245 */ 1246 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes, 1247 CharSequence titleText, Fragment resultTo, int resultRequestCode) { 1248 if (mSinglePane) { 1249 startWithFragment(fragmentClass, args, resultTo, resultRequestCode, titleRes, 0); 1250 } else { 1251 Fragment f = Fragment.instantiate(this, fragmentClass, args); 1252 if (resultTo != null) { 1253 f.setTargetFragment(resultTo, resultRequestCode); 1254 } 1255 FragmentTransaction transaction = getFragmentManager().beginTransaction(); 1256 transaction.replace(com.android.internal.R.id.prefs, f); 1257 if (titleRes != 0) { 1258 transaction.setBreadCrumbTitle(titleRes); 1259 } else if (titleText != null) { 1260 transaction.setBreadCrumbTitle(titleText); 1261 } 1262 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 1263 transaction.addToBackStack(BACK_STACK_PREFS); 1264 transaction.commitAllowingStateLoss(); 1265 } 1266 } 1267 1268 /** 1269 * Called by a preference panel fragment to finish itself. 1270 * 1271 * @param caller The fragment that is asking to be finished. 1272 * @param resultCode Optional result code to send back to the original 1273 * launching fragment. 1274 * @param resultData Optional result data to send back to the original 1275 * launching fragment. 1276 */ 1277 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) { 1278 if (mSinglePane) { 1279 setResult(resultCode, resultData); 1280 finish(); 1281 } else { 1282 // XXX be smarter about popping the stack. 1283 onBackPressed(); 1284 if (caller != null) { 1285 if (caller.getTargetFragment() != null) { 1286 caller.getTargetFragment().onActivityResult(caller.getTargetRequestCode(), 1287 resultCode, resultData); 1288 } 1289 } 1290 } 1291 } 1292 1293 @Override 1294 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) { 1295 startPreferencePanel(pref.getFragment(), pref.getExtras(), pref.getTitleRes(), 1296 pref.getTitle(), null, 0); 1297 return true; 1298 } 1299 1300 /** 1301 * Posts a message to bind the preferences to the list view. 1302 * <p> 1303 * Binding late is preferred as any custom preference types created in 1304 * {@link #onCreate(Bundle)} are able to have their views recycled. 1305 */ 1306 private void postBindPreferences() { 1307 if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) return; 1308 mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget(); 1309 } 1310 1311 private void bindPreferences() { 1312 final PreferenceScreen preferenceScreen = getPreferenceScreen(); 1313 if (preferenceScreen != null) { 1314 preferenceScreen.bind(getListView()); 1315 if (mSavedInstanceState != null) { 1316 super.onRestoreInstanceState(mSavedInstanceState); 1317 mSavedInstanceState = null; 1318 } 1319 } 1320 } 1321 1322 /** 1323 * Returns the {@link PreferenceManager} used by this activity. 1324 * @return The {@link PreferenceManager}. 1325 * 1326 * @deprecated This function is not relevant for a modern fragment-based 1327 * PreferenceActivity. 1328 */ 1329 @Deprecated 1330 public PreferenceManager getPreferenceManager() { 1331 return mPreferenceManager; 1332 } 1333 1334 private void requirePreferenceManager() { 1335 if (mPreferenceManager == null) { 1336 if (mAdapter == null) { 1337 throw new RuntimeException("This should be called after super.onCreate."); 1338 } 1339 throw new RuntimeException( 1340 "Modern two-pane PreferenceActivity requires use of a PreferenceFragment"); 1341 } 1342 } 1343 1344 /** 1345 * Sets the root of the preference hierarchy that this activity is showing. 1346 * 1347 * @param preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy. 1348 * 1349 * @deprecated This function is not relevant for a modern fragment-based 1350 * PreferenceActivity. 1351 */ 1352 @Deprecated 1353 public void setPreferenceScreen(PreferenceScreen preferenceScreen) { 1354 requirePreferenceManager(); 1355 1356 if (mPreferenceManager.setPreferences(preferenceScreen) && preferenceScreen != null) { 1357 postBindPreferences(); 1358 CharSequence title = getPreferenceScreen().getTitle(); 1359 // Set the title of the activity 1360 if (title != null) { 1361 setTitle(title); 1362 } 1363 } 1364 } 1365 1366 /** 1367 * Gets the root of the preference hierarchy that this activity is showing. 1368 * 1369 * @return The {@link PreferenceScreen} that is the root of the preference 1370 * hierarchy. 1371 * 1372 * @deprecated This function is not relevant for a modern fragment-based 1373 * PreferenceActivity. 1374 */ 1375 @Deprecated 1376 public PreferenceScreen getPreferenceScreen() { 1377 if (mPreferenceManager != null) { 1378 return mPreferenceManager.getPreferenceScreen(); 1379 } 1380 return null; 1381 } 1382 1383 /** 1384 * Adds preferences from activities that match the given {@link Intent}. 1385 * 1386 * @param intent The {@link Intent} to query activities. 1387 * 1388 * @deprecated This function is not relevant for a modern fragment-based 1389 * PreferenceActivity. 1390 */ 1391 @Deprecated 1392 public void addPreferencesFromIntent(Intent intent) { 1393 requirePreferenceManager(); 1394 1395 setPreferenceScreen(mPreferenceManager.inflateFromIntent(intent, getPreferenceScreen())); 1396 } 1397 1398 /** 1399 * Inflates the given XML resource and adds the preference hierarchy to the current 1400 * preference hierarchy. 1401 * 1402 * @param preferencesResId The XML resource ID to inflate. 1403 * 1404 * @deprecated This function is not relevant for a modern fragment-based 1405 * PreferenceActivity. 1406 */ 1407 @Deprecated 1408 public void addPreferencesFromResource(int preferencesResId) { 1409 requirePreferenceManager(); 1410 1411 setPreferenceScreen(mPreferenceManager.inflateFromResource(this, preferencesResId, 1412 getPreferenceScreen())); 1413 } 1414 1415 /** 1416 * {@inheritDoc} 1417 * 1418 * @deprecated This function is not relevant for a modern fragment-based 1419 * PreferenceActivity. 1420 */ 1421 @Deprecated 1422 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 1423 return false; 1424 } 1425 1426 /** 1427 * Finds a {@link Preference} based on its key. 1428 * 1429 * @param key The key of the preference to retrieve. 1430 * @return The {@link Preference} with the key, or null. 1431 * @see PreferenceGroup#findPreference(CharSequence) 1432 * 1433 * @deprecated This function is not relevant for a modern fragment-based 1434 * PreferenceActivity. 1435 */ 1436 @Deprecated 1437 public Preference findPreference(CharSequence key) { 1438 1439 if (mPreferenceManager == null) { 1440 return null; 1441 } 1442 1443 return mPreferenceManager.findPreference(key); 1444 } 1445 1446 @Override 1447 protected void onNewIntent(Intent intent) { 1448 if (mPreferenceManager != null) { 1449 mPreferenceManager.dispatchNewIntent(intent); 1450 } 1451 } 1452 1453 // give subclasses access to the Next button 1454 /** @hide */ 1455 protected boolean hasNextButton() { 1456 return mNextButton != null; 1457 } 1458 /** @hide */ 1459 protected Button getNextButton() { 1460 return mNextButton; 1461 } 1462} 1463