blob: c7f8ab2e5e262b86248e557c80366861d71b564c [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
19import android.app.Dialog;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.os.Bundle;
23import android.os.Parcel;
24import android.os.Parcelable;
Amith Yamasanica74c902009-06-17 16:56:08 -070025import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.util.AttributeSet;
27import android.view.View;
28import android.widget.Adapter;
29import android.widget.AdapterView;
30import android.widget.ListAdapter;
31import android.widget.ListView;
32
33/**
34 * Represents a top-level {@link Preference} that
35 * is the root of a Preference hierarchy. A {@link PreferenceActivity}
36 * points to an instance of this class to show the preferences. To instantiate
37 * this class, use {@link PreferenceManager#createPreferenceScreen(Context)}.
38 * <ul>
39 * This class can appear in two places:
40 * <li> When a {@link PreferenceActivity} points to this, it is used as the root
41 * and is not shown (only the contained preferences are shown).
42 * <li> When it appears inside another preference hierarchy, it is shown and
43 * serves as the gateway to another screen of preferences (either by showing
44 * another screen of preferences as a {@link Dialog} or via a
45 * {@link Context#startActivity(android.content.Intent)} from the
46 * {@link Preference#getIntent()}). The children of this {@link PreferenceScreen}
47 * are NOT shown in the screen that this {@link PreferenceScreen} is shown in.
48 * Instead, a separate screen will be shown when this preference is clicked.
49 * </ul>
50 * <p>Here's an example XML layout of a PreferenceScreen:</p>
51 * <pre>
52&lt;PreferenceScreen
53 xmlns:android="http://schemas.android.com/apk/res/android"
54 android:key="first_preferencescreen"&gt;
55 &lt;CheckBoxPreference
56 android:key="wifi enabled"
57 android:title="WiFi" /&gt;
58 &lt;PreferenceScreen
59 android:key="second_preferencescreen"
60 android:title="WiFi settings"&gt;
61 &lt;CheckBoxPreference
62 android:key="prefer wifi"
63 android:title="Prefer WiFi" /&gt;
64 ... other preferences here ...
65 &lt;/PreferenceScreen&gt;
66&lt;/PreferenceScreen&gt; </pre>
67 * <p>
68 * In this example, the "first_preferencescreen" will be used as the root of the
69 * hierarchy and given to a {@link PreferenceActivity}. The first screen will
70 * show preferences "WiFi" (which can be used to quickly enable/disable WiFi)
71 * and "WiFi settings". The "WiFi settings" is the "second_preferencescreen" and when
72 * clicked will show another screen of preferences such as "Prefer WiFi" (and
73 * the other preferences that are children of the "second_preferencescreen" tag).
74 *
75 * @see PreferenceCategory
76 */
77public final class PreferenceScreen extends PreferenceGroup implements AdapterView.OnItemClickListener,
78 DialogInterface.OnDismissListener {
79
80 private ListAdapter mRootAdapter;
81
82 private Dialog mDialog;
83
84 /**
85 * Do NOT use this constructor, use {@link PreferenceManager#createPreferenceScreen(Context)}.
86 * @hide-
87 */
88 public PreferenceScreen(Context context, AttributeSet attrs) {
89 super(context, attrs, com.android.internal.R.attr.preferenceScreenStyle);
90 }
91
92 /**
93 * Returns an adapter that can be attached to a {@link PreferenceActivity}
Daisuke Miyakawa52a50782010-08-13 13:20:04 -070094 * or {@link PreferenceFragment} to show the preferences contained in this
95 * {@link PreferenceScreen}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * <p>
97 * This {@link PreferenceScreen} will NOT appear in the returned adapter, instead
98 * it appears in the hierarchy above this {@link PreferenceScreen}.
99 * <p>
100 * This adapter's {@link Adapter#getItem(int)} should always return a
101 * subclass of {@link Preference}.
102 *
103 * @return An adapter that provides the {@link Preference} contained in this
104 * {@link PreferenceScreen}.
105 */
106 public ListAdapter getRootAdapter() {
107 if (mRootAdapter == null) {
108 mRootAdapter = onCreateRootAdapter();
109 }
110
111 return mRootAdapter;
112 }
113
114 /**
115 * Creates the root adapter.
116 *
117 * @return An adapter that contains the preferences contained in this {@link PreferenceScreen}.
118 * @see #getRootAdapter()
119 */
120 protected ListAdapter onCreateRootAdapter() {
121 return new PreferenceGroupAdapter(this);
122 }
123
124 /**
125 * Binds a {@link ListView} to the preferences contained in this {@link PreferenceScreen} via
126 * {@link #getRootAdapter()}. It also handles passing list item clicks to the corresponding
127 * {@link Preference} contained by this {@link PreferenceScreen}.
128 *
129 * @param listView The list view to attach to.
130 */
131 public void bind(ListView listView) {
132 listView.setOnItemClickListener(this);
133 listView.setAdapter(getRootAdapter());
134
135 onAttachedToActivity();
136 }
137
138 @Override
139 protected void onClick() {
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700140 if (getIntent() != null || getFragment() != null || getPreferenceCount() == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 return;
142 }
143
144 showDialog(null);
145 }
146
147 private void showDialog(Bundle state) {
148 Context context = getContext();
149 ListView listView = new ListView(context);
150 bind(listView);
151
Amith Yamasanica74c902009-06-17 16:56:08 -0700152 // Set the title bar if title is available, else no title bar
153 final CharSequence title = getTitle();
Amith Yamasania013a982009-06-26 16:03:46 -0700154 Dialog dialog = mDialog = new Dialog(context, TextUtils.isEmpty(title)
Amith Yamasanica74c902009-06-17 16:56:08 -0700155 ? com.android.internal.R.style.Theme_NoTitleBar
156 : com.android.internal.R.style.Theme);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 dialog.setContentView(listView);
Amith Yamasanica74c902009-06-17 16:56:08 -0700158 if (!TextUtils.isEmpty(title)) {
159 dialog.setTitle(title);
160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 dialog.setOnDismissListener(this);
162 if (state != null) {
163 dialog.onRestoreInstanceState(state);
164 }
Amith Yamasanica74c902009-06-17 16:56:08 -0700165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 // Add the screen to the list of preferences screens opened as dialogs
167 getPreferenceManager().addPreferencesScreen(dialog);
168
169 dialog.show();
170 }
171
172 public void onDismiss(DialogInterface dialog) {
173 mDialog = null;
174 getPreferenceManager().removePreferencesScreen(dialog);
175 }
176
177 /**
178 * Used to get a handle to the dialog.
179 * This is useful for cases where we want to manipulate the dialog
180 * as we would with any other activity or view.
181 */
182 public Dialog getDialog() {
183 return mDialog;
184 }
185
186 public void onItemClick(AdapterView parent, View view, int position, long id) {
187 Object item = getRootAdapter().getItem(position);
188 if (!(item instanceof Preference)) return;
189
190 final Preference preference = (Preference) item;
191 preference.performClick(this);
192 }
193
194 @Override
195 protected boolean isOnSameScreenAsChildren() {
196 return false;
197 }
198
199 @Override
200 protected Parcelable onSaveInstanceState() {
201 final Parcelable superState = super.onSaveInstanceState();
202 final Dialog dialog = mDialog;
203 if (dialog == null || !dialog.isShowing()) {
204 return superState;
205 }
206
207 final SavedState myState = new SavedState(superState);
208 myState.isDialogShowing = true;
209 myState.dialogBundle = dialog.onSaveInstanceState();
210 return myState;
211 }
212
213 @Override
214 protected void onRestoreInstanceState(Parcelable state) {
215 if (state == null || !state.getClass().equals(SavedState.class)) {
216 // Didn't save state for us in onSaveInstanceState
217 super.onRestoreInstanceState(state);
218 return;
219 }
220
221 SavedState myState = (SavedState) state;
222 super.onRestoreInstanceState(myState.getSuperState());
223 if (myState.isDialogShowing) {
224 showDialog(myState.dialogBundle);
225 }
226 }
227
228 private static class SavedState extends BaseSavedState {
229 boolean isDialogShowing;
230 Bundle dialogBundle;
231
232 public SavedState(Parcel source) {
233 super(source);
234 isDialogShowing = source.readInt() == 1;
235 dialogBundle = source.readBundle();
236 }
237
238 @Override
239 public void writeToParcel(Parcel dest, int flags) {
240 super.writeToParcel(dest, flags);
241 dest.writeInt(isDialogShowing ? 1 : 0);
242 dest.writeBundle(dialogBundle);
243 }
244
245 public SavedState(Parcelable superState) {
246 super(superState);
247 }
248
249 public static final Parcelable.Creator<SavedState> CREATOR =
250 new Parcelable.Creator<SavedState>() {
251 public SavedState createFromParcel(Parcel in) {
252 return new SavedState(in);
253 }
254
255 public SavedState[] newArray(int size) {
256 return new SavedState[size];
257 }
258 };
259 }
260
261}