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