blob: a6c7d9e23ef71ccda64652a97fcd080337509182 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070019import com.android.internal.util.XmlUtils;
20
21import org.xmlpull.v1.XmlPullParser;
22import org.xmlpull.v1.XmlPullParserException;
23
24import android.app.Fragment;
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070025import android.app.FragmentBreadCrumbs;
Andrew Stadleraa904f42010-09-02 14:50:08 -070026import android.app.FragmentTransaction;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.app.ListActivity;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070028import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.Intent;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070030import android.content.res.Configuration;
31import android.content.res.TypedArray;
32import android.content.res.XmlResourceParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.Bundle;
34import android.os.Handler;
35import android.os.Message;
Dianne Hackborna21e3da2010-09-12 19:27:46 -070036import android.os.Parcel;
37import android.os.Parcelable;
Freeman Ng19ea2e02010-03-25 15:09:00 -070038import android.text.TextUtils;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070039import android.util.AttributeSet;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070040import android.util.Xml;
41import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.view.View;
Freeman Ng19ea2e02010-03-25 15:09:00 -070043import android.view.View.OnClickListener;
Andrew Stadleraa904f42010-09-02 14:50:08 -070044import android.view.ViewGroup;
Dianne Hackborna21e3da2010-09-12 19:27:46 -070045import android.widget.AbsListView;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070046import android.widget.ArrayAdapter;
Freeman Ng19ea2e02010-03-25 15:09:00 -070047import android.widget.Button;
Dianne Hackborn5c769a42010-08-26 17:08:08 -070048import android.widget.FrameLayout;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070049import android.widget.ImageView;
50import android.widget.ListView;
51import android.widget.TextView;
52
53import java.io.IOException;
54import java.util.ArrayList;
55import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57/**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070058 * This is the base class for an activity to show a hierarchy of preferences
59 * to the user. Prior to {@link android.os.Build.VERSION_CODES#HONEYCOMB}
60 * this class only allowed the display of a single set of preference; this
61 * functionality should now be found in the new {@link PreferenceFragment}
62 * class. If you are using PreferenceActivity in its old mode, the documentation
63 * there applies to the deprecated APIs here.
Freeman Ng19ea2e02010-03-25 15:09:00 -070064 *
Dianne Hackbornb1ad5972010-08-02 17:30:33 -070065 * <p>This activity shows one or more headers of preferences, each of with
66 * is associated with a {@link PreferenceFragment} to display the preferences
67 * of that header. The actual layout and display of these associations can
68 * however vary; currently there are two major approaches it may take:
69 *
70 * <ul>
71 * <li>On a small screen it may display only the headers as a single list
72 * when first launched. Selecting one of the header items will re-launch
73 * the activity with it only showing the PreferenceFragment of that header.
74 * <li>On a large screen in may display both the headers and current
75 * PreferenceFragment together as panes. Selecting a header item switches
76 * to showing the correct PreferenceFragment for that item.
77 * </ul>
78 *
79 * <p>Subclasses of PreferenceActivity should implement
80 * {@link #onBuildHeaders} to populate the header list with the desired
81 * items. Doing this implicitly switches the class into its new "headers
82 * + fragments" mode rather than the old style of just showing a single
83 * preferences list.
84 *
85 * <a name="SampleCode"></a>
86 * <h3>Sample Code</h3>
87 *
88 * <p>The following sample code shows a simple preference activity that
89 * has two different sets of preferences. The implementation, consisting
90 * of the activity itself as well as its two preference fragments is:</p>
91 *
92 * {@sample development/samples/ApiDemos/src/com/example/android/apis/preference/PreferenceWithHeaders.java
93 * activity}
94 *
95 * <p>The preference_headers resource describes the headers to be displayed
96 * and the fragments associated with them. It is:
97 *
98 * {@sample development/samples/ApiDemos/res/xml/preference_headers.xml headers}
99 *
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700100 * <p>The first header is shown by Prefs1Fragment, which populates itself
101 * from the following XML resource:</p>
102 *
103 * {@sample development/samples/ApiDemos/res/xml/fragmented_preferences.xml preferences}
104 *
105 * <p>Note that this XML resource contains a preference screen holding another
106 * fragment, the Prefs1FragmentInner implemented here. This allows the user
107 * to traverse down a hierarchy of preferences; pressing back will pop each
108 * fragment off the stack to return to the previous preferences.
109 *
110 * <p>See {@link PreferenceFragment} for information on implementing the
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700111 * fragments themselves.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 */
113public abstract class PreferenceActivity extends ListActivity implements
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700114 PreferenceManager.OnPreferenceTreeClickListener,
115 PreferenceFragment.OnPreferenceStartFragmentCallback {
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700116 private static final String TAG = "PreferenceActivity";
Freeman Ng19ea2e02010-03-25 15:09:00 -0700117
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700118 // Constants for state save/restore
119 private static final String HEADERS_TAG = ":android:headers";
120 private static final String CUR_HEADER_TAG = ":android:cur_header";
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700121 private static final String PREFERENCES_TAG = ":android:preferences";
Freeman Ng19ea2e02010-03-25 15:09:00 -0700122
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700123 /**
124 * When starting this activity, the invoking Intent can contain this extra
125 * string to specify which fragment should be initially displayed.
126 */
127 public static final String EXTRA_SHOW_FRAGMENT = ":android:show_fragment";
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700128
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700129 /**
130 * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
131 * this extra can also be specify to supply a Bundle of arguments to pass
132 * to that fragment when it is instantiated during the initial creation
133 * of PreferenceActivity.
134 */
135 public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":android:show_fragment_args";
136
137 /**
138 * When starting this activity, the invoking Intent can contain this extra
139 * boolean that the header list should not be displayed. This is most often
140 * used in conjunction with {@link #EXTRA_SHOW_FRAGMENT} to launch
141 * the activity to display a specific fragment that the user has navigated
142 * to.
143 */
144 public static final String EXTRA_NO_HEADERS = ":android:no_headers";
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700145
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700146 private static final String BACK_STACK_PREFS = ":android:prefs";
147
Freeman Ng19ea2e02010-03-25 15:09:00 -0700148 // extras that allow any preference activity to be launched as part of a wizard
149
150 // show Back and Next buttons? takes boolean parameter
151 // Back will then return RESULT_CANCELED and Next RESULT_OK
152 private static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
153
Freeman Ng09dbf182010-08-11 15:45:01 -0700154 // add a Skip button?
155 private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
156
Freeman Ng19ea2e02010-03-25 15:09:00 -0700157 // specify custom text for the Back or Next buttons, or cause a button to not appear
158 // at all by setting it to null
159 private static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
160 private static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
161
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700162 // --- State for new mode when showing a list of headers + prefs fragment
163
164 private final ArrayList<Header> mHeaders = new ArrayList<Header>();
165
166 private HeaderAdapter mAdapter;
167
Dianne Hackborn5c769a42010-08-26 17:08:08 -0700168 private FrameLayout mListFooter;
169
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700170 private View mPrefsContainer;
171
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700172 private FragmentBreadCrumbs mFragmentBreadCrumbs;
173
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700174 private boolean mSinglePane;
175
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700176 private Header mCurHeader;
177
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700178 // --- State for old mode when showing a single preference list
Freeman Ng19ea2e02010-03-25 15:09:00 -0700179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 private PreferenceManager mPreferenceManager;
Freeman Ng19ea2e02010-03-25 15:09:00 -0700181
Adam Powelle7fea452010-03-18 14:51:39 -0700182 private Bundle mSavedInstanceState;
183
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700184 // --- Common state
185
186 private Button mNextButton;
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 /**
189 * The starting request code given out to preference framework.
190 */
191 private static final int FIRST_REQUEST_CODE = 100;
Freeman Ng19ea2e02010-03-25 15:09:00 -0700192
Dianne Hackborn3e449ce2010-09-11 20:52:31 -0700193 private static final int MSG_BIND_PREFERENCES = 1;
194 private static final int MSG_BUILD_HEADERS = 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 private Handler mHandler = new Handler() {
196 @Override
197 public void handleMessage(Message msg) {
198 switch (msg.what) {
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700199 case MSG_BIND_PREFERENCES: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 bindPreferences();
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700201 } break;
202 case MSG_BUILD_HEADERS: {
203 ArrayList<Header> oldHeaders = new ArrayList<Header>(mHeaders);
204 mHeaders.clear();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700205 onBuildHeaders(mHeaders);
206 mAdapter.notifyDataSetChanged();
Dianne Hackborn5c769a42010-08-26 17:08:08 -0700207 Header header = onGetNewHeader();
208 if (header != null && header.fragment != null) {
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700209 Header mappedHeader = findBestMatchingHeader(header, oldHeaders);
210 if (mappedHeader == null || mCurHeader != mappedHeader) {
211 switchToHeader(header);
212 }
213 } else if (mCurHeader != null) {
214 Header mappedHeader = findBestMatchingHeader(mCurHeader, mHeaders);
215 if (mappedHeader != null) {
216 setSelectedHeader(mappedHeader);
217 } else {
218 switchToHeader(null);
219 }
Dianne Hackborn5c769a42010-08-26 17:08:08 -0700220 }
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700221 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 }
223 }
224 };
225
Andrew Stadler468c3232010-08-17 16:16:42 -0700226 private static class HeaderAdapter extends ArrayAdapter<Header> {
227 private static class HeaderViewHolder {
228 ImageView icon;
229 TextView title;
230 TextView summary;
231 }
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700232
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700233 private LayoutInflater mInflater;
234
235 public HeaderAdapter(Context context, List<Header> objects) {
236 super(context, 0, objects);
237 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
238 }
239
240 @Override
241 public View getView(int position, View convertView, ViewGroup parent) {
242 HeaderViewHolder holder;
243 View view;
244
245 if (convertView == null) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -0700246 view = mInflater.inflate(com.android.internal.R.layout.preference_header_item,
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700247 parent, false);
248 holder = new HeaderViewHolder();
Andrew Stadler468c3232010-08-17 16:16:42 -0700249 holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
250 holder.title = (TextView) view.findViewById(com.android.internal.R.id.title);
251 holder.summary = (TextView) view.findViewById(com.android.internal.R.id.summary);
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700252 view.setTag(holder);
253 } else {
254 view = convertView;
Andrew Stadler468c3232010-08-17 16:16:42 -0700255 holder = (HeaderViewHolder) view.getTag();
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700256 }
257
Andrew Stadler468c3232010-08-17 16:16:42 -0700258 // All view fields must be updated every time, because the view may be recycled
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700259 Header header = getItem(position);
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700260 holder.icon.setImageResource(header.iconRes);
Andrew Stadler468c3232010-08-17 16:16:42 -0700261 holder.title.setText(header.title);
262 if (TextUtils.isEmpty(header.summary)) {
263 holder.summary.setVisibility(View.GONE);
264 } else {
265 holder.summary.setVisibility(View.VISIBLE);
266 holder.summary.setText(header.summary);
267 }
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700268
269 return view;
270 }
271 }
272
273 /**
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700274 * Default value for {@link Header#id Header.id} indicating that no
275 * identifier value is set. All other values (including those below -1)
276 * are valid.
277 */
278 public static final long HEADER_ID_UNDEFINED = -1;
279
280 /**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700281 * Description of a single Header item that the user can select.
282 */
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700283 public static final class Header implements Parcelable {
284 /**
285 * Identifier for this header, to correlate with a new list when
286 * it is updated. The default value is
287 * {@link PreferenceActivity#HEADER_ID_UNDEFINED}, meaning no id.
288 * @attr ref android.R.styleable#PreferenceHeader_id
289 */
290 public long id = HEADER_ID_UNDEFINED;
291
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700292 /**
293 * Title of the header that is shown to the user.
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700294 * @attr ref android.R.styleable#PreferenceHeader_title
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700295 */
Andrew Stadler468c3232010-08-17 16:16:42 -0700296 public CharSequence title;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700297
298 /**
299 * Optional summary describing what this header controls.
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700300 * @attr ref android.R.styleable#PreferenceHeader_summary
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700301 */
Andrew Stadler468c3232010-08-17 16:16:42 -0700302 public CharSequence summary;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700303
304 /**
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700305 * Optional text to show as the title in the bread crumb.
306 * @attr ref android.R.styleable#PreferenceHeader_breadCrumbTitle
307 */
308 public CharSequence breadCrumbTitle;
309
310 /**
311 * Optional text to show as the short title in the bread crumb.
312 * @attr ref android.R.styleable#PreferenceHeader_breadCrumbShortTitle
313 */
314 public CharSequence breadCrumbShortTitle;
315
316 /**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700317 * Optional icon resource to show for this header.
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700318 * @attr ref android.R.styleable#PreferenceHeader_icon
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700319 */
Andrew Stadler468c3232010-08-17 16:16:42 -0700320 public int iconRes;
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700321
322 /**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700323 * Full class name of the fragment to display when this header is
324 * selected.
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700325 * @attr ref android.R.styleable#PreferenceHeader_fragment
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700326 */
Andrew Stadler468c3232010-08-17 16:16:42 -0700327 public String fragment;
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700328
329 /**
330 * Optional arguments to supply to the fragment when it is
331 * instantiated.
332 */
Andrew Stadler468c3232010-08-17 16:16:42 -0700333 public Bundle fragmentArguments;
Dianne Hackborn5c769a42010-08-26 17:08:08 -0700334
335 /**
336 * Intent to launch when the preference is selected.
337 */
338 public Intent intent;
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700339
340 /**
341 * Optional additional data for use by subclasses of PreferenceActivity.
342 */
343 public Bundle extras;
344
345 public Header() {
346 }
347
348 @Override
349 public int describeContents() {
350 return 0;
351 }
352
353 @Override
354 public void writeToParcel(Parcel dest, int flags) {
355 dest.writeLong(id);
356 TextUtils.writeToParcel(title, dest, flags);
357 TextUtils.writeToParcel(summary, dest, flags);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700358 TextUtils.writeToParcel(breadCrumbTitle, dest, flags);
359 TextUtils.writeToParcel(breadCrumbShortTitle, dest, flags);
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700360 dest.writeInt(iconRes);
361 dest.writeString(fragment);
362 dest.writeBundle(fragmentArguments);
363 if (intent != null) {
364 dest.writeInt(1);
365 intent.writeToParcel(dest, flags);
366 } else {
367 dest.writeInt(0);
368 }
369 dest.writeBundle(extras);
370 }
371
372 public void readFromParcel(Parcel in) {
373 id = in.readLong();
374 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
375 summary = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700376 breadCrumbTitle = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
377 breadCrumbShortTitle = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700378 iconRes = in.readInt();
379 fragment = in.readString();
380 fragmentArguments = in.readBundle();
381 if (in.readInt() != 0) {
382 intent = Intent.CREATOR.createFromParcel(in);
383 }
384 extras = in.readBundle();
385 }
386
387 Header(Parcel in) {
388 readFromParcel(in);
389 }
390
391 public static final Creator<Header> CREATOR = new Creator<Header>() {
392 public Header createFromParcel(Parcel source) {
393 return new Header(source);
394 }
395 public Header[] newArray(int size) {
396 return new Header[size];
397 }
398 };
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700399 }
400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 @Override
402 protected void onCreate(Bundle savedInstanceState) {
403 super.onCreate(savedInstanceState);
404
405 setContentView(com.android.internal.R.layout.preference_list_content);
Freeman Ng19ea2e02010-03-25 15:09:00 -0700406
Dianne Hackborn5c769a42010-08-26 17:08:08 -0700407 mListFooter = (FrameLayout)findViewById(com.android.internal.R.id.list_footer);
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700408 mPrefsContainer = findViewById(com.android.internal.R.id.prefs);
409 boolean hidingHeaders = onIsHidingHeaders();
410 mSinglePane = hidingHeaders || !onIsMultiPane();
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700411 String initialFragment = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
412 Bundle initialArguments = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700413
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700414 if (savedInstanceState != null) {
415 // We are restarting from a previous saved state; used that to
416 // initialize, instead of starting fresh.
417 ArrayList<Header> headers = savedInstanceState.getParcelableArrayList(HEADERS_TAG);
418 if (headers != null) {
419 mHeaders.addAll(headers);
420 int curHeader = savedInstanceState.getInt(CUR_HEADER_TAG,
421 (int)HEADER_ID_UNDEFINED);
422 if (curHeader >= 0 && curHeader < mHeaders.size()) {
423 setSelectedHeader(mHeaders.get(curHeader));
424 }
425 }
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700426
427 } else {
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700428 if (initialFragment != null && mSinglePane) {
429 // If we are just showing a fragment, we want to run in
430 // new fragment mode, but don't need to compute and show
431 // the headers.
432 switchToHeader(initialFragment, initialArguments);
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700433
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700434 } else {
435 // We need to try to build the headers.
436 onBuildHeaders(mHeaders);
437
438 // If there are headers, then at this point we need to show
439 // them and, depending on the screen, we may also show in-line
440 // the currently selected preference fragment.
441 if (mHeaders.size() > 0) {
442 if (!mSinglePane) {
443 if (initialFragment == null) {
444 Header h = onGetInitialHeader();
445 switchToHeader(h);
446 } else {
447 switchToHeader(initialFragment, initialArguments);
448 }
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700449 }
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700450 }
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700451 }
452 }
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700453
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700454 // The default configuration is to only show the list view. Adjust
455 // visibility for other configurations.
456 if (initialFragment != null && mSinglePane) {
457 // Single pane, showing just a prefs fragment.
Amith Yamasani05fbc312010-09-26 13:29:01 -0700458 findViewById(com.android.internal.R.id.headers).setVisibility(View.GONE);
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700459 mPrefsContainer.setVisibility(View.VISIBLE);
460 } else if (mHeaders.size() > 0) {
461 mAdapter = new HeaderAdapter(this, mHeaders);
462 setListAdapter(mAdapter);
463 if (!mSinglePane) {
464 // Multi-pane.
465 getListView().setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -0700466 if (mCurHeader != null) {
467 setSelectedHeader(mCurHeader);
468 }
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700469 mPrefsContainer.setVisibility(View.VISIBLE);
470 }
471 } else {
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700472 // If there are no headers, we are in the old "just show a screen
473 // of preferences" mode.
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700474 mPreferenceManager = new PreferenceManager(this, FIRST_REQUEST_CODE);
475 mPreferenceManager.setOnPreferenceTreeClickListener(this);
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700476 }
477
478 getListView().setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
479
Freeman Ng19ea2e02010-03-25 15:09:00 -0700480 // see if we should show Back/Next buttons
481 Intent intent = getIntent();
482 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
483
484 findViewById(com.android.internal.R.id.button_bar).setVisibility(View.VISIBLE);
485
486 Button backButton = (Button)findViewById(com.android.internal.R.id.back_button);
487 backButton.setOnClickListener(new OnClickListener() {
488 public void onClick(View v) {
489 setResult(RESULT_CANCELED);
490 finish();
491 }
492 });
Freeman Ng09dbf182010-08-11 15:45:01 -0700493 Button skipButton = (Button)findViewById(com.android.internal.R.id.skip_button);
494 skipButton.setOnClickListener(new OnClickListener() {
495 public void onClick(View v) {
496 setResult(RESULT_OK);
497 finish();
498 }
499 });
Freeman Ng19ea2e02010-03-25 15:09:00 -0700500 mNextButton = (Button)findViewById(com.android.internal.R.id.next_button);
501 mNextButton.setOnClickListener(new OnClickListener() {
502 public void onClick(View v) {
503 setResult(RESULT_OK);
504 finish();
505 }
506 });
507
508 // set our various button parameters
509 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
510 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
511 if (TextUtils.isEmpty(buttonText)) {
512 mNextButton.setVisibility(View.GONE);
513 }
514 else {
515 mNextButton.setText(buttonText);
516 }
517 }
518 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
519 String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
520 if (TextUtils.isEmpty(buttonText)) {
521 backButton.setVisibility(View.GONE);
522 }
523 else {
524 backButton.setText(buttonText);
525 }
526 }
Freeman Ng09dbf182010-08-11 15:45:01 -0700527 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
528 skipButton.setVisibility(View.VISIBLE);
529 }
Freeman Ng19ea2e02010-03-25 15:09:00 -0700530 }
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700531 }
Freeman Ng19ea2e02010-03-25 15:09:00 -0700532
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700533 /**
Dianne Hackborn291905e2010-08-17 15:17:15 -0700534 * Returns true if this activity is currently showing the header list.
535 */
536 public boolean hasHeaders() {
537 return getListView().getVisibility() == View.VISIBLE
538 && mPreferenceManager == null;
539 }
540
541 /**
542 * Returns true if this activity is showing multiple panes -- the headers
543 * and a preference fragment.
544 */
545 public boolean isMultiPane() {
546 return hasHeaders() && mPrefsContainer.getVisibility() == View.VISIBLE;
547 }
548
549 /**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700550 * Called to determine if the activity should run in multi-pane mode.
551 * The default implementation returns true if the screen is large
552 * enough.
553 */
554 public boolean onIsMultiPane() {
555 Configuration config = getResources().getConfiguration();
556 if ((config.screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK)
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700557 == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
558 return true;
559 }
560 if ((config.screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK)
561 == Configuration.SCREENLAYOUT_SIZE_LARGE
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700562 && config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
563 return true;
564 }
565 return false;
566 }
567
568 /**
Dianne Hackborn291905e2010-08-17 15:17:15 -0700569 * Called to determine whether the header list should be hidden.
570 * The default implementation returns the
571 * value given in {@link #EXTRA_NO_HEADERS} or false if it is not supplied.
572 * This is set to false, for example, when the activity is being re-launched
573 * to show a particular preference activity.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700574 */
575 public boolean onIsHidingHeaders() {
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700576 return getIntent().getBooleanExtra(EXTRA_NO_HEADERS, false);
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700577 }
578
579 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700580 * Called to determine the initial header to be shown. The default
581 * implementation simply returns the fragment of the first header. Note
582 * that the returned Header object does not actually need to exist in
583 * your header list -- whatever its fragment is will simply be used to
584 * show for the initial UI.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700585 */
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700586 public Header onGetInitialHeader() {
587 return mHeaders.get(0);
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700588 }
589
590 /**
Dianne Hackborn5c769a42010-08-26 17:08:08 -0700591 * Called after the header list has been updated ({@link #onBuildHeaders}
592 * has been called and returned due to {@link #invalidateHeaders()}) to
593 * specify the header that should now be selected. The default implementation
594 * returns null to keep whatever header is currently selected.
595 */
596 public Header onGetNewHeader() {
597 return null;
598 }
599
600 /**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700601 * Called when the activity needs its list of headers build. By
602 * implementing this and adding at least one item to the list, you
603 * will cause the activity to run in its modern fragment mode. Note
604 * that this function may not always be called; for example, if the
605 * activity has been asked to display a particular fragment without
606 * the header list, there is no need to build the headers.
607 *
608 * <p>Typical implementations will use {@link #loadHeadersFromResource}
609 * to fill in the list from a resource.
610 *
611 * @param target The list in which to place the headers.
612 */
613 public void onBuildHeaders(List<Header> target) {
614 }
615
616 /**
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700617 * Call when you need to change the headers being displayed. Will result
618 * in onBuildHeaders() later being called to retrieve the new list.
619 */
620 public void invalidateHeaders() {
621 if (!mHandler.hasMessages(MSG_BUILD_HEADERS)) {
622 mHandler.sendEmptyMessage(MSG_BUILD_HEADERS);
623 }
624 }
625
626 /**
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700627 * Parse the given XML file as a header description, adding each
628 * parsed Header into the target list.
629 *
630 * @param resid The XML resource to load and parse.
631 * @param target The list in which the parsed headers should be placed.
632 */
633 public void loadHeadersFromResource(int resid, List<Header> target) {
634 XmlResourceParser parser = null;
635 try {
636 parser = getResources().getXml(resid);
637 AttributeSet attrs = Xml.asAttributeSet(parser);
638
639 int type;
640 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
641 && type != XmlPullParser.START_TAG) {
642 }
643
644 String nodeName = parser.getName();
Dianne Hackborndef15372010-08-15 12:43:52 -0700645 if (!"preference-headers".equals(nodeName)) {
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700646 throw new RuntimeException(
Dianne Hackborndef15372010-08-15 12:43:52 -0700647 "XML document must start with <preference-headers> tag; found"
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700648 + nodeName + " at " + parser.getPositionDescription());
649 }
650
Dianne Hackborndef15372010-08-15 12:43:52 -0700651 Bundle curBundle = null;
652
Dianne Hackborn5c769a42010-08-26 17:08:08 -0700653 final int outerDepth = parser.getDepth();
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700654 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
655 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
656 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
657 continue;
658 }
659
660 nodeName = parser.getName();
Dianne Hackborndef15372010-08-15 12:43:52 -0700661 if ("header".equals(nodeName)) {
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700662 Header header = new Header();
663
664 TypedArray sa = getResources().obtainAttributes(attrs,
665 com.android.internal.R.styleable.PreferenceHeader);
Amith Yamasanied13cde2010-09-17 16:56:47 -0700666 header.id = sa.getResourceId(
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700667 com.android.internal.R.styleable.PreferenceHeader_id,
668 (int)HEADER_ID_UNDEFINED);
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700669 header.title = sa.getText(
670 com.android.internal.R.styleable.PreferenceHeader_title);
671 header.summary = sa.getText(
672 com.android.internal.R.styleable.PreferenceHeader_summary);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700673 header.breadCrumbTitle = sa.getText(
674 com.android.internal.R.styleable.PreferenceHeader_breadCrumbTitle);
675 header.breadCrumbShortTitle = sa.getText(
676 com.android.internal.R.styleable.PreferenceHeader_breadCrumbShortTitle);
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700677 header.iconRes = sa.getResourceId(
678 com.android.internal.R.styleable.PreferenceHeader_icon, 0);
679 header.fragment = sa.getString(
680 com.android.internal.R.styleable.PreferenceHeader_fragment);
681 sa.recycle();
682
Dianne Hackborndef15372010-08-15 12:43:52 -0700683 if (curBundle == null) {
684 curBundle = new Bundle();
685 }
Dianne Hackborn5c769a42010-08-26 17:08:08 -0700686
687 final int innerDepth = parser.getDepth();
688 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
689 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
690 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
691 continue;
692 }
693
694 String innerNodeName = parser.getName();
695 if (innerNodeName.equals("extra")) {
696 getResources().parseBundleExtra("extra", attrs, curBundle);
697 XmlUtils.skipCurrentTag(parser);
698
699 } else if (innerNodeName.equals("intent")) {
700 header.intent = Intent.parseIntent(getResources(), parser, attrs);
701
702 } else {
703 XmlUtils.skipCurrentTag(parser);
704 }
705 }
706
Dianne Hackborndef15372010-08-15 12:43:52 -0700707 if (curBundle.size() > 0) {
708 header.fragmentArguments = curBundle;
709 curBundle = null;
710 }
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700711
Dianne Hackborndef15372010-08-15 12:43:52 -0700712 target.add(header);
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700713 } else {
714 XmlUtils.skipCurrentTag(parser);
715 }
716 }
717
718 } catch (XmlPullParserException e) {
719 throw new RuntimeException("Error parsing headers", e);
720 } catch (IOException e) {
721 throw new RuntimeException("Error parsing headers", e);
722 } finally {
723 if (parser != null) parser.close();
724 }
725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 }
727
Dianne Hackborn5c769a42010-08-26 17:08:08 -0700728 /**
729 * Set a footer that should be shown at the bottom of the header list.
730 */
731 public void setListFooter(View view) {
732 mListFooter.removeAllViews();
733 mListFooter.addView(view, new FrameLayout.LayoutParams(
734 FrameLayout.LayoutParams.MATCH_PARENT,
735 FrameLayout.LayoutParams.WRAP_CONTENT));
736 }
737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 @Override
739 protected void onStop() {
740 super.onStop();
Freeman Ng19ea2e02010-03-25 15:09:00 -0700741
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700742 if (mPreferenceManager != null) {
743 mPreferenceManager.dispatchActivityStop();
744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
746
747 @Override
748 protected void onDestroy() {
749 super.onDestroy();
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700750
751 if (mPreferenceManager != null) {
752 mPreferenceManager.dispatchActivityDestroy();
753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 }
755
756 @Override
757 protected void onSaveInstanceState(Bundle outState) {
758 super.onSaveInstanceState(outState);
759
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700760 if (mHeaders.size() > 0) {
761 outState.putParcelableArrayList(HEADERS_TAG, mHeaders);
762 if (mCurHeader != null) {
763 int index = mHeaders.indexOf(mCurHeader);
764 if (index >= 0) {
765 outState.putInt(CUR_HEADER_TAG, index);
766 }
767 }
768 }
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700769
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700770 if (mPreferenceManager != null) {
771 final PreferenceScreen preferenceScreen = getPreferenceScreen();
772 if (preferenceScreen != null) {
773 Bundle container = new Bundle();
774 preferenceScreen.saveHierarchyState(container);
775 outState.putBundle(PREFERENCES_TAG, container);
776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 }
778 }
779
780 @Override
781 protected void onRestoreInstanceState(Bundle state) {
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700782 if (mPreferenceManager != null) {
783 Bundle container = state.getBundle(PREFERENCES_TAG);
784 if (container != null) {
785 final PreferenceScreen preferenceScreen = getPreferenceScreen();
786 if (preferenceScreen != null) {
787 preferenceScreen.restoreHierarchyState(container);
788 mSavedInstanceState = state;
789 return;
790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 }
792 }
Adam Powelle7fea452010-03-18 14:51:39 -0700793
794 // Only call this if we didn't save the instance state for later.
795 // If we did save it, it will be restored when we bind the adapter.
796 super.onRestoreInstanceState(state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 }
798
799 @Override
800 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
801 super.onActivityResult(requestCode, resultCode, data);
Freeman Ng19ea2e02010-03-25 15:09:00 -0700802
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700803 if (mPreferenceManager != null) {
804 mPreferenceManager.dispatchActivityResult(requestCode, resultCode, data);
805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 }
807
808 @Override
809 public void onContentChanged() {
810 super.onContentChanged();
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700811
812 if (mPreferenceManager != null) {
813 postBindPreferences();
814 }
815 }
816
817 @Override
818 protected void onListItemClick(ListView l, View v, int position, long id) {
819 super.onListItemClick(l, v, position, id);
820
821 if (mAdapter != null) {
822 onHeaderClick(mHeaders.get(position), position);
823 }
824 }
825
826 /**
827 * Called when the user selects an item in the header list. The default
Jean-Baptiste Queru72dc7802010-08-13 09:25:19 -0700828 * implementation will call either {@link #startWithFragment(String, Bundle)}
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700829 * or {@link #switchToHeader(Header)} as appropriate.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700830 *
831 * @param header The header that was selected.
832 * @param position The header's position in the list.
833 */
834 public void onHeaderClick(Header header, int position) {
Dianne Hackborn5c769a42010-08-26 17:08:08 -0700835 if (header.fragment != null) {
836 if (mSinglePane) {
837 startWithFragment(header.fragment, header.fragmentArguments);
838 } else {
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700839 switchToHeader(header);
Dianne Hackborn5c769a42010-08-26 17:08:08 -0700840 }
841 } else if (header.intent != null) {
842 startActivity(header.intent);
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700843 }
844 }
845
846 /**
847 * Start a new instance of this activity, showing only the given
848 * preference fragment. When launched in this mode, the header list
849 * will be hidden and the given preference fragment will be instantiated
850 * and fill the entire activity.
851 *
852 * @param fragmentName The name of the fragment to display.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700853 * @param args Optional arguments to supply to the fragment.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700854 */
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700855 public void startWithFragment(String fragmentName, Bundle args) {
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700856 Intent intent = new Intent(Intent.ACTION_MAIN);
857 intent.setClass(this, getClass());
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700858 intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentName);
859 intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
860 intent.putExtra(EXTRA_NO_HEADERS, true);
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700861 startActivity(intent);
862 }
863
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700864 /**
865 * Change the base title of the bread crumbs for the current preferences.
866 * This will normally be called for you. See
867 * {@link android.app.FragmentBreadCrumbs} for more information.
868 */
869 public void showBreadCrumbs(CharSequence title, CharSequence shortTitle) {
870 if (mFragmentBreadCrumbs == null) {
871 mFragmentBreadCrumbs = new FragmentBreadCrumbs(this);
872 mFragmentBreadCrumbs.setActivity(this);
873 getActionBar().setCustomNavigationMode(mFragmentBreadCrumbs);
874 }
875 mFragmentBreadCrumbs.setTitle(title, shortTitle);
876 }
877
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700878 void setSelectedHeader(Header header) {
879 mCurHeader = header;
880 int index = mHeaders.indexOf(header);
881 if (index >= 0) {
882 getListView().setItemChecked(index, true);
883 } else {
884 getListView().clearChoices();
885 }
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700886 if (header != null) {
887 CharSequence title = header.breadCrumbTitle;
888 if (title == null) title = header.title;
889 if (title == null) title = getTitle();
890 showBreadCrumbs(title, header.breadCrumbShortTitle);
891 } else {
892 showBreadCrumbs(getTitle(), null);
893 }
894 }
895
896 public void switchToHeaderInner(String fragmentName, Bundle args) {
897 getFragmentManager().popBackStack(BACK_STACK_PREFS, POP_BACK_STACK_INCLUSIVE);
898 Fragment f = Fragment.instantiate(this, fragmentName, args);
899 getFragmentManager().openTransaction().replace(
900 com.android.internal.R.id.prefs, f).commit();
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700901 }
902
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700903 /**
904 * When in two-pane mode, switch the fragment pane to show the given
905 * preference fragment.
906 *
907 * @param fragmentName The name of the fragment to display.
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700908 * @param args Optional arguments to supply to the fragment.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -0700909 */
Dianne Hackbornb7a2e472010-08-12 16:20:42 -0700910 public void switchToHeader(String fragmentName, Bundle args) {
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700911 setSelectedHeader(null);
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700912 switchToHeaderInner(fragmentName, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914
Andrew Stadleraa904f42010-09-02 14:50:08 -0700915 /**
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700916 * When in two-pane mode, switch to the fragment pane to show the given
917 * preference fragment.
918 *
919 * @param header The new header to display.
920 */
921 public void switchToHeader(Header header) {
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700922 switchToHeaderInner(header.fragment, header.fragmentArguments);
Dianne Hackborna21e3da2010-09-12 19:27:46 -0700923 setSelectedHeader(header);
924 }
925
926 Header findBestMatchingHeader(Header cur, ArrayList<Header> from) {
927 ArrayList<Header> matches = new ArrayList<Header>();
928 for (int j=0; j<from.size(); j++) {
929 Header oh = from.get(j);
930 if (cur == oh || (cur.id != HEADER_ID_UNDEFINED && cur.id == oh.id)) {
931 // Must be this one.
932 matches.clear();
933 matches.add(oh);
934 break;
935 }
936 if (cur.fragment != null) {
937 if (cur.fragment.equals(oh.fragment)) {
938 matches.add(oh);
939 }
940 } else if (cur.intent != null) {
941 if (cur.intent.equals(oh.intent)) {
942 matches.add(oh);
943 }
944 } else if (cur.title != null) {
945 if (cur.title.equals(oh.title)) {
946 matches.add(oh);
947 }
948 }
949 }
950 final int NM = matches.size();
951 if (NM == 1) {
952 return matches.get(0);
953 } else if (NM > 1) {
954 for (int j=0; j<NM; j++) {
955 Header oh = matches.get(j);
956 if (cur.fragmentArguments != null &&
957 cur.fragmentArguments.equals(oh.fragmentArguments)) {
958 return oh;
959 }
960 if (cur.extras != null && cur.extras.equals(oh.extras)) {
961 return oh;
962 }
963 if (cur.title != null && cur.title.equals(oh.title)) {
964 return oh;
965 }
966 }
967 }
968 return null;
969 }
970
971 /**
Andrew Stadleraa904f42010-09-02 14:50:08 -0700972 * Start a new fragment.
973 *
974 * @param fragment The fragment to start
975 * @param push If true, the current fragment will be pushed onto the back stack. If false,
976 * the current fragment will be replaced.
977 */
978 public void startPreferenceFragment(Fragment fragment, boolean push) {
979 FragmentTransaction transaction = getFragmentManager().openTransaction();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700980 startPreferenceFragment(fragment, transaction);
Andrew Stadleraa904f42010-09-02 14:50:08 -0700981 if (push) {
982 transaction.addToBackStack(BACK_STACK_PREFS);
983 }
984 transaction.commit();
985 }
986
Dianne Hackbornc6669ca2010-09-16 01:33:24 -0700987 /**
988 * Start a new fragment.
989 *
990 * @param fragment The fragment to start
991 * @param ft The FragmentTransaction in which to perform this operation.
992 * Will not be added to the back stack or committed for you; you use do that.
993 */
994 public void startPreferenceFragment(Fragment fragment, FragmentTransaction ft) {
995 ft.replace(com.android.internal.R.id.prefs, fragment);
996 }
997
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700998 @Override
999 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
Dianne Hackborndef15372010-08-15 12:43:52 -07001000 Fragment f = Fragment.instantiate(this, pref.getFragment(), pref.getExtras());
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07001001 FragmentTransaction transaction = getFragmentManager().openTransaction();
1002 startPreferenceFragment(f, transaction);
1003 transaction.setBreadCrumbTitle(pref.getTitle());
1004 transaction.addToBackStack(BACK_STACK_PREFS);
1005 transaction.commit();
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -07001006 return true;
1007 }
1008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 /**
1010 * Posts a message to bind the preferences to the list view.
1011 * <p>
1012 * Binding late is preferred as any custom preference types created in
1013 * {@link #onCreate(Bundle)} are able to have their views recycled.
1014 */
1015 private void postBindPreferences() {
1016 if (mHandler.hasMessages(MSG_BIND_PREFERENCES)) return;
1017 mHandler.obtainMessage(MSG_BIND_PREFERENCES).sendToTarget();
1018 }
Freeman Ng19ea2e02010-03-25 15:09:00 -07001019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 private void bindPreferences() {
1021 final PreferenceScreen preferenceScreen = getPreferenceScreen();
1022 if (preferenceScreen != null) {
1023 preferenceScreen.bind(getListView());
Adam Powelle7fea452010-03-18 14:51:39 -07001024 if (mSavedInstanceState != null) {
1025 super.onRestoreInstanceState(mSavedInstanceState);
1026 mSavedInstanceState = null;
1027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 }
1029 }
Freeman Ng19ea2e02010-03-25 15:09:00 -07001030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 * Returns the {@link PreferenceManager} used by this activity.
1033 * @return The {@link PreferenceManager}.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001034 *
1035 * @deprecated This function is not relevant for a modern fragment-based
1036 * PreferenceActivity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001038 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 public PreferenceManager getPreferenceManager() {
1040 return mPreferenceManager;
1041 }
Freeman Ng19ea2e02010-03-25 15:09:00 -07001042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 private void requirePreferenceManager() {
1044 if (mPreferenceManager == null) {
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001045 if (mAdapter == null) {
1046 throw new RuntimeException("This should be called after super.onCreate.");
1047 }
1048 throw new RuntimeException(
1049 "Modern two-pane PreferenceActivity requires use of a PreferenceFragment");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 }
1051 }
1052
1053 /**
1054 * Sets the root of the preference hierarchy that this activity is showing.
Freeman Ng19ea2e02010-03-25 15:09:00 -07001055 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 * @param preferenceScreen The root {@link PreferenceScreen} of the preference hierarchy.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001057 *
1058 * @deprecated This function is not relevant for a modern fragment-based
1059 * PreferenceActivity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 */
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001061 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001063 requirePreferenceManager();
1064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 if (mPreferenceManager.setPreferences(preferenceScreen) && preferenceScreen != null) {
1066 postBindPreferences();
1067 CharSequence title = getPreferenceScreen().getTitle();
1068 // Set the title of the activity
1069 if (title != null) {
1070 setTitle(title);
1071 }
1072 }
1073 }
Freeman Ng19ea2e02010-03-25 15:09:00 -07001074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 /**
1076 * Gets the root of the preference hierarchy that this activity is showing.
Freeman Ng19ea2e02010-03-25 15:09:00 -07001077 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 * @return The {@link PreferenceScreen} that is the root of the preference
1079 * hierarchy.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001080 *
1081 * @deprecated This function is not relevant for a modern fragment-based
1082 * PreferenceActivity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 */
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001084 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 public PreferenceScreen getPreferenceScreen() {
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001086 if (mPreferenceManager != null) {
1087 return mPreferenceManager.getPreferenceScreen();
1088 }
1089 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 }
Freeman Ng19ea2e02010-03-25 15:09:00 -07001091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 /**
1093 * Adds preferences from activities that match the given {@link Intent}.
Freeman Ng19ea2e02010-03-25 15:09:00 -07001094 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 * @param intent The {@link Intent} to query activities.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001096 *
1097 * @deprecated This function is not relevant for a modern fragment-based
1098 * PreferenceActivity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 */
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001100 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 public void addPreferencesFromIntent(Intent intent) {
1102 requirePreferenceManager();
Freeman Ng19ea2e02010-03-25 15:09:00 -07001103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 setPreferenceScreen(mPreferenceManager.inflateFromIntent(intent, getPreferenceScreen()));
1105 }
Freeman Ng19ea2e02010-03-25 15:09:00 -07001106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 /**
1108 * Inflates the given XML resource and adds the preference hierarchy to the current
1109 * preference hierarchy.
Freeman Ng19ea2e02010-03-25 15:09:00 -07001110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 * @param preferencesResId The XML resource ID to inflate.
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001112 *
1113 * @deprecated This function is not relevant for a modern fragment-based
1114 * PreferenceActivity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 */
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001116 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 public void addPreferencesFromResource(int preferencesResId) {
1118 requirePreferenceManager();
Freeman Ng19ea2e02010-03-25 15:09:00 -07001119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 setPreferenceScreen(mPreferenceManager.inflateFromResource(this, preferencesResId,
1121 getPreferenceScreen()));
1122 }
1123
1124 /**
1125 * {@inheritDoc}
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001126 *
1127 * @deprecated This function is not relevant for a modern fragment-based
1128 * PreferenceActivity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 */
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001130 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
1132 return false;
1133 }
Freeman Ng19ea2e02010-03-25 15:09:00 -07001134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 * Finds a {@link Preference} based on its key.
Freeman Ng19ea2e02010-03-25 15:09:00 -07001137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 * @param key The key of the preference to retrieve.
1139 * @return The {@link Preference} with the key, or null.
1140 * @see PreferenceGroup#findPreference(CharSequence)
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001141 *
1142 * @deprecated This function is not relevant for a modern fragment-based
1143 * PreferenceActivity.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 */
Dianne Hackbornb1ad5972010-08-02 17:30:33 -07001145 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 public Preference findPreference(CharSequence key) {
Freeman Ng19ea2e02010-03-25 15:09:00 -07001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 if (mPreferenceManager == null) {
1149 return null;
1150 }
Freeman Ng19ea2e02010-03-25 15:09:00 -07001151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 return mPreferenceManager.findPreference(key);
1153 }
1154
1155 @Override
1156 protected void onNewIntent(Intent intent) {
1157 if (mPreferenceManager != null) {
1158 mPreferenceManager.dispatchNewIntent(intent);
1159 }
1160 }
Freeman Ng19ea2e02010-03-25 15:09:00 -07001161
1162 // give subclasses access to the Next button
1163 /** @hide */
1164 protected boolean hasNextButton() {
1165 return mNextButton != null;
1166 }
1167 /** @hide */
1168 protected Button getNextButton() {
1169 return mNextButton;
1170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171}