| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.preference; |
| 18 |
|
| 19 |
import android.animation.LayoutTransition; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.StringRes; |
| 22 |
import android.annotation.XmlRes; |
| 23 |
import android.app.Fragment; |
| 24 |
import android.app.FragmentBreadCrumbs; |
| 25 |
import android.app.FragmentManager; |
| 26 |
import android.app.FragmentTransaction; |
| 27 |
import android.app.ListActivity; |
| 28 |
import android.compat.annotation.UnsupportedAppUsage; |
| 29 |
import android.content.Context; |
| 30 |
import android.content.Intent; |
| 31 |
import android.content.res.Resources; |
| 32 |
import android.content.res.TypedArray; |
| 33 |
import android.content.res.XmlResourceParser; |
| 34 |
import android.os.Build; |
| 35 |
import android.os.Bundle; |
| 36 |
import android.os.Handler; |
| 37 |
import android.os.Message; |
| 38 |
import android.os.Parcel; |
| 39 |
import android.os.Parcelable; |
| 40 |
import android.text.TextUtils; |
| 41 |
import android.util.AttributeSet; |
| 42 |
import android.util.TypedValue; |
| 43 |
import android.util.Xml; |
| 44 |
import android.view.LayoutInflater; |
| 45 |
import android.view.MenuItem; |
| 46 |
import android.view.View; |
| 47 |
import android.view.View.OnClickListener; |
| 48 |
import android.view.ViewGroup; |
| 49 |
import android.widget.AbsListView; |
| 50 |
import android.widget.ArrayAdapter; |
| 51 |
import android.widget.BaseAdapter; |
| 52 |
import android.widget.Button; |
| 53 |
import android.widget.FrameLayout; |
| 54 |
import android.widget.ImageView; |
| 55 |
import android.widget.ListView; |
| 56 |
import android.widget.TextView; |
| 57 |
|
| 58 |
import com.android.internal.util.XmlUtils; |
| 59 |
|
| 60 |
import org.xmlpull.v1.XmlPullParser; |
| 61 |
import org.xmlpull.v1.XmlPullParserException; |
| 62 |
|
| 63 |
import java.io.IOException; |
| 64 |
import java.util.ArrayList; |
| 65 |
import java.util.List; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
@Deprecated |
| 108 |
public abstract class PreferenceActivity extends ListActivity implements |
| 109 |
PreferenceManager.OnPreferenceTreeClickListener, |
| 110 |
PreferenceFragment.OnPreferenceStartFragmentCallback { |
| 111 |
|
| 112 |
private static final String TAG = "PreferenceActivity"; |
| 113 |
|
| 114 |
|
| 115 |
private static final String HEADERS_TAG = ":android:headers"; |
| 116 |
private static final String CUR_HEADER_TAG = ":android:cur_header"; |
| 117 |
private static final String PREFERENCES_TAG = ":android:preferences"; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public static final String EXTRA_SHOW_FRAGMENT = ":android:show_fragment"; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":android:show_fragment_args"; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":android:show_fragment_title"; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public static final String EXTRA_SHOW_FRAGMENT_SHORT_TITLE |
| 149 |
= ":android:show_fragment_short_title"; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public static final String EXTRA_NO_HEADERS = ":android:no_headers"; |
| 159 |
|
| 160 |
private static final String BACK_STACK_PREFS = ":android:prefs"; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar"; |
| 167 |
|
| 168 |
|
| 169 |
private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip"; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text"; |
| 174 |
private static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text"; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
private final ArrayList<Header> mHeaders = new ArrayList<Header>(); |
| 179 |
|
| 180 |
private FrameLayout mListFooter; |
| 181 |
|
| 182 |
@UnsupportedAppUsage |
| 183 |
private ViewGroup mPrefsContainer; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
private CharSequence mActivityTitle; |
| 188 |
|
| 189 |
|
| 190 |
private ViewGroup mHeadersContainer; |
| 191 |
|
| 192 |
private FragmentBreadCrumbs mFragmentBreadCrumbs; |
| 193 |
|
| 194 |
private boolean mSinglePane; |
| 195 |
|
| 196 |
private Header mCurHeader; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
@UnsupportedAppUsage |
| 201 |
private PreferenceManager mPreferenceManager; |
| 202 |
|
| 203 |
private Bundle mSavedInstanceState; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
private Button mNextButton; |
| 208 |
|
| 209 |
private int mPreferenceHeaderItemResId = 0; |
| 210 |
private boolean mPreferenceHeaderRemoveEmptyIcon = false; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
private static final int FIRST_REQUEST_CODE = 100; |
| 216 |
|
| 217 |
private static final int MSG_BIND_PREFERENCES = 1; |
| 218 |
private static final int MSG_BUILD_HEADERS = 2; |
| 219 |
private Handler mHandler = new Handler() { |
| 220 |
@Override |
| 221 |
public void handleMessage(Message msg) { |
| 222 |
switch (msg.what) { |
| 223 |
case MSG_BIND_PREFERENCES: { |
| 224 |
bindPreferences(); |
| 225 |
} break; |
| 226 |
case MSG_BUILD_HEADERS: { |
| 227 |
ArrayList<Header> oldHeaders = new ArrayList<Header>(mHeaders); |
| 228 |
mHeaders.clear(); |
| 229 |
onBuildHeaders(mHeaders); |
| 230 |
if (mAdapter instanceof BaseAdapter) { |
| 231 |
((BaseAdapter) mAdapter).notifyDataSetChanged(); |
| 232 |
} |
| 233 |
Header header = onGetNewHeader(); |
| 234 |
if (header != null && header.fragment != null) { |
| 235 |
Header mappedHeader = findBestMatchingHeader(header, oldHeaders); |
| 236 |
if (mappedHeader == null || mCurHeader != mappedHeader) { |
| 237 |
switchToHeader(header); |
| 238 |
} |
| 239 |
} else if (mCurHeader != null) { |
| 240 |
Header mappedHeader = findBestMatchingHeader(mCurHeader, mHeaders); |
| 241 |
if (mappedHeader != null) { |
| 242 |
setSelectedHeader(mappedHeader); |
| 243 |
} |
| 244 |
} |
| 245 |
} break; |
| 246 |
} |
| 247 |
} |
| 248 |
}; |
| 249 |
|
| 250 |
private static class HeaderAdapter extends ArrayAdapter<Header> { |
| 251 |
private static class HeaderViewHolder { |
| 252 |
ImageView icon; |
| 253 |
TextView title; |
| 254 |
TextView summary; |
| 255 |
} |
| 256 |
|
| 257 |
private LayoutInflater mInflater; |
| 258 |
private int mLayoutResId; |
| 259 |
private boolean mRemoveIconIfEmpty; |
| 260 |
|
| 261 |
public HeaderAdapter(Context context, List<Header> objects, int layoutResId, |
| 262 |
boolean removeIconBehavior) { |
| 263 |
super(context, 0, objects); |
| 264 |
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 265 |
mLayoutResId = layoutResId; |
| 266 |
mRemoveIconIfEmpty = removeIconBehavior; |
| 267 |
} |
| 268 |
|
| 269 |
@Override |
| 270 |
public View getView(int position, View convertView, ViewGroup parent) { |
| 271 |
HeaderViewHolder holder; |
| 272 |
View view; |
| 273 |
|
| 274 |
if (convertView == null) { |
| 275 |
view = mInflater.inflate(mLayoutResId, parent, false); |
| 276 |
holder = new HeaderViewHolder(); |
| 277 |
holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon); |
| 278 |
holder.title = (TextView) view.findViewById(com.android.internal.R.id.title); |
| 279 |
holder.summary = (TextView) view.findViewById(com.android.internal.R.id.summary); |
| 280 |
view.setTag(holder); |
| 281 |
} else { |
| 282 |
view = convertView; |
| 283 |
holder = (HeaderViewHolder) view.getTag(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
Header header = getItem(position); |
| 288 |
if (mRemoveIconIfEmpty) { |
| 289 |
if (header.iconRes == 0) { |
| 290 |
holder.icon.setVisibility(View.GONE); |
| 291 |
} else { |
| 292 |
holder.icon.setVisibility(View.VISIBLE); |
| 293 |
holder.icon.setImageResource(header.iconRes); |
| 294 |
} |
| 295 |
} else { |
| 296 |
holder.icon.setImageResource(header.iconRes); |
| 297 |
} |
| 298 |
holder.title.setText(header.getTitle(getContext().getResources())); |
| 299 |
CharSequence summary = header.getSummary(getContext().getResources()); |
| 300 |
if (!TextUtils.isEmpty(summary)) { |
| 301 |
holder.summary.setVisibility(View.VISIBLE); |
| 302 |
holder.summary.setText(summary); |
| 303 |
} else { |
| 304 |
holder.summary.setVisibility(View.GONE); |
| 305 |
} |
| 306 |
|
| 307 |
return view; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public static final long HEADER_ID_UNDEFINED = -1; |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
@Deprecated |
| 328 |
public static final class Header implements Parcelable { |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public long id = HEADER_ID_UNDEFINED; |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
@StringRes |
| 342 |
public int titleRes; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
public CharSequence title; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
@StringRes |
| 355 |
public int summaryRes; |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public CharSequence summary; |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
@StringRes |
| 368 |
public int breadCrumbTitleRes; |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public CharSequence breadCrumbTitle; |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
@StringRes |
| 381 |
public int breadCrumbShortTitleRes; |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public CharSequence breadCrumbShortTitle; |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public int iconRes; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public String fragment; |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public Bundle fragmentArguments; |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public Intent intent; |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public Bundle extras; |
| 417 |
|
| 418 |
public Header() { |
| 419 |
|
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public CharSequence getTitle(Resources res) { |
| 428 |
if (titleRes != 0) { |
| 429 |
return res.getText(titleRes); |
| 430 |
} |
| 431 |
return title; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
public CharSequence getSummary(Resources res) { |
| 440 |
if (summaryRes != 0) { |
| 441 |
return res.getText(summaryRes); |
| 442 |
} |
| 443 |
return summary; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public CharSequence getBreadCrumbTitle(Resources res) { |
| 452 |
if (breadCrumbTitleRes != 0) { |
| 453 |
return res.getText(breadCrumbTitleRes); |
| 454 |
} |
| 455 |
return breadCrumbTitle; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
public CharSequence getBreadCrumbShortTitle(Resources res) { |
| 465 |
if (breadCrumbShortTitleRes != 0) { |
| 466 |
return res.getText(breadCrumbShortTitleRes); |
| 467 |
} |
| 468 |
return breadCrumbShortTitle; |
| 469 |
} |
| 470 |
|
| 471 |
@Override |
| 472 |
public int describeContents() { |
| 473 |
return 0; |
| 474 |
} |
| 475 |
|
| 476 |
@Override |
| 477 |
public void writeToParcel(Parcel dest, int flags) { |
| 478 |
dest.writeLong(id); |
| 479 |
dest.writeInt(titleRes); |
| 480 |
TextUtils.writeToParcel(title, dest, flags); |
| 481 |
dest.writeInt(summaryRes); |
| 482 |
TextUtils.writeToParcel(summary, dest, flags); |
| 483 |
dest.writeInt(breadCrumbTitleRes); |
| 484 |
TextUtils.writeToParcel(breadCrumbTitle, dest, flags); |
| 485 |
dest.writeInt(breadCrumbShortTitleRes); |
| 486 |
TextUtils.writeToParcel(breadCrumbShortTitle, dest, flags); |
| 487 |
dest.writeInt(iconRes); |
| 488 |
dest.writeString(fragment); |
| 489 |
dest.writeBundle(fragmentArguments); |
| 490 |
if (intent != null) { |
| 491 |
dest.writeInt(1); |
| 492 |
intent.writeToParcel(dest, flags); |
| 493 |
} else { |
| 494 |
dest.writeInt(0); |
| 495 |
} |
| 496 |
dest.writeBundle(extras); |
| 497 |
} |
| 498 |
|
| 499 |
public void readFromParcel(Parcel in) { |
| 500 |
id = in.readLong(); |
| 501 |
titleRes = in.readInt(); |
| 502 |
title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 503 |
summaryRes = in.readInt(); |
| 504 |
summary = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 505 |
breadCrumbTitleRes = in.readInt(); |
| 506 |
breadCrumbTitle = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 507 |
breadCrumbShortTitleRes = in.readInt(); |
| 508 |
breadCrumbShortTitle = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 509 |
iconRes = in.readInt(); |
| 510 |
fragment = in.readString(); |
| 511 |
fragmentArguments = in.readBundle(); |
| 512 |
if (in.readInt() != 0) { |
| 513 |
intent = Intent.CREATOR.createFromParcel(in); |
| 514 |
} |
| 515 |
extras = in.readBundle(); |
| 516 |
} |
| 517 |
|
| 518 |
Header(Parcel in) { |
| 519 |
readFromParcel(in); |
| 520 |
} |
| 521 |
|
| 522 |
public static final @android.annotation.NonNull Creator<Header> CREATOR = new Creator<Header>() { |
| 523 |
public Header createFromParcel(Parcel source) { |
| 524 |
return new Header(source); |
| 525 |
} |
| 526 |
public Header[] newArray(int size) { |
| 527 |
return new Header[size]; |
| 528 |
} |
| 529 |
}; |
| 530 |
} |
| 531 |
|
| 532 |
@Override |
| 533 |
public boolean onOptionsItemSelected(MenuItem item) { |
| 534 |
if (item.getItemId() == android.R.id.home) { |
| 535 |
|
| 536 |
onBackPressed(); |
| 537 |
return true; |
| 538 |
} |
| 539 |
return super.onOptionsItemSelected(item); |
| 540 |
} |
| 541 |
|
| 542 |
@Override |
| 543 |
protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 544 |
super.onCreate(savedInstanceState); |
| 545 |
|
| 546 |
|
| 547 |
TypedArray sa = obtainStyledAttributes(null, |
| 548 |
com.android.internal.R.styleable.PreferenceActivity, |
| 549 |
com.android.internal.R.attr.preferenceActivityStyle, |
| 550 |
0); |
| 551 |
|
| 552 |
final int layoutResId = sa.getResourceId( |
| 553 |
com.android.internal.R.styleable.PreferenceActivity_layout, |
| 554 |
com.android.internal.R.layout.preference_list_content); |
| 555 |
|
| 556 |
mPreferenceHeaderItemResId = sa.getResourceId( |
| 557 |
com.android.internal.R.styleable.PreferenceActivity_headerLayout, |
| 558 |
com.android.internal.R.layout.preference_header_item); |
| 559 |
mPreferenceHeaderRemoveEmptyIcon = sa.getBoolean( |
| 560 |
com.android.internal.R.styleable.PreferenceActivity_headerRemoveIconIfEmpty, |
| 561 |
false); |
| 562 |
|
| 563 |
sa.recycle(); |
| 564 |
|
| 565 |
setContentView(layoutResId); |
| 566 |
|
| 567 |
mListFooter = (FrameLayout)findViewById(com.android.internal.R.id.list_footer); |
| 568 |
mPrefsContainer = (ViewGroup) findViewById(com.android.internal.R.id.prefs_frame); |
| 569 |
mHeadersContainer = (ViewGroup) findViewById(com.android.internal.R.id.headers); |
| 570 |
boolean hidingHeaders = onIsHidingHeaders(); |
| 571 |
mSinglePane = hidingHeaders || !onIsMultiPane(); |
| 572 |
String initialFragment = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT); |
| 573 |
Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS); |
| 574 |
int initialTitle = getIntent().getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE, 0); |
| 575 |
int initialShortTitle = getIntent().getIntExtra(EXTRA_SHOW_FRAGMENT_SHORT_TITLE, 0); |
| 576 |
mActivityTitle = getTitle(); |
| 577 |
|
| 578 |
if (savedInstanceState != null) { |
| 579 |
|
| 580 |
|
| 581 |
ArrayList<Header> headers = savedInstanceState.getParcelableArrayList(HEADERS_TAG); |
| 582 |
if (headers != null) { |
| 583 |
mHeaders.addAll(headers); |
| 584 |
int curHeader = savedInstanceState.getInt(CUR_HEADER_TAG, |
| 585 |
(int) HEADER_ID_UNDEFINED); |
| 586 |
if (curHeader >= 0 && curHeader < mHeaders.size()) { |
| 587 |
setSelectedHeader(mHeaders.get(curHeader)); |
| 588 |
} else if (!mSinglePane && initialFragment == null) { |
| 589 |
switchToHeader(onGetInitialHeader()); |
| 590 |
} |
| 591 |
} else { |
| 592 |
|
| 593 |
showBreadCrumbs(getTitle(), null); |
| 594 |
} |
| 595 |
} else { |
| 596 |
if (!onIsHidingHeaders()) { |
| 597 |
onBuildHeaders(mHeaders); |
| 598 |
} |
| 599 |
|
| 600 |
if (initialFragment != null) { |
| 601 |
switchToHeader(initialFragment, initialArguments); |
| 602 |
} else if (!mSinglePane && mHeaders.size() > 0) { |
| 603 |
switchToHeader(onGetInitialHeader()); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
if (mHeaders.size() > 0) { |
| 608 |
setListAdapter(new HeaderAdapter(this, mHeaders, mPreferenceHeaderItemResId, |
| 609 |
mPreferenceHeaderRemoveEmptyIcon)); |
| 610 |
if (!mSinglePane) { |
| 611 |
getListView().setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
if (mSinglePane && initialFragment != null && initialTitle != 0) { |
| 616 |
CharSequence initialTitleStr = getText(initialTitle); |
| 617 |
CharSequence initialShortTitleStr = initialShortTitle != 0 |
| 618 |
? getText(initialShortTitle) : null; |
| 619 |
showBreadCrumbs(initialTitleStr, initialShortTitleStr); |
| 620 |
} |
| 621 |
|
| 622 |
if (mHeaders.size() == 0 && initialFragment == null) { |
| 623 |
|
| 624 |
|
| 625 |
setContentView(com.android.internal.R.layout.preference_list_content_single); |
| 626 |
mListFooter = (FrameLayout) findViewById(com.android.internal.R.id.list_footer); |
| 627 |
mPrefsContainer = (ViewGroup) findViewById(com.android.internal.R.id.prefs); |
| 628 |
mPreferenceManager = new PreferenceManager(this, FIRST_REQUEST_CODE); |
| 629 |
mPreferenceManager.setOnPreferenceTreeClickListener(this); |
| 630 |
mHeadersContainer = null; |
| 631 |
} else if (mSinglePane) { |
| 632 |
|
| 633 |
if (initialFragment != null || mCurHeader != null) { |
| 634 |
mHeadersContainer.setVisibility(View.GONE); |
| 635 |
} else { |
| 636 |
mPrefsContainer.setVisibility(View.GONE); |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
ViewGroup container = (ViewGroup) findViewById( |
| 642 |
com.android.internal.R.id.prefs_container); |
| 643 |
container.setLayoutTransition(new LayoutTransition()); |
| 644 |
} else { |
| 645 |
|
| 646 |
if (mHeaders.size() > 0 && mCurHeader != null) { |
| 647 |
setSelectedHeader(mCurHeader); |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
Intent intent = getIntent(); |
| 653 |
if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) { |
| 654 |
|
| 655 |
findViewById(com.android.internal.R.id.button_bar).setVisibility(View.VISIBLE); |
| 656 |
|
| 657 |
Button backButton = (Button)findViewById(com.android.internal.R.id.back_button); |
| 658 |
backButton.setOnClickListener(new OnClickListener() { |
| 659 |
public void onClick(View v) { |
| 660 |
setResult(RESULT_CANCELED); |
| 661 |
finish(); |
| 662 |
} |
| 663 |
}); |
| 664 |
Button skipButton = (Button)findViewById(com.android.internal.R.id.skip_button); |
| 665 |
skipButton.setOnClickListener(new OnClickListener() { |
| 666 |
public void onClick(View v) { |
| 667 |
setResult(RESULT_OK); |
| 668 |
finish(); |
| 669 |
} |
| 670 |
}); |
| 671 |
mNextButton = (Button)findViewById(com.android.internal.R.id.next_button); |
| 672 |
mNextButton.setOnClickListener(new OnClickListener() { |
| 673 |
public void onClick(View v) { |
| 674 |
setResult(RESULT_OK); |
| 675 |
finish(); |
| 676 |
} |
| 677 |
}); |
| 678 |
|
| 679 |
|
| 680 |
if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) { |
| 681 |
String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT); |
| 682 |
if (TextUtils.isEmpty(buttonText)) { |
| 683 |
mNextButton.setVisibility(View.GONE); |
| 684 |
} |
| 685 |
else { |
| 686 |
mNextButton.setText(buttonText); |
| 687 |
} |
| 688 |
} |
| 689 |
if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) { |
| 690 |
String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT); |
| 691 |
if (TextUtils.isEmpty(buttonText)) { |
| 692 |
backButton.setVisibility(View.GONE); |
| 693 |
} |
| 694 |
else { |
| 695 |
backButton.setText(buttonText); |
| 696 |
} |
| 697 |
} |
| 698 |
if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) { |
| 699 |
skipButton.setVisibility(View.VISIBLE); |
| 700 |
} |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
@Override |
| 705 |
public void onBackPressed() { |
| 706 |
if (mCurHeader != null && mSinglePane && getFragmentManager().getBackStackEntryCount() == 0 |
| 707 |
&& getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT) == null) { |
| 708 |
mCurHeader = null; |
| 709 |
|
| 710 |
mPrefsContainer.setVisibility(View.GONE); |
| 711 |
mHeadersContainer.setVisibility(View.VISIBLE); |
| 712 |
if (mActivityTitle != null) { |
| 713 |
showBreadCrumbs(mActivityTitle, null); |
| 714 |
} |
| 715 |
getListView().clearChoices(); |
| 716 |
} else { |
| 717 |
super.onBackPressed(); |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
public boolean hasHeaders() { |
| 725 |
return mHeadersContainer != null && mHeadersContainer.getVisibility() == View.VISIBLE; |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 733 |
public List<Header> getHeaders() { |
| 734 |
return mHeaders; |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
public boolean isMultiPane() { |
| 742 |
return !mSinglePane; |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
public boolean onIsMultiPane() { |
| 751 |
boolean preferMultiPane = getResources().getBoolean( |
| 752 |
com.android.internal.R.bool.preferences_prefer_dual_pane); |
| 753 |
return preferMultiPane; |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
public boolean onIsHidingHeaders() { |
| 764 |
return getIntent().getBooleanExtra(EXTRA_NO_HEADERS, false); |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
public Header onGetInitialHeader() { |
| 775 |
for (int i=0; i<mHeaders.size(); i++) { |
| 776 |
Header h = mHeaders.get(i); |
| 777 |
if (h.fragment != null) { |
| 778 |
return h; |
| 779 |
} |
| 780 |
} |
| 781 |
throw new IllegalStateException("Must have at least one header with a fragment"); |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
public Header onGetNewHeader() { |
| 791 |
return null; |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
public void onBuildHeaders(List<Header> target) { |
| 808 |
|
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
public void invalidateHeaders() { |
| 816 |
if (!mHandler.hasMessages(MSG_BUILD_HEADERS)) { |
| 817 |
mHandler.sendEmptyMessage(MSG_BUILD_HEADERS); |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
public void loadHeadersFromResource(@XmlRes int resid, List<Header> target) { |
| 829 |
XmlResourceParser parser = null; |
| 830 |
try { |
| 831 |
parser = getResources().getXml(resid); |
| 832 |
AttributeSet attrs = Xml.asAttributeSet(parser); |
| 833 |
|
| 834 |
int type; |
| 835 |
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 836 |
&& type != XmlPullParser.START_TAG) { |
| 837 |
|
| 838 |
} |
| 839 |
|
| 840 |
String nodeName = parser.getName(); |
| 841 |
if (!"preference-headers".equals(nodeName)) { |
| 842 |
throw new RuntimeException( |
| 843 |
"XML document must start with <preference-headers> tag; found" |
| 844 |
+ nodeName + " at " + parser.getPositionDescription()); |
| 845 |
} |
| 846 |
|
| 847 |
Bundle curBundle = null; |
| 848 |
|
| 849 |
final int outerDepth = parser.getDepth(); |
| 850 |
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 851 |
&& (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 852 |
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 853 |
continue; |
| 854 |
} |
| 855 |
|
| 856 |
nodeName = parser.getName(); |
| 857 |
if ("header".equals(nodeName)) { |
| 858 |
Header header = new Header(); |
| 859 |
|
| 860 |
TypedArray sa = obtainStyledAttributes( |
| 861 |
attrs, com.android.internal.R.styleable.PreferenceHeader); |
| 862 |
header.id = sa.getResourceId( |
| 863 |
com.android.internal.R.styleable.PreferenceHeader_id, |
| 864 |
(int)HEADER_ID_UNDEFINED); |
| 865 |
TypedValue tv = sa.peekValue( |
| 866 |
com.android.internal.R.styleable.PreferenceHeader_title); |
| 867 |
if (tv != null && tv.type == TypedValue.TYPE_STRING) { |
| 868 |
if (tv.resourceId != 0) { |
| 869 |
header.titleRes = tv.resourceId; |
| 870 |
} else { |
| 871 |
header.title = tv.string; |
| 872 |
} |
| 873 |
} |
| 874 |
tv = sa.peekValue( |
| 875 |
com.android.internal.R.styleable.PreferenceHeader_summary); |
| 876 |
if (tv != null && tv.type == TypedValue.TYPE_STRING) { |
| 877 |
if (tv.resourceId != 0) { |
| 878 |
header.summaryRes = tv.resourceId; |
| 879 |
} else { |
| 880 |
header.summary = tv.string; |
| 881 |
} |
| 882 |
} |
| 883 |
tv = sa.peekValue( |
| 884 |
com.android.internal.R.styleable.PreferenceHeader_breadCrumbTitle); |
| 885 |
if (tv != null && tv.type == TypedValue.TYPE_STRING) { |
| 886 |
if (tv.resourceId != 0) { |
| 887 |
header.breadCrumbTitleRes = tv.resourceId; |
| 888 |
} else { |
| 889 |
header.breadCrumbTitle = tv.string; |
| 890 |
} |
| 891 |
} |
| 892 |
tv = sa.peekValue( |
| 893 |
com.android.internal.R.styleable.PreferenceHeader_breadCrumbShortTitle); |
| 894 |
if (tv != null && tv.type == TypedValue.TYPE_STRING) { |
| 895 |
if (tv.resourceId != 0) { |
| 896 |
header.breadCrumbShortTitleRes = tv.resourceId; |
| 897 |
} else { |
| 898 |
header.breadCrumbShortTitle = tv.string; |
| 899 |
} |
| 900 |
} |
| 901 |
header.iconRes = sa.getResourceId( |
| 902 |
com.android.internal.R.styleable.PreferenceHeader_icon, 0); |
| 903 |
header.fragment = sa.getString( |
| 904 |
com.android.internal.R.styleable.PreferenceHeader_fragment); |
| 905 |
sa.recycle(); |
| 906 |
|
| 907 |
if (curBundle == null) { |
| 908 |
curBundle = new Bundle(); |
| 909 |
} |
| 910 |
|
| 911 |
final int innerDepth = parser.getDepth(); |
| 912 |
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 913 |
&& (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) { |
| 914 |
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 915 |
continue; |
| 916 |
} |
| 917 |
|
| 918 |
String innerNodeName = parser.getName(); |
| 919 |
if (innerNodeName.equals("extra")) { |
| 920 |
getResources().parseBundleExtra("extra", attrs, curBundle); |
| 921 |
XmlUtils.skipCurrentTag(parser); |
| 922 |
|
| 923 |
} else if (innerNodeName.equals("intent")) { |
| 924 |
header.intent = Intent.parseIntent(getResources(), parser, attrs); |
| 925 |
|
| 926 |
} else { |
| 927 |
XmlUtils.skipCurrentTag(parser); |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
if (curBundle.size() > 0) { |
| 932 |
header.fragmentArguments = curBundle; |
| 933 |
curBundle = null; |
| 934 |
} |
| 935 |
|
| 936 |
target.add(header); |
| 937 |
} else { |
| 938 |
XmlUtils.skipCurrentTag(parser); |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
} catch (XmlPullParserException e) { |
| 943 |
throw new RuntimeException("Error parsing headers", e); |
| 944 |
} catch (IOException e) { |
| 945 |
throw new RuntimeException("Error parsing headers", e); |
| 946 |
} finally { |
| 947 |
if (parser != null) parser.close(); |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
protected boolean isValidFragment(String fragmentName) { |
| 960 |
if (getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.KITKAT) { |
| 961 |
throw new RuntimeException( |
| 962 |
"Subclasses of PreferenceActivity must override isValidFragment(String)" |
| 963 |
+ " to verify that the Fragment class is valid! " |
| 964 |
+ this.getClass().getName() |
| 965 |
+ " has not checked if fragment " + fragmentName + " is valid."); |
| 966 |
} else { |
| 967 |
return true; |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
public void setListFooter(View view) { |
| 975 |
mListFooter.removeAllViews(); |
| 976 |
mListFooter.addView(view, new FrameLayout.LayoutParams( |
| 977 |
FrameLayout.LayoutParams.MATCH_PARENT, |
| 978 |
FrameLayout.LayoutParams.WRAP_CONTENT)); |
| 979 |
} |
| 980 |
|
| 981 |
@Override |
| 982 |
protected void onStop() { |
| 983 |
super.onStop(); |
| 984 |
|
| 985 |
if (mPreferenceManager != null) { |
| 986 |
mPreferenceManager.dispatchActivityStop(); |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
@Override |
| 991 |
protected void onDestroy() { |
| 992 |
mHandler.removeMessages(MSG_BIND_PREFERENCES); |
| 993 |
mHandler.removeMessages(MSG_BUILD_HEADERS); |
| 994 |
super.onDestroy(); |
| 995 |
|
| 996 |
if (mPreferenceManager != null) { |
| 997 |
mPreferenceManager.dispatchActivityDestroy(); |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
@Override |
| 1002 |
protected void onSaveInstanceState(Bundle outState) { |
| 1003 |
super.onSaveInstanceState(outState); |
| 1004 |
|
| 1005 |
if (mHeaders.size() > 0) { |
| 1006 |
outState.putParcelableArrayList(HEADERS_TAG, mHeaders); |
| 1007 |
if (mCurHeader != null) { |
| 1008 |
int index = mHeaders.indexOf(mCurHeader); |
| 1009 |
if (index >= 0) { |
| 1010 |
outState.putInt(CUR_HEADER_TAG, index); |
| 1011 |
} |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
if (mPreferenceManager != null) { |
| 1016 |
final PreferenceScreen preferenceScreen = getPreferenceScreen(); |
| 1017 |
if (preferenceScreen != null) { |
| 1018 |
Bundle container = new Bundle(); |
| 1019 |
preferenceScreen.saveHierarchyState(container); |
| 1020 |
outState.putBundle(PREFERENCES_TAG, container); |
| 1021 |
} |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
@Override |
| 1026 |
protected void onRestoreInstanceState(Bundle state) { |
| 1027 |
if (mPreferenceManager != null) { |
| 1028 |
Bundle container = state.getBundle(PREFERENCES_TAG); |
| 1029 |
if (container != null) { |
| 1030 |
final PreferenceScreen preferenceScreen = getPreferenceScreen(); |
| 1031 |
if (preferenceScreen != null) { |
| 1032 |
preferenceScreen.restoreHierarchyState(container); |
| 1033 |
mSavedInstanceState = state; |
| 1034 |
return; |
| 1035 |
} |
| 1036 |
} |
| 1037 |
} |
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
super.onRestoreInstanceState(state); |
| 1042 |
|
| 1043 |
if (!mSinglePane) { |
| 1044 |
|
| 1045 |
if (mCurHeader != null) { |
| 1046 |
setSelectedHeader(mCurHeader); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
@Override |
| 1052 |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 1053 |
super.onActivityResult(requestCode, resultCode, data); |
| 1054 |
|
| 1055 |
if (mPreferenceManager != null) { |
| 1056 |
mPreferenceManager.dispatchActivityResult(requestCode, resultCode, data); |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
@Override |
| 1061 |
public void onContentChanged() { |
| 1062 |
super.onContentChanged(); |
| 1063 |
|
| 1064 |
if (mPreferenceManager != null) { |
| 1065 |
postBindPreferences(); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
@Override |
| 1070 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
| 1071 |
if (!isResumed()) { |
| 1072 |
return; |
| 1073 |
} |
| 1074 |
super.onListItemClick(l, v, position, id); |
| 1075 |
|
| 1076 |
if (mAdapter != null) { |
| 1077 |
Object item = mAdapter.getItem(position); |
| 1078 |
if (item instanceof Header) onHeaderClick((Header) item, position); |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
public void onHeaderClick(Header header, int position) { |
| 1092 |
if (header.fragment != null) { |
| 1093 |
switchToHeader(header); |
| 1094 |
} else if (header.intent != null) { |
| 1095 |
startActivity(header.intent); |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args, |
| 1114 |
@StringRes int titleRes, int shortTitleRes) { |
| 1115 |
Intent intent = new Intent(Intent.ACTION_MAIN); |
| 1116 |
intent.setClass(this, getClass()); |
| 1117 |
intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentName); |
| 1118 |
intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args); |
| 1119 |
intent.putExtra(EXTRA_SHOW_FRAGMENT_TITLE, titleRes); |
| 1120 |
intent.putExtra(EXTRA_SHOW_FRAGMENT_SHORT_TITLE, shortTitleRes); |
| 1121 |
intent.putExtra(EXTRA_NO_HEADERS, true); |
| 1122 |
return intent; |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
public void startWithFragment(String fragmentName, Bundle args, |
| 1130 |
Fragment resultTo, int resultRequestCode) { |
| 1131 |
startWithFragment(fragmentName, args, resultTo, resultRequestCode, 0, 0); |
| 1132 |
} |
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
public void startWithFragment(String fragmentName, Bundle args, |
| 1152 |
Fragment resultTo, int resultRequestCode, @StringRes int titleRes, |
| 1153 |
@StringRes int shortTitleRes) { |
| 1154 |
Intent intent = onBuildStartFragmentIntent(fragmentName, args, titleRes, shortTitleRes); |
| 1155 |
if (resultTo == null) { |
| 1156 |
startActivity(intent); |
| 1157 |
} else { |
| 1158 |
resultTo.startActivityForResult(intent, resultRequestCode); |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
public void showBreadCrumbs(CharSequence title, CharSequence shortTitle) { |
| 1168 |
if (mFragmentBreadCrumbs == null) { |
| 1169 |
View crumbs = findViewById(android.R.id.title); |
| 1170 |
|
| 1171 |
try { |
| 1172 |
mFragmentBreadCrumbs = (FragmentBreadCrumbs)crumbs; |
| 1173 |
} catch (ClassCastException e) { |
| 1174 |
setTitle(title); |
| 1175 |
return; |
| 1176 |
} |
| 1177 |
if (mFragmentBreadCrumbs == null) { |
| 1178 |
if (title != null) { |
| 1179 |
setTitle(title); |
| 1180 |
} |
| 1181 |
return; |
| 1182 |
} |
| 1183 |
if (mSinglePane) { |
| 1184 |
mFragmentBreadCrumbs.setVisibility(View.GONE); |
| 1185 |
|
| 1186 |
View bcSection = findViewById(com.android.internal.R.id.breadcrumb_section); |
| 1187 |
if (bcSection != null) bcSection.setVisibility(View.GONE); |
| 1188 |
setTitle(title); |
| 1189 |
} |
| 1190 |
mFragmentBreadCrumbs.setMaxVisible(2); |
| 1191 |
mFragmentBreadCrumbs.setActivity(this); |
| 1192 |
} |
| 1193 |
if (mFragmentBreadCrumbs.getVisibility() != View.VISIBLE) { |
| 1194 |
setTitle(title); |
| 1195 |
} else { |
| 1196 |
mFragmentBreadCrumbs.setTitle(title, shortTitle); |
| 1197 |
mFragmentBreadCrumbs.setParentTitle(null, null, null); |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
public void setParentTitle(CharSequence title, CharSequence shortTitle, |
| 1209 |
OnClickListener listener) { |
| 1210 |
if (mFragmentBreadCrumbs != null) { |
| 1211 |
mFragmentBreadCrumbs.setParentTitle(title, shortTitle, listener); |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
void setSelectedHeader(Header header) { |
| 1216 |
mCurHeader = header; |
| 1217 |
int index = mHeaders.indexOf(header); |
| 1218 |
if (index >= 0) { |
| 1219 |
getListView().setItemChecked(index, true); |
| 1220 |
} else { |
| 1221 |
getListView().clearChoices(); |
| 1222 |
} |
| 1223 |
showBreadCrumbs(header); |
| 1224 |
} |
| 1225 |
|
| 1226 |
void showBreadCrumbs(Header header) { |
| 1227 |
if (header != null) { |
| 1228 |
CharSequence title = header.getBreadCrumbTitle(getResources()); |
| 1229 |
if (title == null) title = header.getTitle(getResources()); |
| 1230 |
if (title == null) title = getTitle(); |
| 1231 |
showBreadCrumbs(title, header.getBreadCrumbShortTitle(getResources())); |
| 1232 |
} else { |
| 1233 |
showBreadCrumbs(getTitle(), null); |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
private void switchToHeaderInner(String fragmentName, Bundle args) { |
| 1238 |
getFragmentManager().popBackStack(BACK_STACK_PREFS, |
| 1239 |
FragmentManager.POP_BACK_STACK_INCLUSIVE); |
| 1240 |
if (!isValidFragment(fragmentName)) { |
| 1241 |
throw new IllegalArgumentException("Invalid fragment for this activity: " |
| 1242 |
+ fragmentName); |
| 1243 |
} |
| 1244 |
|
| 1245 |
Fragment f = Fragment.instantiate(this, fragmentName, args); |
| 1246 |
FragmentTransaction transaction = getFragmentManager().beginTransaction(); |
| 1247 |
transaction.setTransition(mSinglePane |
| 1248 |
? FragmentTransaction.TRANSIT_NONE |
| 1249 |
: FragmentTransaction.TRANSIT_FRAGMENT_FADE); |
| 1250 |
transaction.replace(com.android.internal.R.id.prefs, f); |
| 1251 |
transaction.commitAllowingStateLoss(); |
| 1252 |
|
| 1253 |
if (mSinglePane && mPrefsContainer.getVisibility() == View.GONE) { |
| 1254 |
|
| 1255 |
|
| 1256 |
mPrefsContainer.setVisibility(View.VISIBLE); |
| 1257 |
mHeadersContainer.setVisibility(View.GONE); |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
public void switchToHeader(String fragmentName, Bundle args) { |
| 1269 |
Header selectedHeader = null; |
| 1270 |
for (int i = 0; i < mHeaders.size(); i++) { |
| 1271 |
if (fragmentName.equals(mHeaders.get(i).fragment)) { |
| 1272 |
selectedHeader = mHeaders.get(i); |
| 1273 |
break; |
| 1274 |
} |
| 1275 |
} |
| 1276 |
setSelectedHeader(selectedHeader); |
| 1277 |
switchToHeaderInner(fragmentName, args); |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
public void switchToHeader(Header header) { |
| 1287 |
if (mCurHeader == header) { |
| 1288 |
|
| 1289 |
|
| 1290 |
getFragmentManager().popBackStack(BACK_STACK_PREFS, |
| 1291 |
FragmentManager.POP_BACK_STACK_INCLUSIVE); |
| 1292 |
} else { |
| 1293 |
if (header.fragment == null) { |
| 1294 |
throw new IllegalStateException("can't switch to header that has no fragment"); |
| 1295 |
} |
| 1296 |
switchToHeaderInner(header.fragment, header.fragmentArguments); |
| 1297 |
setSelectedHeader(header); |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
Header findBestMatchingHeader(Header cur, ArrayList<Header> from) { |
| 1302 |
ArrayList<Header> matches = new ArrayList<Header>(); |
| 1303 |
for (int j=0; j<from.size(); j++) { |
| 1304 |
Header oh = from.get(j); |
| 1305 |
if (cur == oh || (cur.id != HEADER_ID_UNDEFINED && cur.id == oh.id)) { |
| 1306 |
|
| 1307 |
matches.clear(); |
| 1308 |
matches.add(oh); |
| 1309 |
break; |
| 1310 |
} |
| 1311 |
if (cur.fragment != null) { |
| 1312 |
if (cur.fragment.equals(oh.fragment)) { |
| 1313 |
matches.add(oh); |
| 1314 |
} |
| 1315 |
} else if (cur.intent != null) { |
| 1316 |
if (cur.intent.equals(oh.intent)) { |
| 1317 |
matches.add(oh); |
| 1318 |
} |
| 1319 |
} else if (cur.title != null) { |
| 1320 |
if (cur.title.equals(oh.title)) { |
| 1321 |
matches.add(oh); |
| 1322 |
} |
| 1323 |
} |
| 1324 |
} |
| 1325 |
final int NM = matches.size(); |
| 1326 |
if (NM == 1) { |
| 1327 |
return matches.get(0); |
| 1328 |
} else if (NM > 1) { |
| 1329 |
for (int j=0; j<NM; j++) { |
| 1330 |
Header oh = matches.get(j); |
| 1331 |
if (cur.fragmentArguments != null && |
| 1332 |
cur.fragmentArguments.equals(oh.fragmentArguments)) { |
| 1333 |
return oh; |
| 1334 |
} |
| 1335 |
if (cur.extras != null && cur.extras.equals(oh.extras)) { |
| 1336 |
return oh; |
| 1337 |
} |
| 1338 |
if (cur.title != null && cur.title.equals(oh.title)) { |
| 1339 |
return oh; |
| 1340 |
} |
| 1341 |
} |
| 1342 |
} |
| 1343 |
return null; |
| 1344 |
} |
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
public void startPreferenceFragment(Fragment fragment, boolean push) { |
| 1354 |
FragmentTransaction transaction = getFragmentManager().beginTransaction(); |
| 1355 |
transaction.replace(com.android.internal.R.id.prefs, fragment); |
| 1356 |
if (push) { |
| 1357 |
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); |
| 1358 |
transaction.addToBackStack(BACK_STACK_PREFS); |
| 1359 |
} else { |
| 1360 |
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); |
| 1361 |
} |
| 1362 |
transaction.commitAllowingStateLoss(); |
| 1363 |
} |
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
public void startPreferencePanel(String fragmentClass, Bundle args, @StringRes int titleRes, |
| 1385 |
CharSequence titleText, Fragment resultTo, int resultRequestCode) { |
| 1386 |
Fragment f = Fragment.instantiate(this, fragmentClass, args); |
| 1387 |
if (resultTo != null) { |
| 1388 |
f.setTargetFragment(resultTo, resultRequestCode); |
| 1389 |
} |
| 1390 |
FragmentTransaction transaction = getFragmentManager().beginTransaction(); |
| 1391 |
transaction.replace(com.android.internal.R.id.prefs, f); |
| 1392 |
if (titleRes != 0) { |
| 1393 |
transaction.setBreadCrumbTitle(titleRes); |
| 1394 |
} else if (titleText != null) { |
| 1395 |
transaction.setBreadCrumbTitle(titleText); |
| 1396 |
} |
| 1397 |
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); |
| 1398 |
transaction.addToBackStack(BACK_STACK_PREFS); |
| 1399 |
transaction.commitAllowingStateLoss(); |
| 1400 |
} |
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) { |
| 1412 |
|
| 1413 |
onBackPressed(); |
| 1414 |
if (caller != null) { |
| 1415 |
if (caller.getTargetFragment() != null) { |
| 1416 |
caller.getTargetFragment().onActivityResult(caller.getTargetRequestCode(), |
| 1417 |
resultCode, resultData); |
| 1418 |
} |
| 1419 |
} |
| 1420 |
} |
| 1421 |
|
| 1422 |
@Override |
| 1423 |
public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) { |
| 1424 |
startPreferencePanel(pref.getFragment(), pref.getExtras(), pref.getTitleRes(), |
| 1425 |
pref.getTitle(), null, 0); |
| 1426 |
return true; |
| 1427 |
} |
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
@UnsupportedAppUsage |
| 1436 |
private void postBindPreferences() { |
| 1437 |
if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) return; |
| 1438 |
mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget(); |
| 1439 |
} |
| 1440 |
|
| 1441 |
private void bindPreferences() { |
| 1442 |
final PreferenceScreen preferenceScreen = getPreferenceScreen(); |
| 1443 |
if (preferenceScreen != null) { |
| 1444 |
preferenceScreen.bind(getListView()); |
| 1445 |
if (mSavedInstanceState != null) { |
| 1446 |
super.onRestoreInstanceState(mSavedInstanceState); |
| 1447 |
mSavedInstanceState = null; |
| 1448 |
} |
| 1449 |
} |
| 1450 |
} |
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
@Deprecated |
| 1460 |
public PreferenceManager getPreferenceManager() { |
| 1461 |
return mPreferenceManager; |
| 1462 |
} |
| 1463 |
|
| 1464 |
@UnsupportedAppUsage |
| 1465 |
private void requirePreferenceManager() { |
| 1466 |
if (mPreferenceManager == null) { |
| 1467 |
if (mAdapter == null) { |
| 1468 |
throw new RuntimeException("This should be called after super.onCreate."); |
| 1469 |
} |
| 1470 |
throw new RuntimeException( |
| 1471 |
"Modern two-pane PreferenceActivity requires use of a PreferenceFragment"); |
| 1472 |
} |
| 1473 |
} |
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
@Deprecated |
| 1484 |
public void setPreferenceScreen(PreferenceScreen preferenceScreen) { |
| 1485 |
requirePreferenceManager(); |
| 1486 |
|
| 1487 |
if (mPreferenceManager.setPreferences(preferenceScreen) && preferenceScreen != null) { |
| 1488 |
postBindPreferences(); |
| 1489 |
CharSequence title = getPreferenceScreen().getTitle(); |
| 1490 |
|
| 1491 |
if (title != null) { |
| 1492 |
setTitle(title); |
| 1493 |
} |
| 1494 |
} |
| 1495 |
} |
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
@Deprecated |
| 1507 |
public PreferenceScreen getPreferenceScreen() { |
| 1508 |
if (mPreferenceManager != null) { |
| 1509 |
return mPreferenceManager.getPreferenceScreen(); |
| 1510 |
} |
| 1511 |
return null; |
| 1512 |
} |
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
@Deprecated |
| 1523 |
public void addPreferencesFromIntent(Intent intent) { |
| 1524 |
requirePreferenceManager(); |
| 1525 |
|
| 1526 |
setPreferenceScreen(mPreferenceManager.inflateFromIntent(intent, getPreferenceScreen())); |
| 1527 |
} |
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
@Deprecated |
| 1539 |
public void addPreferencesFromResource(int preferencesResId) { |
| 1540 |
requirePreferenceManager(); |
| 1541 |
|
| 1542 |
setPreferenceScreen(mPreferenceManager.inflateFromResource(this, preferencesResId, |
| 1543 |
getPreferenceScreen())); |
| 1544 |
} |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
@Deprecated |
| 1553 |
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
| 1554 |
return false; |
| 1555 |
} |
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
@Deprecated |
| 1568 |
public Preference findPreference(CharSequence key) { |
| 1569 |
|
| 1570 |
if (mPreferenceManager == null) { |
| 1571 |
return null; |
| 1572 |
} |
| 1573 |
|
| 1574 |
return mPreferenceManager.findPreference(key); |
| 1575 |
} |
| 1576 |
|
| 1577 |
@Override |
| 1578 |
protected void onNewIntent(Intent intent) { |
| 1579 |
if (mPreferenceManager != null) { |
| 1580 |
mPreferenceManager.dispatchNewIntent(intent); |
| 1581 |
} |
| 1582 |
} |
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
protected boolean hasNextButton() { |
| 1587 |
return mNextButton != null; |
| 1588 |
} |
| 1589 |
|
| 1590 |
protected Button getNextButton() { |
| 1591 |
return mNextButton; |
| 1592 |
} |
| 1593 |
} |