1package com.android.deskclock.settings;
2
3import android.app.AlertDialog;
4import android.content.Context;
5import android.content.res.TypedArray;
6import android.os.Parcelable;
7import android.preference.DialogPreference;
8import android.support.annotation.NonNull;
9import android.util.AttributeSet;
10import android.view.View;
11import android.widget.NumberPicker;
12import android.widget.TextView;
13
14import com.android.deskclock.NumberPickerCompat;
15import com.android.deskclock.R;
16
17/**
18 * A dialog preference that shows a number picker for selecting crescendo length
19 */
20public final class CrescendoLengthDialog extends DialogPreference {
21
22    private static final String DEFAULT_CRESCENDO_TIME = "0";
23    private static final int CRESCENDO_TIME_STEP = 5;
24
25    private NumberPickerCompat mNumberPickerView;
26    private TextView mNumberPickerSecondsView;
27    private int mCrescendoSeconds;
28
29    public CrescendoLengthDialog(Context context, AttributeSet attrs) {
30        super(context, attrs);
31        setDialogLayoutResource(R.layout.crescendo_length_picker);
32        setTitle(R.string.crescendo_duration_title);
33    }
34
35    @Override
36    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
37        super.onPrepareDialogBuilder(builder);
38        builder.setTitle(getContext().getString(R.string.crescendo_duration_title))
39                .setCancelable(true);
40    }
41
42    @Override
43    protected void onBindDialogView(@NonNull View view) {
44        super.onBindDialogView(view);
45
46        final String[] displayedValues = new String[13];
47        displayedValues[0] = getContext().getString(R.string.no_crescendo_duration);
48        for (int i = 1; i < displayedValues.length; i++) {
49            displayedValues[i] = String.valueOf(i * CRESCENDO_TIME_STEP);
50        }
51
52        mNumberPickerSecondsView = (TextView) view.findViewById(R.id.title);
53        mNumberPickerSecondsView.setText(getContext().getString(R.string.crescendo_picker_label));
54        mNumberPickerView = (NumberPickerCompat) view.findViewById(R.id.seconds_picker);
55        mNumberPickerView.setDisplayedValues(displayedValues);
56        mNumberPickerView.setMinValue(0);
57        mNumberPickerView.setMaxValue(displayedValues.length - 1);
58        mNumberPickerView.setValue(mCrescendoSeconds / CRESCENDO_TIME_STEP);
59        updateUnits();
60
61        mNumberPickerView.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
62            @Override
63            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
64                updateUnits();
65            }
66        });
67        mNumberPickerView.setOnAnnounceValueChangedListener(
68                new NumberPickerCompat.OnAnnounceValueChangedListener() {
69            @Override
70            public void onAnnounceValueChanged(NumberPicker picker, int value,
71                    String displayedValue) {
72                final String announceString;
73                if (value == 0) {
74                    announceString = getContext().getString(R.string.no_crescendo_duration);
75                } else {
76                    announceString = getContext().getString(
77                            R.string.crescendo_duration, displayedValue);
78                }
79                picker.announceForAccessibility(announceString);
80            }
81        });
82    }
83
84    @Override
85    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
86        String val;
87        if (restorePersistedValue) {
88            val = getPersistedString(DEFAULT_CRESCENDO_TIME);
89            if (val != null) {
90                mCrescendoSeconds = Integer.parseInt(val);
91            }
92        } else {
93            val = (String) defaultValue;
94            if (val != null) {
95                mCrescendoSeconds = Integer.parseInt(val);
96            }
97            persistString(val);
98        }
99    }
100
101    @Override
102    protected Object onGetDefaultValue(TypedArray a, int index) {
103        return a.getString(index);
104    }
105
106    @Override
107    protected void onRestoreInstanceState(Parcelable state) {
108        // Restore the value to the NumberPicker.
109        super.onRestoreInstanceState(state);
110
111        // Update the unit display in response to the new value.
112        updateUnits();
113    }
114
115    private void updateUnits() {
116        if (mNumberPickerView != null) {
117            final int value = mNumberPickerView.getValue();
118            final int visibility = value == 0 ? View.INVISIBLE : View.VISIBLE;
119            mNumberPickerSecondsView.setVisibility(visibility);
120        }
121    }
122
123    @Override
124    protected void onDialogClosed(boolean positiveResult) {
125        if (positiveResult) {
126            mNumberPickerView.clearFocus();
127            mCrescendoSeconds = mNumberPickerView.getValue() * CRESCENDO_TIME_STEP;
128            persistString(Integer.toString(mCrescendoSeconds));
129            setSummary();
130        }
131    }
132
133    public void setSummary() {
134        if (mCrescendoSeconds == 0) {
135            setSummary(getContext().getString(R.string.no_crescendo_duration));
136        } else {
137            setSummary(getContext().getString(R.string.crescendo_duration, mCrescendoSeconds));
138        }
139    }
140}