PreferenceScreen.java revision a013a98f93fd5ced58cf76dcc2bd6ab2ca95dd92
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
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;
25import android.text.TextUtils;
26import 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}
94     * to show the preferences contained in this {@link PreferenceScreen}.
95     * <p>
96     * This {@link PreferenceScreen} will NOT appear in the returned adapter, instead
97     * it appears in the hierarchy above this {@link PreferenceScreen}.
98     * <p>
99     * This adapter's {@link Adapter#getItem(int)} should always return a
100     * subclass of {@link Preference}.
101     *
102     * @return An adapter that provides the {@link Preference} contained in this
103     *         {@link PreferenceScreen}.
104     */
105    public ListAdapter getRootAdapter() {
106        if (mRootAdapter == null) {
107            mRootAdapter = onCreateRootAdapter();
108        }
109
110        return mRootAdapter;
111    }
112
113    /**
114     * Creates the root adapter.
115     *
116     * @return An adapter that contains the preferences contained in this {@link PreferenceScreen}.
117     * @see #getRootAdapter()
118     */
119    protected ListAdapter onCreateRootAdapter() {
120        return new PreferenceGroupAdapter(this);
121    }
122
123    /**
124     * Binds a {@link ListView} to the preferences contained in this {@link PreferenceScreen} via
125     * {@link #getRootAdapter()}. It also handles passing list item clicks to the corresponding
126     * {@link Preference} contained by this {@link PreferenceScreen}.
127     *
128     * @param listView The list view to attach to.
129     */
130    public void bind(ListView listView) {
131        listView.setOnItemClickListener(this);
132        listView.setAdapter(getRootAdapter());
133
134        onAttachedToActivity();
135    }
136
137    @Override
138    protected void onClick() {
139        if (getIntent() != null || getPreferenceCount() == 0) {
140            return;
141        }
142
143        showDialog(null);
144    }
145
146    private void showDialog(Bundle state) {
147        Context context = getContext();
148        ListView listView = new ListView(context);
149        bind(listView);
150
151        // Set the title bar if title is available, else no title bar
152        final CharSequence title = getTitle();
153        Dialog dialog = mDialog = new Dialog(context, TextUtils.isEmpty(title)
154                ? com.android.internal.R.style.Theme_NoTitleBar
155                : com.android.internal.R.style.Theme);
156        dialog.setContentView(listView);
157        if (!TextUtils.isEmpty(title)) {
158            dialog.setTitle(title);
159        }
160        dialog.setOnDismissListener(this);
161        if (state != null) {
162            dialog.onRestoreInstanceState(state);
163        }
164
165        // Add the screen to the list of preferences screens opened as dialogs
166        getPreferenceManager().addPreferencesScreen(dialog);
167
168        dialog.show();
169    }
170
171    public void onDismiss(DialogInterface dialog) {
172        mDialog = null;
173        getPreferenceManager().removePreferencesScreen(dialog);
174    }
175
176    /**
177     * Used to get a handle to the dialog.
178     * This is useful for cases where we want to manipulate the dialog
179     * as we would with any other activity or view.
180     */
181    public Dialog getDialog() {
182        return mDialog;
183    }
184
185    public void onItemClick(AdapterView parent, View view, int position, long id) {
186        Object item = getRootAdapter().getItem(position);
187        if (!(item instanceof Preference)) return;
188
189        final Preference preference = (Preference) item;
190        preference.performClick(this);
191    }
192
193    @Override
194    protected boolean isOnSameScreenAsChildren() {
195        return false;
196    }
197
198    @Override
199    protected Parcelable onSaveInstanceState() {
200        final Parcelable superState = super.onSaveInstanceState();
201        final Dialog dialog = mDialog;
202        if (dialog == null || !dialog.isShowing()) {
203            return superState;
204        }
205
206        final SavedState myState = new SavedState(superState);
207        myState.isDialogShowing = true;
208        myState.dialogBundle = dialog.onSaveInstanceState();
209        return myState;
210    }
211
212    @Override
213    protected void onRestoreInstanceState(Parcelable state) {
214        if (state == null || !state.getClass().equals(SavedState.class)) {
215            // Didn't save state for us in onSaveInstanceState
216            super.onRestoreInstanceState(state);
217            return;
218        }
219
220        SavedState myState = (SavedState) state;
221        super.onRestoreInstanceState(myState.getSuperState());
222        if (myState.isDialogShowing) {
223            showDialog(myState.dialogBundle);
224        }
225    }
226
227    private static class SavedState extends BaseSavedState {
228        boolean isDialogShowing;
229        Bundle dialogBundle;
230
231        public SavedState(Parcel source) {
232            super(source);
233            isDialogShowing = source.readInt() == 1;
234            dialogBundle = source.readBundle();
235        }
236
237        @Override
238        public void writeToParcel(Parcel dest, int flags) {
239            super.writeToParcel(dest, flags);
240            dest.writeInt(isDialogShowing ? 1 : 0);
241            dest.writeBundle(dialogBundle);
242        }
243
244        public SavedState(Parcelable superState) {
245            super(superState);
246        }
247
248        public static final Parcelable.Creator<SavedState> CREATOR =
249                new Parcelable.Creator<SavedState>() {
250            public SavedState createFromParcel(Parcel in) {
251                return new SavedState(in);
252            }
253
254            public SavedState[] newArray(int size) {
255                return new SavedState[size];
256            }
257        };
258    }
259
260}
261