blob: dd00a53b4803ffda9f985ded82e31745666d4276 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.preference;
18
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010019import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.app.Dialog;
21import android.content.Context;
22import android.content.DialogInterface;
Michael Kwan744be162016-07-22 18:37:31 -070023import android.content.res.TypedArray;
24import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.os.Bundle;
26import android.os.Parcel;
27import android.os.Parcelable;
Amith Yamasanica74c902009-06-17 16:56:08 -070028import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.util.AttributeSet;
Amith Yamasanif9638a42012-04-30 16:32:39 -070030import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.view.View;
Amith Yamasani72e6bec2011-09-23 10:43:40 -070032import android.view.Window;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.widget.Adapter;
34import android.widget.AdapterView;
35import android.widget.ListAdapter;
36import android.widget.ListView;
Michael Kwan744be162016-07-22 18:37:31 -070037import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39/**
40 * Represents a top-level {@link Preference} that
41 * is the root of a Preference hierarchy. A {@link PreferenceActivity}
42 * points to an instance of this class to show the preferences. To instantiate
43 * this class, use {@link PreferenceManager#createPreferenceScreen(Context)}.
44 * <ul>
45 * This class can appear in two places:
46 * <li> When a {@link PreferenceActivity} points to this, it is used as the root
47 * and is not shown (only the contained preferences are shown).
48 * <li> When it appears inside another preference hierarchy, it is shown and
49 * serves as the gateway to another screen of preferences (either by showing
50 * another screen of preferences as a {@link Dialog} or via a
51 * {@link Context#startActivity(android.content.Intent)} from the
52 * {@link Preference#getIntent()}). The children of this {@link PreferenceScreen}
53 * are NOT shown in the screen that this {@link PreferenceScreen} is shown in.
54 * Instead, a separate screen will be shown when this preference is clicked.
55 * </ul>
56 * <p>Here's an example XML layout of a PreferenceScreen:</p>
57 * <pre>
58&lt;PreferenceScreen
59 xmlns:android="http://schemas.android.com/apk/res/android"
60 android:key="first_preferencescreen"&gt;
61 &lt;CheckBoxPreference
62 android:key="wifi enabled"
63 android:title="WiFi" /&gt;
64 &lt;PreferenceScreen
65 android:key="second_preferencescreen"
66 android:title="WiFi settings"&gt;
67 &lt;CheckBoxPreference
68 android:key="prefer wifi"
69 android:title="Prefer WiFi" /&gt;
70 ... other preferences here ...
71 &lt;/PreferenceScreen&gt;
72&lt;/PreferenceScreen&gt; </pre>
73 * <p>
74 * In this example, the "first_preferencescreen" will be used as the root of the
75 * hierarchy and given to a {@link PreferenceActivity}. The first screen will
76 * show preferences "WiFi" (which can be used to quickly enable/disable WiFi)
77 * and "WiFi settings". The "WiFi settings" is the "second_preferencescreen" and when
78 * clicked will show another screen of preferences such as "Prefer WiFi" (and
79 * the other preferences that are children of the "second_preferencescreen" tag).
80 *
Scott Maincdd0c592012-07-26 17:03:51 -070081 * <div class="special reference">
82 * <h3>Developer Guides</h3>
83 * <p>For information about building a settings UI with Preferences,
84 * read the <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>
85 * guide.</p>
86 * </div>
87 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 * @see PreferenceCategory
89 */
90public final class PreferenceScreen extends PreferenceGroup implements AdapterView.OnItemClickListener,
91 DialogInterface.OnDismissListener {
92
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010093 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 private ListAdapter mRootAdapter;
95
96 private Dialog mDialog;
Mathias Jeppsson15600032011-01-18 14:34:52 +010097
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010098 @UnsupportedAppUsage
Mathias Jeppsson15600032011-01-18 14:34:52 +010099 private ListView mListView;
Michael Kwan744be162016-07-22 18:37:31 -0700100
101 private int mLayoutResId = com.android.internal.R.layout.preference_list_fragment;
102 private Drawable mDividerDrawable;
103 private boolean mDividerSpecified;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 /**
106 * Do NOT use this constructor, use {@link PreferenceManager#createPreferenceScreen(Context)}.
107 * @hide-
108 */
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +0100109 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 public PreferenceScreen(Context context, AttributeSet attrs) {
111 super(context, attrs, com.android.internal.R.attr.preferenceScreenStyle);
Michael Kwan744be162016-07-22 18:37:31 -0700112
113 TypedArray a = context.obtainStyledAttributes(null,
114 com.android.internal.R.styleable.PreferenceScreen,
115 com.android.internal.R.attr.preferenceScreenStyle,
116 0);
117
118 mLayoutResId = a.getResourceId(
119 com.android.internal.R.styleable.PreferenceScreen_screenLayout,
120 mLayoutResId);
121 if (a.hasValueOrEmpty(com.android.internal.R.styleable.PreferenceScreen_divider)) {
122 mDividerDrawable =
123 a.getDrawable(com.android.internal.R.styleable.PreferenceScreen_divider);
124 mDividerSpecified = true;
125 }
126
127 a.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 }
129
130 /**
131 * Returns an adapter that can be attached to a {@link PreferenceActivity}
Daisuke Miyakawa52a50782010-08-13 13:20:04 -0700132 * or {@link PreferenceFragment} to show the preferences contained in this
133 * {@link PreferenceScreen}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * <p>
135 * This {@link PreferenceScreen} will NOT appear in the returned adapter, instead
136 * it appears in the hierarchy above this {@link PreferenceScreen}.
137 * <p>
138 * This adapter's {@link Adapter#getItem(int)} should always return a
139 * subclass of {@link Preference}.
140 *
141 * @return An adapter that provides the {@link Preference} contained in this
142 * {@link PreferenceScreen}.
143 */
144 public ListAdapter getRootAdapter() {
145 if (mRootAdapter == null) {
146 mRootAdapter = onCreateRootAdapter();
147 }
148
149 return mRootAdapter;
150 }
151
152 /**
153 * Creates the root adapter.
154 *
155 * @return An adapter that contains the preferences contained in this {@link PreferenceScreen}.
156 * @see #getRootAdapter()
157 */
158 protected ListAdapter onCreateRootAdapter() {
159 return new PreferenceGroupAdapter(this);
160 }
161
162 /**
163 * Binds a {@link ListView} to the preferences contained in this {@link PreferenceScreen} via
164 * {@link #getRootAdapter()}. It also handles passing list item clicks to the corresponding
165 * {@link Preference} contained by this {@link PreferenceScreen}.
166 *
167 * @param listView The list view to attach to.
168 */
169 public void bind(ListView listView) {
170 listView.setOnItemClickListener(this);
171 listView.setAdapter(getRootAdapter());
172
173 onAttachedToActivity();
174 }
175
176 @Override
177 protected void onClick() {
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700178 if (getIntent() != null || getFragment() != null || getPreferenceCount() == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 return;
180 }
181
182 showDialog(null);
183 }
184
185 private void showDialog(Bundle state) {
186 Context context = getContext();
Mathias Jeppsson15600032011-01-18 14:34:52 +0100187 if (mListView != null) {
188 mListView.setAdapter(null);
189 }
Amith Yamasanif9638a42012-04-30 16:32:39 -0700190
191 LayoutInflater inflater = (LayoutInflater)
192 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Michael Kwan744be162016-07-22 18:37:31 -0700193 View childPrefScreen = inflater.inflate(mLayoutResId, null);
194 View titleView = childPrefScreen.findViewById(android.R.id.title);
Amith Yamasanif9638a42012-04-30 16:32:39 -0700195 mListView = (ListView) childPrefScreen.findViewById(android.R.id.list);
Michael Kwan744be162016-07-22 18:37:31 -0700196 if (mDividerSpecified) {
197 mListView.setDivider(mDividerDrawable);
198 }
199
Mathias Jeppsson15600032011-01-18 14:34:52 +0100200 bind(mListView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201
Amith Yamasanica74c902009-06-17 16:56:08 -0700202 // Set the title bar if title is available, else no title bar
203 final CharSequence title = getTitle();
Amith Yamasani72e6bec2011-09-23 10:43:40 -0700204 Dialog dialog = mDialog = new Dialog(context, context.getThemeResId());
205 if (TextUtils.isEmpty(title)) {
Michael Kwan744be162016-07-22 18:37:31 -0700206 if (titleView != null) {
207 titleView.setVisibility(View.GONE);
208 }
Amith Yamasani72e6bec2011-09-23 10:43:40 -0700209 dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
210 } else {
Michael Kwan744be162016-07-22 18:37:31 -0700211 if (titleView instanceof TextView) {
212 ((TextView) titleView).setText(title);
213 titleView.setVisibility(View.VISIBLE);
214 } else {
215 dialog.setTitle(title);
216 }
Amith Yamasanica74c902009-06-17 16:56:08 -0700217 }
Amith Yamasanif9638a42012-04-30 16:32:39 -0700218 dialog.setContentView(childPrefScreen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 dialog.setOnDismissListener(this);
220 if (state != null) {
221 dialog.onRestoreInstanceState(state);
222 }
Amith Yamasanica74c902009-06-17 16:56:08 -0700223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 // Add the screen to the list of preferences screens opened as dialogs
225 getPreferenceManager().addPreferencesScreen(dialog);
226
227 dialog.show();
228 }
229
230 public void onDismiss(DialogInterface dialog) {
231 mDialog = null;
232 getPreferenceManager().removePreferencesScreen(dialog);
233 }
234
235 /**
236 * Used to get a handle to the dialog.
237 * This is useful for cases where we want to manipulate the dialog
238 * as we would with any other activity or view.
239 */
240 public Dialog getDialog() {
241 return mDialog;
242 }
243
244 public void onItemClick(AdapterView parent, View view, int position, long id) {
Amith Yamasani2d43d282011-09-29 15:16:16 -0700245 // If the list has headers, subtract them from the index.
246 if (parent instanceof ListView) {
247 position -= ((ListView) parent).getHeaderViewsCount();
248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 Object item = getRootAdapter().getItem(position);
250 if (!(item instanceof Preference)) return;
Amith Yamasani2d43d282011-09-29 15:16:16 -0700251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 final Preference preference = (Preference) item;
253 preference.performClick(this);
254 }
255
256 @Override
257 protected boolean isOnSameScreenAsChildren() {
258 return false;
259 }
260
261 @Override
262 protected Parcelable onSaveInstanceState() {
263 final Parcelable superState = super.onSaveInstanceState();
264 final Dialog dialog = mDialog;
265 if (dialog == null || !dialog.isShowing()) {
266 return superState;
267 }
268
269 final SavedState myState = new SavedState(superState);
270 myState.isDialogShowing = true;
271 myState.dialogBundle = dialog.onSaveInstanceState();
272 return myState;
273 }
274
275 @Override
276 protected void onRestoreInstanceState(Parcelable state) {
277 if (state == null || !state.getClass().equals(SavedState.class)) {
278 // Didn't save state for us in onSaveInstanceState
279 super.onRestoreInstanceState(state);
280 return;
281 }
282
283 SavedState myState = (SavedState) state;
284 super.onRestoreInstanceState(myState.getSuperState());
285 if (myState.isDialogShowing) {
286 showDialog(myState.dialogBundle);
287 }
288 }
289
290 private static class SavedState extends BaseSavedState {
291 boolean isDialogShowing;
292 Bundle dialogBundle;
293
294 public SavedState(Parcel source) {
295 super(source);
296 isDialogShowing = source.readInt() == 1;
297 dialogBundle = source.readBundle();
298 }
299
300 @Override
301 public void writeToParcel(Parcel dest, int flags) {
302 super.writeToParcel(dest, flags);
303 dest.writeInt(isDialogShowing ? 1 : 0);
304 dest.writeBundle(dialogBundle);
305 }
306
307 public SavedState(Parcelable superState) {
308 super(superState);
309 }
310
311 public static final Parcelable.Creator<SavedState> CREATOR =
312 new Parcelable.Creator<SavedState>() {
313 public SavedState createFromParcel(Parcel in) {
314 return new SavedState(in);
315 }
316
317 public SavedState[] newArray(int size) {
318 return new SavedState[size];
319 }
320 };
321 }
322
323}