Alan Viverette | 3da604b | 2020-06-10 18:34:39 +0000 | [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.compat.annotation.UnsupportedAppUsage; |
| 21 | import android.content.Context; |
| 22 | import android.content.DialogInterface; |
| 23 | import android.content.res.TypedArray; |
| 24 | import android.graphics.drawable.Drawable; |
| 25 | import android.os.Bundle; |
| 26 | import android.os.Parcel; |
| 27 | import android.os.Parcelable; |
| 28 | import android.text.TextUtils; |
| 29 | import android.util.AttributeSet; |
| 30 | import android.view.LayoutInflater; |
| 31 | import android.view.View; |
| 32 | import android.view.Window; |
| 33 | import android.widget.Adapter; |
| 34 | import android.widget.AdapterView; |
| 35 | import android.widget.ListAdapter; |
| 36 | import android.widget.ListView; |
| 37 | import android.widget.TextView; |
| 38 | |
| 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 | <PreferenceScreen |
| 59 | xmlns:android="http://schemas.android.com/apk/res/android" |
| 60 | android:key="first_preferencescreen"> |
| 61 | <CheckBoxPreference |
| 62 | android:key="wifi enabled" |
| 63 | android:title="WiFi" /> |
| 64 | <PreferenceScreen |
| 65 | android:key="second_preferencescreen" |
| 66 | android:title="WiFi settings"> |
| 67 | <CheckBoxPreference |
| 68 | android:key="prefer wifi" |
| 69 | android:title="Prefer WiFi" /> |
| 70 | ... other preferences here ... |
| 71 | </PreferenceScreen> |
| 72 | </PreferenceScreen> </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 | * |
| 81 | * <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 | * |
| 88 | * @see PreferenceCategory |
| 89 | * |
| 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>. |
| 95 | */ |
| 96 | @Deprecated |
| 97 | public final class PreferenceScreen extends PreferenceGroup implements AdapterView.OnItemClickListener, |
| 98 | DialogInterface.OnDismissListener { |
| 99 | |
| 100 | @UnsupportedAppUsage |
| 101 | private ListAdapter mRootAdapter; |
| 102 | |
| 103 | private Dialog mDialog; |
| 104 | |
| 105 | @UnsupportedAppUsage |
| 106 | private ListView mListView; |
| 107 | |
| 108 | private int mLayoutResId = com.android.internal.R.layout.preference_list_fragment; |
| 109 | private Drawable mDividerDrawable; |
| 110 | private boolean mDividerSpecified; |
| 111 | |
| 112 | /** |
| 113 | * Do NOT use this constructor, use {@link PreferenceManager#createPreferenceScreen(Context)}. |
| 114 | * @hide- |
| 115 | */ |
| 116 | @UnsupportedAppUsage |
| 117 | public PreferenceScreen(Context context, AttributeSet attrs) { |
| 118 | super(context, attrs, com.android.internal.R.attr.preferenceScreenStyle); |
| 119 | |
| 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(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns an adapter that can be attached to a {@link PreferenceActivity} |
| 139 | * or {@link PreferenceFragment} to show the preferences contained in this |
| 140 | * {@link PreferenceScreen}. |
| 141 | * <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() { |
| 185 | if (getIntent() != null || getFragment() != null || getPreferenceCount() == 0) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | showDialog(null); |
| 190 | } |
| 191 | |
| 192 | private void showDialog(Bundle state) { |
| 193 | Context context = getContext(); |
| 194 | if (mListView != null) { |
| 195 | mListView.setAdapter(null); |
| 196 | } |
| 197 | |
| 198 | LayoutInflater inflater = (LayoutInflater) |
| 199 | context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 200 | View childPrefScreen = inflater.inflate(mLayoutResId, null); |
| 201 | View titleView = childPrefScreen.findViewById(android.R.id.title); |
| 202 | mListView = (ListView) childPrefScreen.findViewById(android.R.id.list); |
| 203 | if (mDividerSpecified) { |
| 204 | mListView.setDivider(mDividerDrawable); |
| 205 | } |
| 206 | |
| 207 | bind(mListView); |
| 208 | |
| 209 | // Set the title bar if title is available, else no title bar |
| 210 | final CharSequence title = getTitle(); |
| 211 | Dialog dialog = mDialog = new Dialog(context, context.getThemeResId()); |
| 212 | if (TextUtils.isEmpty(title)) { |
| 213 | if (titleView != null) { |
| 214 | titleView.setVisibility(View.GONE); |
| 215 | } |
| 216 | dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); |
| 217 | } else { |
| 218 | if (titleView instanceof TextView) { |
| 219 | ((TextView) titleView).setText(title); |
| 220 | titleView.setVisibility(View.VISIBLE); |
| 221 | } else { |
| 222 | dialog.setTitle(title); |
| 223 | } |
| 224 | } |
| 225 | dialog.setContentView(childPrefScreen); |
| 226 | dialog.setOnDismissListener(this); |
| 227 | if (state != null) { |
| 228 | dialog.onRestoreInstanceState(state); |
| 229 | } |
| 230 | |
| 231 | // 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) { |
| 252 | // If the list has headers, subtract them from the index. |
| 253 | if (parent instanceof ListView) { |
| 254 | position -= ((ListView) parent).getHeaderViewsCount(); |
| 255 | } |
| 256 | Object item = getRootAdapter().getItem(position); |
| 257 | if (!(item instanceof Preference)) return; |
| 258 | |
| 259 | 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 | |
| 318 | public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR = |
| 319 | 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 | } |