blob: 278ef5b8a1fdbcaf93ad0c8df439c2e5674b89eb [file] [log] [blame]
James Lemieuxbd9eae12015-08-20 15:40:53 -07001/*
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
17package com.android.deskclock.settings;
Isaac Katzenelson815e2f72012-09-07 18:38:22 -070018
19import android.app.AlertDialog;
20import android.content.Context;
James Lemieuxb3d1bc32015-09-01 11:58:22 -070021import android.content.res.Resources;
Isaac Katzenelson815e2f72012-09-07 18:38:22 -070022import android.content.res.TypedArray;
James Lemieuxb3d1bc32015-09-01 11:58:22 -070023import android.os.Parcelable;
Isaac Katzenelson815e2f72012-09-07 18:38:22 -070024import android.preference.DialogPreference;
James Lemieux88fecb32015-07-22 18:09:32 -070025import android.support.annotation.NonNull;
Isaac Katzenelson815e2f72012-09-07 18:38:22 -070026import android.util.AttributeSet;
27import android.view.View;
28import android.widget.NumberPicker;
29import android.widget.TextView;
30
James Lemieuxbd9eae12015-08-20 15:40:53 -070031import com.android.deskclock.R;
32import com.android.deskclock.Utils;
33
Isaac Katzenelson815e2f72012-09-07 18:38:22 -070034/**
35 * A dialog preference that shows a number picker for selecting snooze length
36 */
James Lemieuxbd9eae12015-08-20 15:40:53 -070037public final class SnoozeLengthDialog extends DialogPreference {
James Lemieux88fecb32015-07-22 18:09:32 -070038
39 private static final String DEFAULT_SNOOZE_TIME = "10";
40
Isaac Katzenelson815e2f72012-09-07 18:38:22 -070041 private NumberPicker mNumberPickerView;
42 private TextView mNumberPickerMinutesView;
43 private final Context mContext;
44 private int mSnoozeMinutes;
45
Isaac Katzenelson815e2f72012-09-07 18:38:22 -070046 public SnoozeLengthDialog(Context context, AttributeSet attrs) {
47 super(context, attrs);
48 mContext = context;
49 setDialogLayoutResource(R.layout.snooze_length_picker);
50 setTitle(R.string.snooze_duration_title);
51 }
52
53 @Override
54 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
55 super.onPrepareDialogBuilder(builder);
56 builder.setTitle(getContext().getString(R.string.snooze_duration_title))
57 .setCancelable(true);
58 }
59
60 @Override
James Lemieux88fecb32015-07-22 18:09:32 -070061 protected void onBindDialogView(@NonNull View view) {
Isaac Katzenelson815e2f72012-09-07 18:38:22 -070062 super.onBindDialogView(view);
63 mNumberPickerMinutesView = (TextView) view.findViewById(R.id.title);
64 mNumberPickerView = (NumberPicker) view.findViewById(R.id.minutes_picker);
65 mNumberPickerView.setMinValue(1);
66 mNumberPickerView.setMaxValue(30);
67 mNumberPickerView.setValue(mSnoozeMinutes);
James Lemieux88fecb32015-07-22 18:09:32 -070068 updateUnits();
Isaac Katzenelson815e2f72012-09-07 18:38:22 -070069 mNumberPickerView.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
70 @Override
71 public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
James Lemieux88fecb32015-07-22 18:09:32 -070072 updateUnits();
Isaac Katzenelson815e2f72012-09-07 18:38:22 -070073 }
74 });
75 }
76
77 @Override
James Lemieux88fecb32015-07-22 18:09:32 -070078 protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
Isaac Katzenelson815e2f72012-09-07 18:38:22 -070079 String val;
80 if (restorePersistedValue) {
81 val = getPersistedString(DEFAULT_SNOOZE_TIME);
82 if (val != null) {
83 mSnoozeMinutes = Integer.parseInt(val);
84 }
85 } else {
86 val = (String) defaultValue;
87 if (val != null) {
88 mSnoozeMinutes = Integer.parseInt(val);
89 }
90 persistString(val);
91 }
92 }
93
94 @Override
95 protected Object onGetDefaultValue(TypedArray a, int index) {
96 return a.getString(index);
97 }
98
James Lemieuxb3d1bc32015-09-01 11:58:22 -070099 @Override
100 protected void onRestoreInstanceState(Parcelable state) {
101 // Restore the value to the NumberPicker.
102 super.onRestoreInstanceState(state);
103
104 // Update the unit display in response to the new value.
105 updateUnits();
106 }
107
James Lemieux88fecb32015-07-22 18:09:32 -0700108 private void updateUnits() {
James Lemieuxb3d1bc32015-09-01 11:58:22 -0700109 if (mNumberPickerView != null) {
110 final Resources res = mContext.getResources();
111 final int value = mNumberPickerView.getValue();
112 final CharSequence units = res.getQuantityText(R.plurals.snooze_picker_label, value);
113 mNumberPickerMinutesView.setText(units);
114 }
Isaac Katzenelson815e2f72012-09-07 18:38:22 -0700115 }
James Lemieux88fecb32015-07-22 18:09:32 -0700116
Isaac Katzenelson815e2f72012-09-07 18:38:22 -0700117 @Override
118 protected void onDialogClosed(boolean positiveResult) {
119 if (positiveResult) {
120 mNumberPickerView.clearFocus();
121 mSnoozeMinutes = mNumberPickerView.getValue();
122 persistString(Integer.toString(mSnoozeMinutes));
123 setSummary();
124 }
125 }
126
127 public void setSummary() {
Annie Chin671e4cb2015-06-12 14:58:11 -0700128 setSummary(Utils.getNumberFormattedQuantityString(mContext, R.plurals.snooze_duration,
James Lemieuxbd9eae12015-08-20 15:40:53 -0700129 mSnoozeMinutes));
Isaac Katzenelson815e2f72012-09-07 18:38:22 -0700130 }
James Lemieuxbd9eae12015-08-20 15:40:53 -0700131}