1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.preference;
import android.annotation.UnsupportedAppUsage;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
* Represents a top-level {@link Preference} that
* is the root of a Preference hierarchy. A {@link PreferenceActivity}
* points to an instance of this class to show the preferences. To instantiate
* this class, use {@link PreferenceManager#createPreferenceScreen(Context)}.
* <ul>
* This class can appear in two places:
* <li> When a {@link PreferenceActivity} points to this, it is used as the root
* and is not shown (only the contained preferences are shown).
* <li> When it appears inside another preference hierarchy, it is shown and
* serves as the gateway to another screen of preferences (either by showing
* another screen of preferences as a {@link Dialog} or via a
* {@link Context#startActivity(android.content.Intent)} from the
* {@link Preference#getIntent()}). The children of this {@link PreferenceScreen}
* are NOT shown in the screen that this {@link PreferenceScreen} is shown in.
* Instead, a separate screen will be shown when this preference is clicked.
* </ul>
* <p>Here's an example XML layout of a PreferenceScreen:</p>
* <pre>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="first_preferencescreen">
<CheckBoxPreference
android:key="wifi enabled"
android:title="WiFi" />
<PreferenceScreen
android:key="second_preferencescreen"
android:title="WiFi settings">
<CheckBoxPreference
android:key="prefer wifi"
android:title="Prefer WiFi" />
... other preferences here ...
</PreferenceScreen>
</PreferenceScreen> </pre>
* <p>
* In this example, the "first_preferencescreen" will be used as the root of the
* hierarchy and given to a {@link PreferenceActivity}. The first screen will
* show preferences "WiFi" (which can be used to quickly enable/disable WiFi)
* and "WiFi settings". The "WiFi settings" is the "second_preferencescreen" and when
* clicked will show another screen of preferences such as "Prefer WiFi" (and
* the other preferences that are children of the "second_preferencescreen" tag).
*
* <div class="special reference">
* <h3>Developer Guides</h3>
* <p>For information about building a settings UI with Preferences,
* read the <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>
* guide.</p>
* </div>
*
* @see PreferenceCategory
*
* @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
* <a href="{@docRoot}reference/androidx/preference/package-summary.html">
* Preference Library</a> for consistent behavior across all devices. For more information on
* using the AndroidX Preference Library see
* <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
*/
@Deprecated
public final class PreferenceScreen extends PreferenceGroup implements AdapterView.OnItemClickListener,
DialogInterface.OnDismissListener {
@UnsupportedAppUsage
private ListAdapter mRootAdapter;
private Dialog mDialog;
@UnsupportedAppUsage
private ListView mListView;
private int mLayoutResId = com.android.internal.R.layout.preference_list_fragment;
private Drawable mDividerDrawable;
private boolean mDividerSpecified;
/**
* Do NOT use this constructor, use {@link PreferenceManager#createPreferenceScreen(Context)}.
* @hide-
*/
@UnsupportedAppUsage
public PreferenceScreen(Context context, AttributeSet attrs) {
super(context, attrs, com.android.internal.R.attr.preferenceScreenStyle);
TypedArray a = context.obtainStyledAttributes(null,
com.android.internal.R.styleable.PreferenceScreen,
com.android.internal.R.attr.preferenceScreenStyle,
0);
mLayoutResId = a.getResourceId(
com.android.internal.R.styleable.PreferenceScreen_screenLayout,
mLayoutResId);
if (a.hasValueOrEmpty(com.android.internal.R.styleable.PreferenceScreen_divider)) {
mDividerDrawable =
a.getDrawable(com.android.internal.R.styleable.PreferenceScreen_divider);
mDividerSpecified = true;
}
a.recycle();
}
/**
* Returns an adapter that can be attached to a {@link PreferenceActivity}
* or {@link PreferenceFragment} to show the preferences contained in this
* {@link PreferenceScreen}.
* <p>
* This {@link PreferenceScreen} will NOT appear in the returned adapter, instead
* it appears in the hierarchy above this {@link PreferenceScreen}.
* <p>
* This adapter's {@link Adapter#getItem(int)} should always return a
* subclass of {@link Preference}.
*
* @return An adapter that provides the {@link Preference} contained in this
* {@link PreferenceScreen}.
*/
public ListAdapter getRootAdapter() {
if (mRootAdapter == null) {
mRootAdapter = onCreateRootAdapter();
}
return mRootAdapter;
}
/**
* Creates the root adapter.
*
* @return An adapter that contains the preferences contained in this {@link PreferenceScreen}.
* @see #getRootAdapter()
*/
protected ListAdapter onCreateRootAdapter() {
return new PreferenceGroupAdapter(this);
}
/**
* Binds a {@link ListView} to the preferences contained in this {@link PreferenceScreen} via
* {@link #getRootAdapter()}. It also handles passing list item clicks to the corresponding
* {@link Preference} contained by this {@link PreferenceScreen}.
*
* @param listView The list view to attach to.
*/
public void bind(ListView listView) {
listView.setOnItemClickListener(this);
listView.setAdapter(getRootAdapter());
onAttachedToActivity();
}
@Override
protected void onClick() {
if (getIntent() != null || getFragment() != null || getPreferenceCount() == 0) {
return;
}
showDialog(null);
}
private void showDialog(Bundle state) {
Context context = getContext();
if (mListView != null) {
mListView.setAdapter(null);
}
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View childPrefScreen = inflater.inflate(mLayoutResId, null);
View titleView = childPrefScreen.findViewById(android.R.id.title);
mListView = (ListView) childPrefScreen.findViewById(android.R.id.list);
if (mDividerSpecified) {
mListView.setDivider(mDividerDrawable);
}
bind(mListView);
// Set the title bar if title is available, else no title bar
final CharSequence title = getTitle();
Dialog dialog = mDialog = new Dialog(context, context.getThemeResId());
if (TextUtils.isEmpty(title)) {
if (titleView != null) {
titleView.setVisibility(View.GONE);
}
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
} else {
if (titleView instanceof TextView) {
((TextView) titleView).setText(title);
titleView.setVisibility(View.VISIBLE);
} else {
dialog.setTitle(title);
}
}
dialog.setContentView(childPrefScreen);
dialog.setOnDismissListener(this);
if (state != null) {
dialog.onRestoreInstanceState(state);
}
// Add the screen to the list of preferences screens opened as dialogs
getPreferenceManager().addPreferencesScreen(dialog);
dialog.show();
}
public void onDismiss(DialogInterface dialog) {
mDialog = null;
getPreferenceManager().removePreferencesScreen(dialog);
}
/**
* Used to get a handle to the dialog.
* This is useful for cases where we want to manipulate the dialog
* as we would with any other activity or view.
*/
public Dialog getDialog() {
return mDialog;
}
public void onItemClick(AdapterView parent, View view, int position, long id) {
// If the list has headers, subtract them from the index.
if (parent instanceof ListView) {
position -= ((ListView) parent).getHeaderViewsCount();
}
Object item = getRootAdapter().getItem(position);
if (!(item instanceof Preference)) return;
final Preference preference = (Preference) item;
preference.performClick(this);
}
@Override
protected boolean isOnSameScreenAsChildren() {
return false;
}
@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
final Dialog dialog = mDialog;
if (dialog == null || !dialog.isShowing()) {
return superState;
}
final SavedState myState = new SavedState(superState);
myState.isDialogShowing = true;
myState.dialogBundle = dialog.onSaveInstanceState();
return myState;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state == null || !state.getClass().equals(SavedState.class)) {
// Didn't save state for us in onSaveInstanceState
super.onRestoreInstanceState(state);
return;
}
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
if (myState.isDialogShowing) {
showDialog(myState.dialogBundle);
}
}
private static class SavedState extends BaseSavedState {
boolean isDialogShowing;
Bundle dialogBundle;
public SavedState(Parcel source) {
super(source);
isDialogShowing = source.readInt() == 1;
dialogBundle = source.readBundle();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeInt(isDialogShowing ? 1 : 0);
dest.writeBundle(dialogBundle);
}
public SavedState(Parcelable superState) {
super(superState);
}
public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}
|