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