Home | History | Annotate | Download | only in deskclock
      1 package com.android.deskclock;
      2 
      3 import android.app.AlertDialog;
      4 import android.content.Context;
      5 import android.content.res.Resources;
      6 import android.content.res.TypedArray;
      7 import android.preference.DialogPreference;
      8 import android.util.AttributeSet;
      9 import android.view.View;
     10 import android.widget.NumberPicker;
     11 import android.widget.TextView;
     12 
     13 /**
     14  * A dialog preference that shows a number picker for selecting snooze length
     15  */
     16 public class SnoozeLengthDialog extends DialogPreference {
     17     private NumberPicker mNumberPickerView;
     18     private TextView mNumberPickerMinutesView;
     19     private final Context mContext;
     20     private int mSnoozeMinutes;
     21 
     22     private static final String DEFAULT_SNOOZE_TIME = "10";
     23 
     24 
     25     public SnoozeLengthDialog(Context context, AttributeSet attrs) {
     26         super(context, attrs);
     27         mContext = context;
     28         setDialogLayoutResource(R.layout.snooze_length_picker);
     29         setTitle(R.string.snooze_duration_title);
     30     }
     31 
     32     @Override
     33     protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
     34         super.onPrepareDialogBuilder(builder);
     35         builder.setTitle(getContext().getString(R.string.snooze_duration_title))
     36                 .setCancelable(true);
     37     }
     38 
     39     @Override
     40     protected void onBindDialogView(View view) {
     41         super.onBindDialogView(view);
     42         mNumberPickerMinutesView = (TextView) view.findViewById(R.id.title);
     43         mNumberPickerView = (NumberPicker) view.findViewById(R.id.minutes_picker);
     44         mNumberPickerView.setMinValue(1);
     45         mNumberPickerView.setMaxValue(30);
     46         mNumberPickerView.setValue(mSnoozeMinutes);
     47         updateDays();
     48         mNumberPickerView.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
     49             @Override
     50             public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
     51                 updateDays();
     52             }
     53         });
     54     }
     55 
     56     @Override
     57     protected void onSetInitialValue (boolean restorePersistedValue, Object defaultValue) {
     58         String val;
     59         if (restorePersistedValue) {
     60             val = getPersistedString(DEFAULT_SNOOZE_TIME);
     61             if (val != null) {
     62                 mSnoozeMinutes = Integer.parseInt(val);
     63             }
     64         } else {
     65             val = (String) defaultValue;
     66             if (val != null) {
     67                 mSnoozeMinutes = Integer.parseInt(val);
     68             }
     69             persistString(val);
     70         }
     71     }
     72 
     73     @Override
     74     protected Object onGetDefaultValue(TypedArray a, int index) {
     75         return a.getString(index);
     76     }
     77 
     78     private void updateDays() {
     79         mNumberPickerMinutesView.setText(String.format(mContext.getResources()
     80                 .getQuantityText(R.plurals.snooze_picker_label, mNumberPickerView.getValue())
     81                 .toString()));
     82     }
     83     @Override
     84     protected void onDialogClosed(boolean positiveResult) {
     85         if (positiveResult) {
     86             mNumberPickerView.clearFocus();
     87             mSnoozeMinutes = mNumberPickerView.getValue();
     88             persistString(Integer.toString(mSnoozeMinutes));
     89             setSummary();
     90         }
     91     }
     92 
     93     public void setSummary() {
     94         setSummary(String.format(mContext.getResources()
     95                 .getQuantityText(R.plurals.snooze_duration, mSnoozeMinutes).toString(),
     96                 mSnoozeMinutes));
     97     }
     98 
     99     public int getCurrentValue() {
    100         return mSnoozeMinutes;
    101     }
    102 }
    103 
    104 
    105 
    106