blob: a353dbc8af1b1c9e1342025c5192f76e54b80b35 [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 Inwoodf2217132018-08-17 13:41:55 +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
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +000089 *
90 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
91 * <a href="{@docRoot}reference/androidx/preference/package-summary.html">
92 * Preference Library</a> for consistent behavior across all devices. For more information on
93 * using the AndroidX Preference Library see
94 * <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 */
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +000096@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097public final class PreferenceScreen extends PreferenceGroup implements AdapterView.OnItemClickListener,
98 DialogInterface.OnDismissListener {
99
Mathew Inwoodf2217132018-08-17 13:41:55 +0100100 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 private ListAdapter mRootAdapter;
102
103 private Dialog mDialog;
Mathias Jeppsson15600032011-01-18 14:34:52 +0100104
Mathew Inwoodf2217132018-08-17 13:41:55 +0100105 @UnsupportedAppUsage
Mathias Jeppsson15600032011-01-18 14:34:52 +0100106 private ListView mListView;
Michael Kwan744be162016-07-22 18:37:31 -0700107
108 private int mLayoutResId = com.android.internal.R.layout.preference_list_fragment;
109 private Drawable mDividerDrawable;
110 private boolean mDividerSpecified;
111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 /**
113 * Do NOT use this constructor, use {@link PreferenceManager#createPreferenceScreen(Context)}.
114 * @hide-
115 */
Mathew Inwoodf2217132018-08-17 13:41:55 +0100116 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public PreferenceScreen(Context context, AttributeSet attrs) {
118 super(context, attrs, com.android.internal.R.attr.preferenceScreenStyle);
Michael Kwan744be162016-07-22 18:37:31 -0700119
120 TypedArray a = context.obtainStyledAttributes(null,
121 com.android.internal.R.styleable.PreferenceScreen,
122 com.android.internal.R.attr.preferenceScreenStyle,
123 0);
124
125 mLayoutResId = a.getResourceId(
126 com.android.internal.R.styleable.PreferenceScreen_screenLayout,
127 mLayoutResId);
128 if (a.hasValueOrEmpty(com.android.internal.R.styleable.PreferenceScreen_divider)) {
129 mDividerDrawable =
130 a.getDrawable(com.android.internal.R.styleable.PreferenceScreen_divider);
131 mDividerSpecified = true;
132 }
133
134 a.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
136
137 /**
138 * Returns an adapter that can be attached to a {@link PreferenceActivity}
Daisuke Miyakawa52a50782010-08-13 13:20:04 -0700139 * or {@link PreferenceFragment} to show the preferences contained in this
140 * {@link PreferenceScreen}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * <p>
142 * This {@link PreferenceScreen} will NOT appear in the returned adapter, instead
143 * it appears in the hierarchy above this {@link PreferenceScreen}.
144 * <p>
145 * This adapter's {@link Adapter#getItem(int)} should always return a
146 * subclass of {@link Preference}.
147 *
148 * @return An adapter that provides the {@link Preference} contained in this
149 * {@link PreferenceScreen}.
150 */
151 public ListAdapter getRootAdapter() {
152 if (mRootAdapter == null) {
153 mRootAdapter = onCreateRootAdapter();
154 }
155
156 return mRootAdapter;
157 }
158
159 /**
160 * Creates the root adapter.
161 *
162 * @return An adapter that contains the preferences contained in this {@link PreferenceScreen}.
163 * @see #getRootAdapter()
164 */
165 protected ListAdapter onCreateRootAdapter() {
166 return new PreferenceGroupAdapter(this);
167 }
168
169 /**
170 * Binds a {@link ListView} to the preferences contained in this {@link PreferenceScreen} via
171 * {@link #getRootAdapter()}. It also handles passing list item clicks to the corresponding
172 * {@link Preference} contained by this {@link PreferenceScreen}.
173 *
174 * @param listView The list view to attach to.
175 */
176 public void bind(ListView listView) {
177 listView.setOnItemClickListener(this);
178 listView.setAdapter(getRootAdapter());
179
180 onAttachedToActivity();
181 }
182
183 @Override
184 protected void onClick() {
Dianne Hackbornb3cf10f2010-08-03 13:07:11 -0700185 if (getIntent() != null || getFragment() != null || getPreferenceCount() == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 return;
187 }
188
189 showDialog(null);
190 }
191
192 private void showDialog(Bundle state) {
193 Context context = getContext();
Mathias Jeppsson15600032011-01-18 14:34:52 +0100194 if (mListView != null) {
195 mListView.setAdapter(null);
196 }
Amith Yamasanif9638a42012-04-30 16:32:39 -0700197
198 LayoutInflater inflater = (LayoutInflater)
199 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Michael Kwan744be162016-07-22 18:37:31 -0700200 View childPrefScreen = inflater.inflate(mLayoutResId, null);
201 View titleView = childPrefScreen.findViewById(android.R.id.title);
Amith Yamasanif9638a42012-04-30 16:32:39 -0700202 mListView = (ListView) childPrefScreen.findViewById(android.R.id.list);
Michael Kwan744be162016-07-22 18:37:31 -0700203 if (mDividerSpecified) {
204 mListView.setDivider(mDividerDrawable);
205 }
206
Mathias Jeppsson15600032011-01-18 14:34:52 +0100207 bind(mListView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
Amith Yamasanica74c902009-06-17 16:56:08 -0700209 // Set the title bar if title is available, else no title bar
210 final CharSequence title = getTitle();
Amith Yamasani72e6bec2011-09-23 10:43:40 -0700211 Dialog dialog = mDialog = new Dialog(context, context.getThemeResId());
212 if (TextUtils.isEmpty(title)) {
Michael Kwan744be162016-07-22 18:37:31 -0700213 if (titleView != null) {
214 titleView.setVisibility(View.GONE);
215 }
Amith Yamasani72e6bec2011-09-23 10:43:40 -0700216 dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
217 } else {
Michael Kwan744be162016-07-22 18:37:31 -0700218 if (titleView instanceof TextView) {
219 ((TextView) titleView).setText(title);
220 titleView.setVisibility(View.VISIBLE);
221 } else {
222 dialog.setTitle(title);
223 }
Amith Yamasanica74c902009-06-17 16:56:08 -0700224 }
Amith Yamasanif9638a42012-04-30 16:32:39 -0700225 dialog.setContentView(childPrefScreen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 dialog.setOnDismissListener(this);
227 if (state != null) {
228 dialog.onRestoreInstanceState(state);
229 }
Amith Yamasanica74c902009-06-17 16:56:08 -0700230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 // Add the screen to the list of preferences screens opened as dialogs
232 getPreferenceManager().addPreferencesScreen(dialog);
233
234 dialog.show();
235 }
236
237 public void onDismiss(DialogInterface dialog) {
238 mDialog = null;
239 getPreferenceManager().removePreferencesScreen(dialog);
240 }
241
242 /**
243 * Used to get a handle to the dialog.
244 * This is useful for cases where we want to manipulate the dialog
245 * as we would with any other activity or view.
246 */
247 public Dialog getDialog() {
248 return mDialog;
249 }
250
251 public void onItemClick(AdapterView parent, View view, int position, long id) {
Amith Yamasani2d43d282011-09-29 15:16:16 -0700252 // If the list has headers, subtract them from the index.
253 if (parent instanceof ListView) {
254 position -= ((ListView) parent).getHeaderViewsCount();
255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 Object item = getRootAdapter().getItem(position);
257 if (!(item instanceof Preference)) return;
Amith Yamasani2d43d282011-09-29 15:16:16 -0700258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 final Preference preference = (Preference) item;
260 preference.performClick(this);
261 }
262
263 @Override
264 protected boolean isOnSameScreenAsChildren() {
265 return false;
266 }
267
268 @Override
269 protected Parcelable onSaveInstanceState() {
270 final Parcelable superState = super.onSaveInstanceState();
271 final Dialog dialog = mDialog;
272 if (dialog == null || !dialog.isShowing()) {
273 return superState;
274 }
275
276 final SavedState myState = new SavedState(superState);
277 myState.isDialogShowing = true;
278 myState.dialogBundle = dialog.onSaveInstanceState();
279 return myState;
280 }
281
282 @Override
283 protected void onRestoreInstanceState(Parcelable state) {
284 if (state == null || !state.getClass().equals(SavedState.class)) {
285 // Didn't save state for us in onSaveInstanceState
286 super.onRestoreInstanceState(state);
287 return;
288 }
289
290 SavedState myState = (SavedState) state;
291 super.onRestoreInstanceState(myState.getSuperState());
292 if (myState.isDialogShowing) {
293 showDialog(myState.dialogBundle);
294 }
295 }
296
297 private static class SavedState extends BaseSavedState {
298 boolean isDialogShowing;
299 Bundle dialogBundle;
300
301 public SavedState(Parcel source) {
302 super(source);
303 isDialogShowing = source.readInt() == 1;
304 dialogBundle = source.readBundle();
305 }
306
307 @Override
308 public void writeToParcel(Parcel dest, int flags) {
309 super.writeToParcel(dest, flags);
310 dest.writeInt(isDialogShowing ? 1 : 0);
311 dest.writeBundle(dialogBundle);
312 }
313
314 public SavedState(Parcelable superState) {
315 super(superState);
316 }
317
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700318 public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 new Parcelable.Creator<SavedState>() {
320 public SavedState createFromParcel(Parcel in) {
321 return new SavedState(in);
322 }
323
324 public SavedState[] newArray(int size) {
325 return new SavedState[size];
326 }
327 };
328 }
329
330}