Home | History | Annotate | Download | only in preference
      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 ref android.R.styleable#DialogPreference_dialogTitle
     33  * @attr ref android.R.styleable#DialogPreference_dialogMessage
     34  * @attr ref android.R.styleable#DialogPreference_dialogIcon
     35  * @attr ref android.R.styleable#DialogPreference_dialogLayout
     36  * @attr ref android.R.styleable#DialogPreference_positiveButtonText
     37  * @attr ref android.R.styleable#DialogPreference_negativeButtonText
     38  */
     39 public abstract class DialogPreference extends Preference {
     40 
     41     public interface TargetFragment {
     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 
     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 
     88     public DialogPreference(Context context, AttributeSet attrs, int defStyleAttr) {
     89         this(context, attrs, defStyleAttr, 0);
     90     }
     91 
     92     public DialogPreference(Context context, AttributeSet attrs) {
     93         this(context, attrs, R.attr.dialogPreferenceStyle);
     94     }
     95 
     96     public DialogPreference(Context context) {
     97         this(context, null);
     98     }
     99 
    100     /**
    101      * Sets the title of the dialog. This will be shown on subsequent dialogs.
    102      *
    103      * @param dialogTitle The title.
    104      */
    105     public void setDialogTitle(CharSequence dialogTitle) {
    106         mDialogTitle = dialogTitle;
    107     }
    108 
    109     /**
    110      * @see #setDialogTitle(CharSequence)
    111      * @param dialogTitleResId The dialog title as a resource.
    112      */
    113     public void setDialogTitle(int dialogTitleResId) {
    114         setDialogTitle(getContext().getString(dialogTitleResId));
    115     }
    116 
    117     /**
    118      * Returns the title to be shown on subsequent dialogs.
    119      * @return The title.
    120      */
    121     public CharSequence getDialogTitle() {
    122         return mDialogTitle;
    123     }
    124 
    125     /**
    126      * Sets the message of the dialog. This will be shown on subsequent dialogs.
    127      * <p>
    128      * This message forms the content View of the dialog and conflicts with
    129      * list-based dialogs, for example. If setting a custom View on a dialog via
    130      * {@link #setDialogLayoutResource(int)}, include a text View with ID
    131      * {@link android.R.id#message} and it will be populated with this message.
    132      *
    133      * @param dialogMessage The message.
    134      */
    135     public void setDialogMessage(CharSequence dialogMessage) {
    136         mDialogMessage = dialogMessage;
    137     }
    138 
    139     /**
    140      * @see #setDialogMessage(CharSequence)
    141      * @param dialogMessageResId The dialog message as a resource.
    142      */
    143     public void setDialogMessage(int dialogMessageResId) {
    144         setDialogMessage(getContext().getString(dialogMessageResId));
    145     }
    146 
    147     /**
    148      * Returns the message to be shown on subsequent dialogs.
    149      * @return The message.
    150      */
    151     public CharSequence getDialogMessage() {
    152         return mDialogMessage;
    153     }
    154 
    155     /**
    156      * Sets the icon of the dialog. This will be shown on subsequent dialogs.
    157      *
    158      * @param dialogIcon The icon, as a {@link Drawable}.
    159      */
    160     public void setDialogIcon(Drawable dialogIcon) {
    161         mDialogIcon = dialogIcon;
    162     }
    163 
    164     /**
    165      * Sets the icon (resource ID) of the dialog. This will be shown on
    166      * subsequent dialogs.
    167      *
    168      * @param dialogIconRes The icon, as a resource ID.
    169      */
    170     public void setDialogIcon(int dialogIconRes) {
    171         mDialogIcon = ContextCompat.getDrawable(getContext(), dialogIconRes);
    172     }
    173 
    174     /**
    175      * Returns the icon to be shown on subsequent dialogs.
    176      * @return The icon, as a {@link Drawable}.
    177      */
    178     public Drawable getDialogIcon() {
    179         return mDialogIcon;
    180     }
    181 
    182     /**
    183      * Sets the text of the positive button of the dialog. This will be shown on
    184      * subsequent dialogs.
    185      *
    186      * @param positiveButtonText The text of the positive button.
    187      */
    188     public void setPositiveButtonText(CharSequence positiveButtonText) {
    189         mPositiveButtonText = positiveButtonText;
    190     }
    191 
    192     /**
    193      * @see #setPositiveButtonText(CharSequence)
    194      * @param positiveButtonTextResId The positive button text as a resource.
    195      */
    196     public void setPositiveButtonText(int positiveButtonTextResId) {
    197         setPositiveButtonText(getContext().getString(positiveButtonTextResId));
    198     }
    199 
    200     /**
    201      * Returns the text of the positive button to be shown on subsequent
    202      * dialogs.
    203      *
    204      * @return The text of the positive button.
    205      */
    206     public CharSequence getPositiveButtonText() {
    207         return mPositiveButtonText;
    208     }
    209 
    210     /**
    211      * Sets the text of the negative button of the dialog. This will be shown on
    212      * subsequent dialogs.
    213      *
    214      * @param negativeButtonText The text of the negative button.
    215      */
    216     public void setNegativeButtonText(CharSequence negativeButtonText) {
    217         mNegativeButtonText = negativeButtonText;
    218     }
    219 
    220     /**
    221      * @see #setNegativeButtonText(CharSequence)
    222      * @param negativeButtonTextResId The negative button text as a resource.
    223      */
    224     public void setNegativeButtonText(int negativeButtonTextResId) {
    225         setNegativeButtonText(getContext().getString(negativeButtonTextResId));
    226     }
    227 
    228     /**
    229      * Returns the text of the negative button to be shown on subsequent
    230      * dialogs.
    231      *
    232      * @return The text of the negative button.
    233      */
    234     public CharSequence getNegativeButtonText() {
    235         return mNegativeButtonText;
    236     }
    237 
    238     /**
    239      * Sets the layout resource that is inflated as the {@link View} to be shown
    240      * as the content View of subsequent dialogs.
    241      *
    242      * @param dialogLayoutResId The layout resource ID to be inflated.
    243      * @see #setDialogMessage(CharSequence)
    244      */
    245     public void setDialogLayoutResource(int dialogLayoutResId) {
    246         mDialogLayoutResId = dialogLayoutResId;
    247     }
    248 
    249     /**
    250      * Returns the layout resource that is used as the content View for
    251      * subsequent dialogs.
    252      *
    253      * @return The layout resource.
    254      */
    255     public int getDialogLayoutResource() {
    256         return mDialogLayoutResId;
    257     }
    258 
    259     @Override
    260     protected void onClick() {
    261         getPreferenceManager().showDialog(this);
    262     }
    263 
    264 }
    265