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