PreferenceActivity.java revision 39725ac96b68f7d2d25d10662bd9ad3189fdab77
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 } else if (mHeaders.size() > 0) { 567 setListAdapter(new HeaderAdapter(this, mHeaders)); 568 if (!mSinglePane) { 569 // Multi-pane. 570 getListView().setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); 571 if (mCurHeader != null) { 572 setSelectedHeader(mCurHeader); 573 } 574 mPrefsContainer.setVisibility(View.VISIBLE); 575 } 576 } else { 577 // If there are no headers, we are in the old "just show a screen 578 // of preferences" mode. 579 setContentView(com.android.internal.R.layout.preference_list_content_single); 580 mListFooter = (FrameLayout) findViewById(com.android.internal.R.id.list_footer); 581 mPrefsContainer = (ViewGroup) findViewById(com.android.internal.R.id.prefs); 582 mPreferenceManager = new PreferenceManager(this, FIRST_REQUEST_CODE); 583 mPreferenceManager.setOnPreferenceTreeClickListener(this); 584 } 585 586 getListView().setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 587 588 // see if we should show Back/Next buttons 589 Intent intent = getIntent(); 590 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) { 591 592 findViewById(com.android.internal.R.id.button_bar).setVisibility(View.VISIBLE); 593 594 Button backButton = (Button)findViewById(com.android.internal.R.id.back_button); 595 backButton.setOnClickListener(new OnClickListener() { 596 public void onClick(View v) { 597 setResult(RESULT_CANCELED); 598 finish(); 599 } 600 }); 601 Button skipButton = (Button)findViewById(com.android.internal.R.id.skip_button); 602 skipButton.setOnClickListener(new OnClickListener() { 603 public void onClick(View v) { 604 setResult(RESULT_OK); 605 finish(); 606 } 607 }); 608 mNextButton = (Button)findViewById(com.android.internal.R.id.next_button); 609 mNextButton.setOnClickListener(new OnClickListener() { 610 public void onClick(View v) { 611 setResult(RESULT_OK); 612 finish(); 613 } 614 }); 615 616 // set our various button parameters 617 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) { 618 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT); 619 if (TextUtils.isEmpty(buttonText)) { 620 mNextButton.setVisibility(View.GONE); 621 } 622 else { 623 mNextButton.setText(buttonText); 624 } 625 } 626 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) { 627 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT); 628 if (TextUtils.isEmpty(buttonText)) { 629 backButton.setVisibility(View.GONE); 630 } 631 else { 632 backButton.setText(buttonText); 633 } 634 } 635 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) { 636 skipButton.setVisibility(View.VISIBLE); 637 } 638 } 639 } 640 641 /** 642 * Returns true if this activity is currently showing the header list. 643 */ 644 public boolean hasHeaders() { 645 return getListView().getVisibility() == View.VISIBLE 646 && mPreferenceManager == null; 647 } 648 649 /** 650 * Returns true if this activity is showing multiple panes -- the headers 651 * and a preference fragment. 652 */ 653 public boolean isMultiPane() { 654 return hasHeaders() && mPrefsContainer.getVisibility() == View.VISIBLE; 655 } 656 657 /** 658 * Called to determine if the activity should run in multi-pane mode. 659 * The default implementation returns true if the screen is large 660 * enough. 661 */ 662 public boolean onIsMultiPane() { 663 boolean preferMultiPane = getResources().getBoolean( 664 com.android.internal.R.bool.preferences_prefer_dual_pane); 665 return preferMultiPane; 666 } 667 668 /** 669 * Called to determine whether the header list should be hidden. 670 * The default implementation returns the 671 * value given in {@link #EXTRA_NO_HEADERS} or false if it is not supplied. 672 * This is set to false, for example, when the activity is being re-launched 673 * to show a particular preference activity. 674 */ 675 public boolean onIsHidingHeaders() { 676 return getIntent().getBooleanExtra(EXTRA_NO_HEADERS, false); 677 } 678 679 /** 680 * Called to determine the initial header to be shown. The default 681 * implementation simply returns the fragment of the first header. Note 682 * that the returned Header object does not actually need to exist in 683 * your header list -- whatever its fragment is will simply be used to 684 * show for the initial UI. 685 */ 686 public Header onGetInitialHeader() { 687 return mHeaders.get(0); 688 } 689 690 /** 691 * Called after the header list has been updated ({@link #onBuildHeaders} 692 * has been called and returned due to {@link #invalidateHeaders()}) to 693 * specify the header that should now be selected. The default implementation 694 * returns null to keep whatever header is currently selected. 695 */ 696 public Header onGetNewHeader() { 697 return null; 698 } 699 700 /** 701 * Called when the activity needs its list of headers build. By 702 * implementing this and adding at least one item to the list, you 703 * will cause the activity to run in its modern fragment mode. Note 704 * that this function may not always be called; for example, if the 705 * activity has been asked to display a particular fragment without 706 * the header list, there is no need to build the headers. 707 * 708 * <p>Typical implementations will use {@link #loadHeadersFromResource} 709 * to fill in the list from a resource. 710 * 711 * @param target The list in which to place the headers. 712 */ 713 public void onBuildHeaders(List<Header> target) { 714 // Should be overloaded by subclasses 715 } 716 717 /** 718 * Call when you need to change the headers being displayed. Will result 719 * in onBuildHeaders() later being called to retrieve the new list. 720 */ 721 public void invalidateHeaders() { 722 if (!mHandler.hasMessages(MSG_BUILD_HEADERS)) { 723 mHandler.sendEmptyMessage(MSG_BUILD_HEADERS); 724 } 725 } 726 727 /** 728 * Parse the given XML file as a header description, adding each 729 * parsed Header into the target list. 730 * 731 * @param resid The XML resource to load and parse. 732 * @param target The list in which the parsed headers should be placed. 733 */ 734 public void loadHeadersFromResource(int resid, List<Header> target) { 735 XmlResourceParser parser = null; 736 try { 737 parser = getResources().getXml(resid); 738 AttributeSet attrs = Xml.asAttributeSet(parser); 739 740 int type; 741 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 742 && type != XmlPullParser.START_TAG) { 743 // Parse next until start tag is found 744 } 745 746 String nodeName = parser.getName(); 747 if (!"preference-headers".equals(nodeName)) { 748 throw new RuntimeException( 749 "XML document must start with <preference-headers> tag; found" 750 + nodeName + " at " + parser.getPositionDescription()); 751 } 752 753 Bundle curBundle = null; 754 755 final int outerDepth = parser.getDepth(); 756 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 757 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 758 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 759 continue; 760 } 761 762 nodeName = parser.getName(); 763 if ("header".equals(nodeName)) { 764 Header header = new Header(); 765 766 TypedArray sa = getResources().obtainAttributes(attrs, 767 com.android.internal.R.styleable.PreferenceHeader); 768 header.id = sa.getResourceId( 769 com.android.internal.R.styleable.PreferenceHeader_id, 770 (int)HEADER_ID_UNDEFINED); 771 TypedValue tv = sa.peekValue( 772 com.android.internal.R.styleable.PreferenceHeader_title); 773 if (tv != null && tv.type == TypedValue.TYPE_STRING) { 774 if (tv.resourceId != 0) { 775 header.titleRes = tv.resourceId; 776 } else { 777 header.title = tv.string; 778 } 779 } 780 tv = sa.peekValue( 781 com.android.internal.R.styleable.PreferenceHeader_summary); 782 if (tv != null && tv.type == TypedValue.TYPE_STRING) { 783 if (tv.resourceId != 0) { 784 header.summaryRes = tv.resourceId; 785 } else { 786 header.summary = tv.string; 787 } 788 } 789 tv = sa.peekValue( 790 com.android.internal.R.styleable.PreferenceHeader_breadCrumbTitle); 791 if (tv != null && tv.type == TypedValue.TYPE_STRING) { 792 if (tv.resourceId != 0) { 793 header.breadCrumbTitleRes = tv.resourceId; 794 } else { 795 header.breadCrumbTitle = tv.string; 796 } 797 } 798 tv = sa.peekValue( 799 com.android.internal.R.styleable.PreferenceHeader_breadCrumbShortTitle); 800 if (tv != null && tv.type == TypedValue.TYPE_STRING) { 801 if (tv.resourceId != 0) { 802 header.breadCrumbShortTitleRes = tv.resourceId; 803 } else { 804 header.breadCrumbShortTitle = tv.string; 805 } 806 } 807 header.iconRes = sa.getResourceId( 808 com.android.internal.R.styleable.PreferenceHeader_icon, 0); 809 header.fragment = sa.getString( 810 com.android.internal.R.styleable.PreferenceHeader_fragment); 811 sa.recycle(); 812 813 if (curBundle == null) { 814 curBundle = new Bundle(); 815 } 816 817 final int innerDepth = parser.getDepth(); 818 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT 819 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) { 820 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 821 continue; 822 } 823 824 String innerNodeName = parser.getName(); 825 if (innerNodeName.equals("extra")) { 826 getResources().parseBundleExtra("extra", attrs, curBundle); 827 XmlUtils.skipCurrentTag(parser); 828 829 } else if (innerNodeName.equals("intent")) { 830 header.intent = Intent.parseIntent(getResources(), parser, attrs); 831 832 } else { 833 XmlUtils.skipCurrentTag(parser); 834 } 835 } 836 837 if (curBundle.size() > 0) { 838 header.fragmentArguments = curBundle; 839 curBundle = null; 840 } 841 842 target.add(header); 843 } else { 844 XmlUtils.skipCurrentTag(parser); 845 } 846 } 847 848 } catch (XmlPullParserException e) { 849 throw new RuntimeException("Error parsing headers", e); 850 } catch (IOException e) { 851 throw new RuntimeException("Error parsing headers", e); 852 } finally { 853 if (parser != null) parser.close(); 854 } 855 856 } 857 858 /** 859 * Set a footer that should be shown at the bottom of the header list. 860 */ 861 public void setListFooter(View view) { 862 mListFooter.removeAllViews(); 863 mListFooter.addView(view, new FrameLayout.LayoutParams( 864 FrameLayout.LayoutParams.MATCH_PARENT, 865 FrameLayout.LayoutParams.WRAP_CONTENT)); 866 } 867 868 @Override 869 protected void onStop() { 870 super.onStop(); 871 872 if (mPreferenceManager != null) { 873 mPreferenceManager.dispatchActivityStop(); 874 } 875 } 876 877 @Override 878 protected void onDestroy() { 879 super.onDestroy(); 880 881 if (mPreferenceManager != null) { 882 mPreferenceManager.dispatchActivityDestroy(); 883 } 884 } 885 886 @Override 887 protected void onSaveInstanceState(Bundle outState) { 888 super.onSaveInstanceState(outState); 889 890 if (mHeaders.size() > 0) { 891 outState.putParcelableArrayList(HEADERS_TAG, mHeaders); 892 if (mCurHeader != null) { 893 int index = mHeaders.indexOf(mCurHeader); 894 if (index >= 0) { 895 outState.putInt(CUR_HEADER_TAG, index); 896 } 897 } 898 } 899 900 if (mPreferenceManager != null) { 901 final PreferenceScreen preferenceScreen = getPreferenceScreen(); 902 if (preferenceScreen != null) { 903 Bundle container = new Bundle(); 904 preferenceScreen.saveHierarchyState(container); 905 outState.putBundle(PREFERENCES_TAG, container); 906 } 907 } 908 } 909 910 @Override 911 protected void onRestoreInstanceState(Bundle state) { 912 if (mPreferenceManager != null) { 913 Bundle container = state.getBundle(PREFERENCES_TAG); 914 if (container != null) { 915 final PreferenceScreen preferenceScreen = getPreferenceScreen(); 916 if (preferenceScreen != null) { 917 preferenceScreen.restoreHierarchyState(container); 918 mSavedInstanceState = state; 919 return; 920 } 921 } 922 } 923 924 // Only call this if we didn't save the instance state for later. 925 // If we did save it, it will be restored when we bind the adapter. 926 super.onRestoreInstanceState(state); 927 } 928 929 @Override 930 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 931 super.onActivityResult(requestCode, resultCode, data); 932 933 if (mPreferenceManager != null) { 934 mPreferenceManager.dispatchActivityResult(requestCode, resultCode, data); 935 } 936 } 937 938 @Override 939 public void onContentChanged() { 940 super.onContentChanged(); 941 942 if (mPreferenceManager != null) { 943 postBindPreferences(); 944 } 945 } 946 947 @Override 948 protected void onListItemClick(ListView l, View v, int position, long id) { 949 super.onListItemClick(l, v, position, id); 950 951 if (mAdapter != null) { 952 Object item = mAdapter.getItem(position); 953 if (item instanceof Header) onHeaderClick((Header) item, position); 954 } 955 } 956 957 /** 958 * Called when the user selects an item in the header list. The default 959 * implementation will call either 960 * {@link #startWithFragment(String, Bundle, Fragment, int, int, int)} 961 * or {@link #switchToHeader(Header)} as appropriate. 962 * 963 * @param header The header that was selected. 964 * @param position The header's position in the list. 965 */ 966 public void onHeaderClick(Header header, int position) { 967 if (header.fragment != null) { 968 if (mSinglePane) { 969 int titleRes = header.breadCrumbTitleRes; 970 int shortTitleRes = header.breadCrumbShortTitleRes; 971 if (titleRes == 0) { 972 titleRes = header.titleRes; 973 shortTitleRes = 0; 974 } 975 startWithFragment(header.fragment, header.fragmentArguments, null, 0, 976 titleRes, shortTitleRes); 977 } else { 978 switchToHeader(header); 979 } 980 } else if (header.intent != null) { 981 startActivity(header.intent); 982 } 983 } 984 985 /** 986 * Called by {@link #startWithFragment(String, Bundle, Fragment, int, int, int)} when 987 * in single-pane mode, to build an Intent to launch a new activity showing 988 * the selected fragment. The default implementation constructs an Intent 989 * that re-launches the current activity with the appropriate arguments to 990 * display the fragment. 991 * 992 * @param fragmentName The name of the fragment to display. 993 * @param args Optional arguments to supply to the fragment. 994 * @param titleRes Optional resource ID of title to show for this item. 995 * @param shortTitleRes Optional resource ID of short title to show for this item. 996 * @return Returns an Intent that can be launched to display the given 997 * fragment. 998 */ 999 public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args, 1000 int titleRes, int shortTitleRes) { 1001 Intent intent = new Intent(Intent.ACTION_MAIN); 1002 intent.setClass(this, getClass()); 1003 intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentName); 1004 intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args); 1005 intent.putExtra(EXTRA_SHOW_FRAGMENT_TITLE, titleRes); 1006 intent.putExtra(EXTRA_SHOW_FRAGMENT_SHORT_TITLE, shortTitleRes); 1007 intent.putExtra(EXTRA_NO_HEADERS, true); 1008 return intent; 1009 } 1010 1011 /** 1012 * Like {@link #startWithFragment(String, Bundle, Fragment, int, int, int)} 1013 * but uses a 0 titleRes. 1014 */ 1015 public void startWithFragment(String fragmentName, Bundle args, 1016 Fragment resultTo, int resultRequestCode) { 1017 startWithFragment(fragmentName, args, resultTo, resultRequestCode, 0, 0); 1018 } 1019 1020 /** 1021 * Start a new instance of this activity, showing only the given 1022 * preference fragment. When launched in this mode, the header list 1023 * will be hidden and the given preference fragment will be instantiated 1024 * and fill the entire activity. 1025 * 1026 * @param fragmentName The name of the fragment to display. 1027 * @param args Optional arguments to supply to the fragment. 1028 * @param resultTo Option fragment that should receive the result of 1029 * the activity launch. 1030 * @param resultRequestCode If resultTo is non-null, this is the request 1031 * code in which to report the result. 1032 * @param titleRes Resource ID of string to display for the title of 1033 * this set of preferences. 1034 * @param shortTitleRes Resource ID of string to display for the short title of 1035 * this set of preferences. 1036 */ 1037 public void startWithFragment(String fragmentName, Bundle args, 1038 Fragment resultTo, int resultRequestCode, int titleRes, int shortTitleRes) { 1039 Intent intent = onBuildStartFragmentIntent(fragmentName, args, titleRes, shortTitleRes); 1040 if (resultTo == null) { 1041 startActivity(intent); 1042 } else { 1043 resultTo.startActivityForResult(intent, resultRequestCode); 1044 } 1045 } 1046 1047 /** 1048 * Change the base title of the bread crumbs for the current preferences. 1049 * This will normally be called for you. See 1050 * {@link android.app.FragmentBreadCrumbs} for more information. 1051 */ 1052 public void showBreadCrumbs(CharSequence title, CharSequence shortTitle) { 1053 if (mFragmentBreadCrumbs == null) { 1054 View crumbs = findViewById(android.R.id.title); 1055 // For screens with a different kind of title, don't create breadcrumbs. 1056 try { 1057 mFragmentBreadCrumbs = (FragmentBreadCrumbs)crumbs; 1058 } catch (ClassCastException e) { 1059 return; 1060 } 1061 if (mFragmentBreadCrumbs == null) { 1062 if (title != null) { 1063 setTitle(title); 1064 } 1065 return; 1066 } 1067 mFragmentBreadCrumbs.setMaxVisible(2); 1068 mFragmentBreadCrumbs.setActivity(this); 1069 } 1070 mFragmentBreadCrumbs.setTitle(title, shortTitle); 1071 mFragmentBreadCrumbs.setParentTitle(null, null, null); 1072 } 1073 1074 /** 1075 * Should be called after onCreate to ensure that the breadcrumbs, if any, were created. 1076 * This prepends a title to the fragment breadcrumbs and attaches a listener to any clicks 1077 * on the parent entry. 1078 * @param title the title for the breadcrumb 1079 * @param shortTitle the short title for the breadcrumb 1080 */ 1081 public void setParentTitle(CharSequence title, CharSequence shortTitle, 1082 OnClickListener listener) { 1083 if (mFragmentBreadCrumbs != null) { 1084 mFragmentBreadCrumbs.setParentTitle(title, shortTitle, listener); 1085 } 1086 } 1087 1088 void setSelectedHeader(Header header) { 1089 mCurHeader = header; 1090 int index = mHeaders.indexOf(header); 1091 if (index >= 0) { 1092 getListView().setItemChecked(index, true); 1093 } else { 1094 getListView().clearChoices(); 1095 } 1096 if (header != null) { 1097 CharSequence title = header.getBreadCrumbTitle(getResources()); 1098 if (title == null) title = header.getTitle(getResources()); 1099 if (title == null) title = getTitle(); 1100 showBreadCrumbs(title, header.getBreadCrumbShortTitle(getResources())); 1101 } else { 1102 showBreadCrumbs(getTitle(), null); 1103 } 1104 } 1105 1106 private void switchToHeaderInner(String fragmentName, Bundle args, int direction) { 1107 getFragmentManager().popBackStack(BACK_STACK_PREFS, 1108 FragmentManager.POP_BACK_STACK_INCLUSIVE); 1109 Fragment f = Fragment.instantiate(this, fragmentName, args); 1110 FragmentTransaction transaction = getFragmentManager().beginTransaction(); 1111 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 1112 transaction.replace(com.android.internal.R.id.prefs, f); 1113 transaction.commitAllowingStateLoss(); 1114 } 1115 1116 /** 1117 * When in two-pane mode, switch the fragment pane to show the given 1118 * preference fragment. 1119 * 1120 * @param fragmentName The name of the fragment to display. 1121 * @param args Optional arguments to supply to the fragment. 1122 */ 1123 public void switchToHeader(String fragmentName, Bundle args) { 1124 setSelectedHeader(null); 1125 switchToHeaderInner(fragmentName, args, 0); 1126 } 1127 1128 /** 1129 * When in two-pane mode, switch to the fragment pane to show the given 1130 * preference fragment. 1131 * 1132 * @param header The new header to display. 1133 */ 1134 public void switchToHeader(Header header) { 1135 if (mCurHeader == header) { 1136 // This is the header we are currently displaying. Just make sure 1137 // to pop the stack up to its root state. 1138 getFragmentManager().popBackStack(BACK_STACK_PREFS, 1139 FragmentManager.POP_BACK_STACK_INCLUSIVE); 1140 } else { 1141 int direction = mHeaders.indexOf(header) - mHeaders.indexOf(mCurHeader); 1142 switchToHeaderInner(header.fragment, header.fragmentArguments, direction); 1143 setSelectedHeader(header); 1144 } 1145 } 1146 1147 Header findBestMatchingHeader(Header cur, ArrayList<Header> from) { 1148 ArrayList<Header> matches = new ArrayList<Header>(); 1149 for (int j=0; j<from.size(); j++) { 1150 Header oh = from.get(j); 1151 if (cur == oh || (cur.id != HEADER_ID_UNDEFINED && cur.id == oh.id)) { 1152 // Must be this one. 1153 matches.clear(); 1154 matches.add(oh); 1155 break; 1156 } 1157 if (cur.fragment != null) { 1158 if (cur.fragment.equals(oh.fragment)) { 1159 matches.add(oh); 1160 } 1161 } else if (cur.intent != null) { 1162 if (cur.intent.equals(oh.intent)) { 1163 matches.add(oh); 1164 } 1165 } else if (cur.title != null) { 1166 if (cur.title.equals(oh.title)) { 1167 matches.add(oh); 1168 } 1169 } 1170 } 1171 final int NM = matches.size(); 1172 if (NM == 1) { 1173 return matches.get(0); 1174 } else if (NM > 1) { 1175 for (int j=0; j<NM; j++) { 1176 Header oh = matches.get(j); 1177 if (cur.fragmentArguments != null && 1178 cur.fragmentArguments.equals(oh.fragmentArguments)) { 1179 return oh; 1180 } 1181 if (cur.extras != null && cur.extras.equals(oh.extras)) { 1182 return oh; 1183 } 1184 if (cur.title != null && cur.title.equals(oh.title)) { 1185 return oh; 1186 } 1187 } 1188 } 1189 return null; 1190 } 1191 1192 /** 1193 * Start a new fragment. 1194 * 1195 * @param fragment The fragment to start 1196 * @param push If true, the current fragment will be pushed onto the back stack. If false, 1197 * the current fragment will be replaced. 1198 */ 1199 public void startPreferenceFragment(Fragment fragment, boolean push) { 1200 FragmentTransaction transaction = getFragmentManager().beginTransaction(); 1201 transaction.replace(com.android.internal.R.id.prefs, fragment); 1202 if (push) { 1203 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 1204 transaction.addToBackStack(BACK_STACK_PREFS); 1205 } else { 1206 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 1207 } 1208 transaction.commitAllowingStateLoss(); 1209 } 1210 1211 /** 1212 * Start a new fragment containing a preference panel. If the prefences 1213 * are being displayed in multi-pane mode, the given fragment class will 1214 * be instantiated and placed in the appropriate pane. If running in 1215 * single-pane mode, a new activity will be launched in which to show the 1216 * fragment. 1217 * 1218 * @param fragmentClass Full name of the class implementing the fragment. 1219 * @param args Any desired arguments to supply to the fragment. 1220 * @param titleRes Optional resource identifier of the title of this 1221 * fragment. 1222 * @param titleText Optional text of the title of this fragment. 1223 * @param resultTo Optional fragment that result data should be sent to. 1224 * If non-null, resultTo.onActivityResult() will be called when this 1225 * preference panel is done. The launched panel must use 1226 * {@link #finishPreferencePanel(Fragment, int, Intent)} when done. 1227 * @param resultRequestCode If resultTo is non-null, this is the caller's 1228 * request code to be received with the resut. 1229 */ 1230 public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes, 1231 CharSequence titleText, Fragment resultTo, int resultRequestCode) { 1232 if (mSinglePane) { 1233 startWithFragment(fragmentClass, args, resultTo, resultRequestCode, titleRes, 0); 1234 } else { 1235 Fragment f = Fragment.instantiate(this, fragmentClass, args); 1236 if (resultTo != null) { 1237 f.setTargetFragment(resultTo, resultRequestCode); 1238 } 1239 FragmentTransaction transaction = getFragmentManager().beginTransaction(); 1240 transaction.replace(com.android.internal.R.id.prefs, f); 1241 if (titleRes != 0) { 1242 transaction.setBreadCrumbTitle(titleRes); 1243 } else if (titleText != null) { 1244 transaction.setBreadCrumbTitle(titleText); 1245 } 1246 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 1247 transaction.addToBackStack(BACK_STACK_PREFS); 1248 transaction.commitAllowingStateLoss(); 1249 } 1250 } 1251 1252 /** 1253 * Called by a preference panel fragment to finish itself. 1254 * 1255 * @param caller The fragment that is asking to be finished. 1256 * @param resultCode Optional result code to send back to the original 1257 * launching fragment. 1258 * @param resultData Optional result data to send back to the original 1259 * launching fragment. 1260 */ 1261 public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) { 1262 if (mSinglePane) { 1263 setResult(resultCode, resultData); 1264 finish(); 1265 } else { 1266 // XXX be smarter about popping the stack. 1267 onBackPressed(); 1268 if (caller != null) { 1269 if (caller.getTargetFragment() != null) { 1270 caller.getTargetFragment().onActivityResult(caller.getTargetRequestCode(), 1271 resultCode, resultData); 1272 } 1273 } 1274 } 1275 } 1276 1277 @Override 1278 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) { 1279 startPreferencePanel(pref.getFragment(), pref.getExtras(), pref.getTitleRes(), 1280 pref.getTitle(), null, 0); 1281 return true; 1282 } 1283 1284 /** 1285 * Posts a message to bind the preferences to the list view. 1286 * <p> 1287 * Binding late is preferred as any custom preference types created in 1288 * {@link #onCreate(Bundle)} are able to have their views recycled. 1289 */ 1290 private void postBindPreferences() { 1291 if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) return; 1292 mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget(); 1293 } 1294 1295 private void bindPreferences() { 1296 final PreferenceScreen preferenceScreen = getPreferenceScreen(); 1297 if (preferenceScreen != null) { 1298 preferenceScreen.bind(getListView()); 1299 if (mSavedInstanceState != null) { 1300 super.onRestoreInstanceState(mSavedInstanceState); 1301 mSavedInstanceState = null; 1302 } 1303 } 1304 } 1305 1306 /** 1307 * Returns the {@link PreferenceManager} used by this activity. 1308 * @return The {@link PreferenceManager}. 1309 * 1310 * @deprecated This function is not relevant for a modern fragment-based 1311 * PreferenceActivity. 1312 */ 1313 @Deprecated 1314 public PreferenceManager getPreferenceManager() { 1315 return mPreferenceManager; 1316 } 1317 1318 private void requirePreferenceManager() { 1319 if (mPreferenceManager == null) { 1320 if (mAdapter == null) { 1321 throw new RuntimeException("This should be called after super.onCreate."); 1322 } 1323 throw new RuntimeException( 1324 "Modern two-pane PreferenceActivity requires use of a PreferenceFragment"); 1325 } 1326 } 1327 1328 /** 1329 * Sets the root of the preference hierarchy that this activity is showing. 1330 * 1331 * @param preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy. 1332 * 1333 * @deprecated This function is not relevant for a modern fragment-based 1334 * PreferenceActivity. 1335 */ 1336 @Deprecated 1337 public void setPreferenceScreen(PreferenceScreen preferenceScreen) { 1338 requirePreferenceManager(); 1339 1340 if (mPreferenceManager.setPreferences(preferenceScreen) && preferenceScreen != null) { 1341 postBindPreferences(); 1342 CharSequence title = getPreferenceScreen().getTitle(); 1343 // Set the title of the activity 1344 if (title != null) { 1345 setTitle(title); 1346 } 1347 } 1348 } 1349 1350 /** 1351 * Gets the root of the preference hierarchy that this activity is showing. 1352 * 1353 * @return The {@link PreferenceScreen} that is the root of the preference 1354 * hierarchy. 1355 * 1356 * @deprecated This function is not relevant for a modern fragment-based 1357 * PreferenceActivity. 1358 */ 1359 @Deprecated 1360 public PreferenceScreen getPreferenceScreen() { 1361 if (mPreferenceManager != null) { 1362 return mPreferenceManager.getPreferenceScreen(); 1363 } 1364 return null; 1365 } 1366 1367 /** 1368 * Adds preferences from activities that match the given {@link Intent}. 1369 * 1370 * @param intent The {@link Intent} to query activities. 1371 * 1372 * @deprecated This function is not relevant for a modern fragment-based 1373 * PreferenceActivity. 1374 */ 1375 @Deprecated 1376 public void addPreferencesFromIntent(Intent intent) { 1377 requirePreferenceManager(); 1378 1379 setPreferenceScreen(mPreferenceManager.inflateFromIntent(intent, getPreferenceScreen())); 1380 } 1381 1382 /** 1383 * Inflates the given XML resource and adds the preference hierarchy to the current 1384 * preference hierarchy. 1385 * 1386 * @param preferencesResId The XML resource ID to inflate. 1387 * 1388 * @deprecated This function is not relevant for a modern fragment-based 1389 * PreferenceActivity. 1390 */ 1391 @Deprecated 1392 public void addPreferencesFromResource(int preferencesResId) { 1393 requirePreferenceManager(); 1394 1395 setPreferenceScreen(mPreferenceManager.inflateFromResource(this, preferencesResId, 1396 getPreferenceScreen())); 1397 } 1398 1399 /** 1400 * {@inheritDoc} 1401 * 1402 * @deprecated This function is not relevant for a modern fragment-based 1403 * PreferenceActivity. 1404 */ 1405 @Deprecated 1406 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 1407 return false; 1408 } 1409 1410 /** 1411 * Finds a {@link Preference} based on its key. 1412 * 1413 * @param key The key of the preference to retrieve. 1414 * @return The {@link Preference} with the key, or null. 1415 * @see PreferenceGroup#findPreference(CharSequence) 1416 * 1417 * @deprecated This function is not relevant for a modern fragment-based 1418 * PreferenceActivity. 1419 */ 1420 @Deprecated 1421 public Preference findPreference(CharSequence key) { 1422 1423 if (mPreferenceManager == null) { 1424 return null; 1425 } 1426 1427 return mPreferenceManager.findPreference(key); 1428 } 1429 1430 @Override 1431 protected void onNewIntent(Intent intent) { 1432 if (mPreferenceManager != null) { 1433 mPreferenceManager.dispatchNewIntent(intent); 1434 } 1435 } 1436 1437 // give subclasses access to the Next button 1438 /** @hide */ 1439 protected boolean hasNextButton() { 1440 return mNextButton != null; 1441 } 1442 /** @hide */ 1443 protected Button getNextButton() { 1444 return mNextButton; 1445 } 1446} 1447