• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 androidx.preference;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.drawable.Drawable;
22 import android.util.AttributeSet;
23 import android.view.View;
24 
25 import androidx.core.content.ContextCompat;
26 import androidx.core.content.res.TypedArrayUtils;
27 
28 /**
29  * A base class for {@link Preference} objects that are
30  * dialog-based. These preferences will, when clicked, open a dialog showing the
31  * actual preference controls.
32  *
33  * @attr name android:dialogTitle
34  * @attr name android:dialogMessage
35  * @attr name android:dialogIcon
36  * @attr name android:dialogLayout
37  * @attr name android:positiveButtonText
38  * @attr name android:negativeButtonText
39  */
40 public abstract class DialogPreference extends Preference {
41 
42     public interface TargetFragment {
findPreference(CharSequence key)43         Preference findPreference(CharSequence key);
44     }
45 
46     private CharSequence mDialogTitle;
47     private CharSequence mDialogMessage;
48     private Drawable mDialogIcon;
49     private CharSequence mPositiveButtonText;
50     private CharSequence mNegativeButtonText;
51     private int mDialogLayoutResId;
52 
DialogPreference( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)53     public DialogPreference(
54             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
55         super(context, attrs, defStyleAttr, defStyleRes);
56 
57         final TypedArray a = context.obtainStyledAttributes(attrs,
58                 R.styleable.DialogPreference, defStyleAttr, defStyleRes);
59 
60         mDialogTitle = TypedArrayUtils.getString(a, R.styleable.DialogPreference_dialogTitle,
61                 R.styleable.DialogPreference_android_dialogTitle);
62         if (mDialogTitle == null) {
63             // Fall back on the regular title of the preference
64             // (the one that is seen in the list)
65             mDialogTitle = getTitle();
66         }
67 
68         mDialogMessage = TypedArrayUtils.getString(a, R.styleable.DialogPreference_dialogMessage,
69                 R.styleable.DialogPreference_android_dialogMessage);
70 
71         mDialogIcon = TypedArrayUtils.getDrawable(a, R.styleable.DialogPreference_dialogIcon,
72                 R.styleable.DialogPreference_android_dialogIcon);
73 
74         mPositiveButtonText = TypedArrayUtils.getString(a,
75                 R.styleable.DialogPreference_positiveButtonText,
76                 R.styleable.DialogPreference_android_positiveButtonText);
77 
78         mNegativeButtonText = TypedArrayUtils.getString(a,
79                 R.styleable.DialogPreference_negativeButtonText,
80                 R.styleable.DialogPreference_android_negativeButtonText);
81 
82         mDialogLayoutResId = TypedArrayUtils.getResourceId(a,
83                 R.styleable.DialogPreference_dialogLayout,
84                 R.styleable.DialogPreference_android_dialogLayout, 0);
85 
86         a.recycle();
87     }
88 
DialogPreference(Context context, AttributeSet attrs, int defStyleAttr)89     public DialogPreference(Context context, AttributeSet attrs, int defStyleAttr) {
90         this(context, attrs, defStyleAttr, 0);
91     }
92 
DialogPreference(Context context, AttributeSet attrs)93     public DialogPreference(Context context, AttributeSet attrs) {
94         this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.dialogPreferenceStyle,
95                 android.R.attr.dialogPreferenceStyle));
96     }
97 
DialogPreference(Context context)98     public DialogPreference(Context context) {
99         this(context, null);
100     }
101 
102     /**
103      * Sets the title of the dialog. This will be shown on subsequent dialogs.
104      *
105      * @param dialogTitle The title.
106      */
setDialogTitle(CharSequence dialogTitle)107     public void setDialogTitle(CharSequence dialogTitle) {
108         mDialogTitle = dialogTitle;
109     }
110 
111     /**
112      * @see #setDialogTitle(CharSequence)
113      * @param dialogTitleResId The dialog title as a resource.
114      */
setDialogTitle(int dialogTitleResId)115     public void setDialogTitle(int dialogTitleResId) {
116         setDialogTitle(getContext().getString(dialogTitleResId));
117     }
118 
119     /**
120      * Returns the title to be shown on subsequent dialogs.
121      * @return The title.
122      */
getDialogTitle()123     public CharSequence getDialogTitle() {
124         return mDialogTitle;
125     }
126 
127     /**
128      * Sets the message of the dialog. This will be shown on subsequent dialogs.
129      * <p>
130      * This message forms the content View of the dialog and conflicts with
131      * list-based dialogs, for example. If setting a custom View on a dialog via
132      * {@link #setDialogLayoutResource(int)}, include a text View with ID
133      * {@link android.R.id#message} and it will be populated with this message.
134      *
135      * @param dialogMessage The message.
136      */
setDialogMessage(CharSequence dialogMessage)137     public void setDialogMessage(CharSequence dialogMessage) {
138         mDialogMessage = dialogMessage;
139     }
140 
141     /**
142      * @see #setDialogMessage(CharSequence)
143      * @param dialogMessageResId The dialog message as a resource.
144      */
setDialogMessage(int dialogMessageResId)145     public void setDialogMessage(int dialogMessageResId) {
146         setDialogMessage(getContext().getString(dialogMessageResId));
147     }
148 
149     /**
150      * Returns the message to be shown on subsequent dialogs.
151      * @return The message.
152      */
getDialogMessage()153     public CharSequence getDialogMessage() {
154         return mDialogMessage;
155     }
156 
157     /**
158      * Sets the icon of the dialog. This will be shown on subsequent dialogs.
159      *
160      * @param dialogIcon The icon, as a {@link Drawable}.
161      */
setDialogIcon(Drawable dialogIcon)162     public void setDialogIcon(Drawable dialogIcon) {
163         mDialogIcon = dialogIcon;
164     }
165 
166     /**
167      * Sets the icon (resource ID) of the dialog. This will be shown on
168      * subsequent dialogs.
169      *
170      * @param dialogIconRes The icon, as a resource ID.
171      */
setDialogIcon(int dialogIconRes)172     public void setDialogIcon(int dialogIconRes) {
173         mDialogIcon = ContextCompat.getDrawable(getContext(), dialogIconRes);
174     }
175 
176     /**
177      * Returns the icon to be shown on subsequent dialogs.
178      * @return The icon, as a {@link Drawable}.
179      */
getDialogIcon()180     public Drawable getDialogIcon() {
181         return mDialogIcon;
182     }
183 
184     /**
185      * Sets the text of the positive button of the dialog. This will be shown on
186      * subsequent dialogs.
187      *
188      * @param positiveButtonText The text of the positive button.
189      */
setPositiveButtonText(CharSequence positiveButtonText)190     public void setPositiveButtonText(CharSequence positiveButtonText) {
191         mPositiveButtonText = positiveButtonText;
192     }
193 
194     /**
195      * @see #setPositiveButtonText(CharSequence)
196      * @param positiveButtonTextResId The positive button text as a resource.
197      */
setPositiveButtonText(int positiveButtonTextResId)198     public void setPositiveButtonText(int positiveButtonTextResId) {
199         setPositiveButtonText(getContext().getString(positiveButtonTextResId));
200     }
201 
202     /**
203      * Returns the text of the positive button to be shown on subsequent
204      * dialogs.
205      *
206      * @return The text of the positive button.
207      */
getPositiveButtonText()208     public CharSequence getPositiveButtonText() {
209         return mPositiveButtonText;
210     }
211 
212     /**
213      * Sets the text of the negative button of the dialog. This will be shown on
214      * subsequent dialogs.
215      *
216      * @param negativeButtonText The text of the negative button.
217      */
setNegativeButtonText(CharSequence negativeButtonText)218     public void setNegativeButtonText(CharSequence negativeButtonText) {
219         mNegativeButtonText = negativeButtonText;
220     }
221 
222     /**
223      * @see #setNegativeButtonText(CharSequence)
224      * @param negativeButtonTextResId The negative button text as a resource.
225      */
setNegativeButtonText(int negativeButtonTextResId)226     public void setNegativeButtonText(int negativeButtonTextResId) {
227         setNegativeButtonText(getContext().getString(negativeButtonTextResId));
228     }
229 
230     /**
231      * Returns the text of the negative button to be shown on subsequent
232      * dialogs.
233      *
234      * @return The text of the negative button.
235      */
getNegativeButtonText()236     public CharSequence getNegativeButtonText() {
237         return mNegativeButtonText;
238     }
239 
240     /**
241      * Sets the layout resource that is inflated as the {@link View} to be shown
242      * as the content View of subsequent dialogs.
243      *
244      * @param dialogLayoutResId The layout resource ID to be inflated.
245      * @see #setDialogMessage(CharSequence)
246      */
setDialogLayoutResource(int dialogLayoutResId)247     public void setDialogLayoutResource(int dialogLayoutResId) {
248         mDialogLayoutResId = dialogLayoutResId;
249     }
250 
251     /**
252      * Returns the layout resource that is used as the content View for
253      * subsequent dialogs.
254      *
255      * @return The layout resource.
256      */
getDialogLayoutResource()257     public int getDialogLayoutResource() {
258         return mDialogLayoutResId;
259     }
260 
261     @Override
onClick()262     protected void onClick() {
263         getPreferenceManager().showDialog(this);
264     }
265 
266 }
267