• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.support.v7.preference;
18 
19 import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20 
21 import android.content.Context;
22 import android.support.annotation.RestrictTo;
23 import android.support.v4.content.res.TypedArrayUtils;
24 import android.util.AttributeSet;
25 
26 /**
27  * Represents a top-level {@link Preference} that
28  * is the root of a Preference hierarchy. A {@link PreferenceFragmentCompat}
29  * points to an instance of this class to show the preferences. To instantiate
30  * this class, use {@link PreferenceManager#createPreferenceScreen(android.content.Context)}.
31  * <ul>
32  * This class can appear in two places:
33  * <li> When a {@link PreferenceFragmentCompat} points to this, it is used as the root
34  * and is not shown (only the contained preferences are shown).
35  * <li> When it appears inside another preference hierarchy, it is shown and
36  * serves as the gateway to another screen of preferences (either by showing
37  * another screen of preferences as a {@link android.app.Dialog} or via a
38  * {@link android.content.Context#startActivity(android.content.Intent)} from the
39  * {@link Preference#getIntent()}). The children of this {@link PreferenceScreen}
40  * are NOT shown in the screen that this {@link PreferenceScreen} is shown in.
41  * Instead, a separate screen will be shown when this preference is clicked.
42  * </ul>
43  * <p>Here's an example XML layout of a PreferenceScreen:</p>
44  * <pre>
45  &lt;PreferenceScreen
46  xmlns:android="http://schemas.android.com/apk/res/android"
47  android:key="first_preferencescreen"&gt;
48  &lt;CheckBoxPreference
49  android:key="wifi enabled"
50  android:title="WiFi" /&gt;
51  &lt;PreferenceScreen
52  android:key="second_preferencescreen"
53  android:title="WiFi settings"&gt;
54  &lt;CheckBoxPreference
55  android:key="prefer wifi"
56  android:title="Prefer WiFi" /&gt;
57  ... other preferences here ...
58  &lt;/PreferenceScreen&gt;
59  &lt;/PreferenceScreen&gt; </pre>
60  * <p>
61  * In this example, the "first_preferencescreen" will be used as the root of the
62  * hierarchy and given to a {@link android.support.v14.preference.PreferenceFragment}
63  * or {@link PreferenceFragmentCompat}.
64  * The first screen will
65  * show preferences "WiFi" (which can be used to quickly enable/disable WiFi)
66  * and "WiFi settings". The "WiFi settings" is the "second_preferencescreen" and when
67  * clicked will show another screen of preferences such as "Prefer WiFi" (and
68  * the other preferences that are children of the "second_preferencescreen" tag).
69  *
70  * <div class="special reference">
71  * <h3>Developer Guides</h3>
72  * <p>For information about building a settings UI with Preferences,
73  * read the <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>
74  * guide.</p>
75  * </div>
76  *
77  * @see PreferenceCategory
78  */
79 public final class PreferenceScreen extends PreferenceGroup  {
80 
81     private boolean mShouldUseGeneratedIds = true;
82 
83     /**
84      * Do NOT use this constructor, use {@link PreferenceManager#createPreferenceScreen(Context)}.
85      * @hide
86      */
87     @RestrictTo(LIBRARY_GROUP)
PreferenceScreen(Context context, AttributeSet attrs)88     public PreferenceScreen(Context context, AttributeSet attrs) {
89         super(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceScreenStyle,
90                 android.R.attr.preferenceScreenStyle));
91     }
92 
93     @Override
onClick()94     protected void onClick() {
95         if (getIntent() != null || getFragment() != null || getPreferenceCount() == 0) {
96             return;
97         }
98         final PreferenceManager.OnNavigateToScreenListener listener =
99                 getPreferenceManager().getOnNavigateToScreenListener();
100         if (listener != null) {
101             listener.onNavigateToScreen(this);
102         }
103     }
104 
105     @Override
isOnSameScreenAsChildren()106     protected boolean isOnSameScreenAsChildren() {
107         return false;
108     }
109 
110     /**
111      * See {@link #setShouldUseGeneratedIds(boolean)}
112      * @return {@code true} if the adapter should use the preference IDs generated by
113      *         {@link PreferenceGroup#addPreference(Preference)} as stable item IDs
114      */
shouldUseGeneratedIds()115     public boolean shouldUseGeneratedIds() {
116         return mShouldUseGeneratedIds;
117     }
118 
119     /**
120      * Set whether the adapter created for this screen should attempt to use the preference IDs
121      * generated by {@link PreferenceGroup#addPreference(Preference)} as stable item IDs. Setting
122      * this to false can suppress unwanted animations if {@link Preference} objects are frequently
123      * removed from and re-added to their containing {@link PreferenceGroup}.
124      * <p>
125      * This method may only be called when the preference screen is not attached to the hierarchy.
126      * <p>
127      * Default value is {@code true}.
128      *
129      * @param shouldUseGeneratedIds {@code true} if the adapter should use the preference ID as a
130      *                                          stable ID, or {@code false} to disable the use of
131      *                                          stable IDs
132      */
setShouldUseGeneratedIds(boolean shouldUseGeneratedIds)133     public void setShouldUseGeneratedIds(boolean shouldUseGeneratedIds) {
134         if (isAttached()) {
135             throw new IllegalStateException("Cannot change the usage of generated IDs while" +
136                     " attached to the preference hierarchy");
137         }
138         mShouldUseGeneratedIds = shouldUseGeneratedIds;
139     }
140 }
141