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.text.TextUtils; 26 import android.util.AttributeSet; 27 import android.view.View; 28 import android.widget.Adapter; 29 import android.widget.AdapterView; 30 import android.widget.ListAdapter; 31 import 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 <PreferenceScreen 53 xmlns:android="http://schemas.android.com/apk/res/android" 54 android:key="first_preferencescreen"> 55 <CheckBoxPreference 56 android:key="wifi enabled" 57 android:title="WiFi" /> 58 <PreferenceScreen 59 android:key="second_preferencescreen" 60 android:title="WiFi settings"> 61 <CheckBoxPreference 62 android:key="prefer wifi" 63 android:title="Prefer WiFi" /> 64 ... other preferences here ... 65 </PreferenceScreen> 66 </PreferenceScreen> </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 */ 77 public 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 */ PreferenceScreen(Context context, AttributeSet attrs)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 */ getRootAdapter()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 */ onCreateRootAdapter()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 */ bind(ListView listView)130 public void bind(ListView listView) { 131 listView.setOnItemClickListener(this); 132 listView.setAdapter(getRootAdapter()); 133 134 onAttachedToActivity(); 135 } 136 137 @Override onClick()138 protected void onClick() { 139 if (getIntent() != null || getPreferenceCount() == 0) { 140 return; 141 } 142 143 showDialog(null); 144 } 145 showDialog(Bundle state)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 onDismiss(DialogInterface dialog)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 */ getDialog()181 public Dialog getDialog() { 182 return mDialog; 183 } 184 onItemClick(AdapterView parent, View view, int position, long id)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 isOnSameScreenAsChildren()194 protected boolean isOnSameScreenAsChildren() { 195 return false; 196 } 197 198 @Override onSaveInstanceState()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 onRestoreInstanceState(Parcelable state)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 SavedState(Parcel source)231 public SavedState(Parcel source) { 232 super(source); 233 isDialogShowing = source.readInt() == 1; 234 dialogBundle = source.readBundle(); 235 } 236 237 @Override writeToParcel(Parcel dest, int flags)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 SavedState(Parcelable superState)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