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