James Lemieux | bd9eae1 | 2015-08-20 15:40:53 -0700 | [diff] [blame] | 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 com.android.deskclock.settings; |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 18 | |
| 19 | import android.app.AlertDialog; |
| 20 | import android.content.Context; |
James Lemieux | b3d1bc3 | 2015-09-01 11:58:22 -0700 | [diff] [blame^] | 21 | import android.content.res.Resources; |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 22 | import android.content.res.TypedArray; |
James Lemieux | b3d1bc3 | 2015-09-01 11:58:22 -0700 | [diff] [blame^] | 23 | import android.os.Parcelable; |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 24 | import android.preference.DialogPreference; |
James Lemieux | 88fecb3 | 2015-07-22 18:09:32 -0700 | [diff] [blame] | 25 | import android.support.annotation.NonNull; |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 26 | import android.util.AttributeSet; |
| 27 | import android.view.View; |
| 28 | import android.widget.NumberPicker; |
| 29 | import android.widget.TextView; |
| 30 | |
James Lemieux | bd9eae1 | 2015-08-20 15:40:53 -0700 | [diff] [blame] | 31 | import com.android.deskclock.R; |
| 32 | import com.android.deskclock.Utils; |
| 33 | |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 34 | /** |
| 35 | * A dialog preference that shows a number picker for selecting snooze length |
| 36 | */ |
James Lemieux | bd9eae1 | 2015-08-20 15:40:53 -0700 | [diff] [blame] | 37 | public final class SnoozeLengthDialog extends DialogPreference { |
James Lemieux | 88fecb3 | 2015-07-22 18:09:32 -0700 | [diff] [blame] | 38 | |
| 39 | private static final String DEFAULT_SNOOZE_TIME = "10"; |
| 40 | |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 41 | private NumberPicker mNumberPickerView; |
| 42 | private TextView mNumberPickerMinutesView; |
| 43 | private final Context mContext; |
| 44 | private int mSnoozeMinutes; |
| 45 | |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 46 | 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 Lemieux | 88fecb3 | 2015-07-22 18:09:32 -0700 | [diff] [blame] | 61 | protected void onBindDialogView(@NonNull View view) { |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 62 | 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 Lemieux | 88fecb3 | 2015-07-22 18:09:32 -0700 | [diff] [blame] | 68 | updateUnits(); |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 69 | mNumberPickerView.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { |
| 70 | @Override |
| 71 | public void onValueChange(NumberPicker picker, int oldVal, int newVal) { |
James Lemieux | 88fecb3 | 2015-07-22 18:09:32 -0700 | [diff] [blame] | 72 | updateUnits(); |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 73 | } |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | @Override |
James Lemieux | 88fecb3 | 2015-07-22 18:09:32 -0700 | [diff] [blame] | 78 | protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) { |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 79 | 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 Lemieux | b3d1bc3 | 2015-09-01 11:58:22 -0700 | [diff] [blame^] | 99 | @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 Lemieux | 88fecb3 | 2015-07-22 18:09:32 -0700 | [diff] [blame] | 108 | private void updateUnits() { |
James Lemieux | b3d1bc3 | 2015-09-01 11:58:22 -0700 | [diff] [blame^] | 109 | 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 Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 115 | } |
James Lemieux | 88fecb3 | 2015-07-22 18:09:32 -0700 | [diff] [blame] | 116 | |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 117 | @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 Chin | 671e4cb | 2015-06-12 14:58:11 -0700 | [diff] [blame] | 128 | setSummary(Utils.getNumberFormattedQuantityString(mContext, R.plurals.snooze_duration, |
James Lemieux | bd9eae1 | 2015-08-20 15:40:53 -0700 | [diff] [blame] | 129 | mSnoozeMinutes)); |
Isaac Katzenelson | 815e2f7 | 2012-09-07 18:38:22 -0700 | [diff] [blame] | 130 | } |
James Lemieux | bd9eae1 | 2015-08-20 15:40:53 -0700 | [diff] [blame] | 131 | } |