Home | History | Annotate | Download | only in settings
      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;
     18 
     19 import android.app.AlertDialog;
     20 import android.content.Context;
     21 import android.content.res.Resources;
     22 import android.content.res.TypedArray;
     23 import android.os.Parcelable;
     24 import android.preference.DialogPreference;
     25 import android.support.annotation.NonNull;
     26 import android.util.AttributeSet;
     27 import android.view.View;
     28 import android.widget.NumberPicker;
     29 import android.widget.TextView;
     30 
     31 import com.android.deskclock.NumberPickerCompat;
     32 import com.android.deskclock.R;
     33 import com.android.deskclock.Utils;
     34 
     35 /**
     36  * A dialog preference that shows a number picker for selecting snooze length
     37  */
     38 public final class SnoozeLengthDialog extends DialogPreference {
     39 
     40     private static final String DEFAULT_SNOOZE_TIME = "10";
     41 
     42     private NumberPickerCompat mNumberPickerView;
     43     private TextView mNumberPickerMinutesView;
     44     private final Context mContext;
     45     private int mSnoozeMinutes;
     46 
     47     public SnoozeLengthDialog(Context context, AttributeSet attrs) {
     48         super(context, attrs);
     49         mContext = context;
     50         setDialogLayoutResource(R.layout.snooze_length_picker);
     51         setTitle(R.string.snooze_duration_title);
     52     }
     53 
     54     @Override
     55     protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
     56         super.onPrepareDialogBuilder(builder);
     57         builder.setTitle(getContext().getString(R.string.snooze_duration_title))
     58                 .setCancelable(true);
     59     }
     60 
     61     @Override
     62     protected void onBindDialogView(@NonNull View view) {
     63         super.onBindDialogView(view);
     64         mNumberPickerMinutesView = (TextView) view.findViewById(R.id.title);
     65         mNumberPickerView = (NumberPickerCompat) view.findViewById(R.id.minutes_picker);
     66         mNumberPickerView.setMinValue(1);
     67         mNumberPickerView.setMaxValue(30);
     68         mNumberPickerView.setValue(mSnoozeMinutes);
     69         updateUnits();
     70 
     71         mNumberPickerView.setOnAnnounceValueChangedListener(
     72                 new NumberPickerCompat.OnAnnounceValueChangedListener() {
     73             @Override
     74             public void onAnnounceValueChanged(NumberPicker picker, int value,
     75                     String displayedValue) {
     76                 final String announceString = Utils.getNumberFormattedQuantityString(
     77                         mContext, R.plurals.snooze_duration, value);
     78                 picker.announceForAccessibility(announceString);
     79             }
     80         });
     81     }
     82 
     83     @Override
     84     protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
     85         String val;
     86         if (restorePersistedValue) {
     87             val = getPersistedString(DEFAULT_SNOOZE_TIME);
     88             if (val != null) {
     89                 mSnoozeMinutes = Integer.parseInt(val);
     90             }
     91         } else {
     92             val = (String) defaultValue;
     93             if (val != null) {
     94                 mSnoozeMinutes = Integer.parseInt(val);
     95             }
     96             persistString(val);
     97         }
     98     }
     99 
    100     @Override
    101     protected Object onGetDefaultValue(TypedArray a, int index) {
    102         return a.getString(index);
    103     }
    104 
    105     @Override
    106     protected void onRestoreInstanceState(Parcelable state) {
    107         // Restore the value to the NumberPicker.
    108         super.onRestoreInstanceState(state);
    109 
    110         // Update the unit display in response to the new value.
    111         updateUnits();
    112     }
    113 
    114     private void updateUnits() {
    115         if (mNumberPickerView != null) {
    116             final Resources res = mContext.getResources();
    117             final int value = mNumberPickerView.getValue();
    118             final CharSequence units = res.getQuantityText(R.plurals.snooze_picker_label, value);
    119             mNumberPickerMinutesView.setText(units);
    120         }
    121     }
    122 
    123     @Override
    124     protected void onDialogClosed(boolean positiveResult) {
    125         if (positiveResult) {
    126             mNumberPickerView.clearFocus();
    127             mSnoozeMinutes = mNumberPickerView.getValue();
    128             persistString(Integer.toString(mSnoozeMinutes));
    129             setSummary();
    130         }
    131     }
    132 
    133     public void setSummary() {
    134         setSummary(Utils.getNumberFormattedQuantityString(mContext, R.plurals.snooze_duration,
    135                 mSnoozeMinutes));
    136     }
    137 }