Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2007 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.mms.ui;
     18 
     19 import android.app.AlertDialog;
     20 import android.content.Context;
     21 import android.content.DialogInterface;
     22 import android.content.DialogInterface.OnClickListener;
     23 import android.os.Bundle;
     24 import android.util.AttributeSet;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.widget.NumberPicker;
     28 
     29 import com.android.mms.R;
     30 
     31 /**
     32  * A dialog that prompts the user for the message deletion limits.
     33  */
     34 public class NumberPickerDialog extends AlertDialog implements OnClickListener {
     35     private static final String NUMBER = "number";
     36 
     37     /**
     38      * The callback interface used to indicate the user is done filling in
     39      * the time (they clicked on the 'Set' button).
     40      */
     41     public interface OnNumberSetListener {
     42 
     43         /**
     44          * @param number The number that was set.
     45          */
     46         void onNumberSet(int number);
     47     }
     48 
     49     private final NumberPicker mNumberPicker;
     50     private final OnNumberSetListener mCallback;
     51 
     52     /**
     53      * @param context Parent.
     54      * @param callBack How parent is notified.
     55      * @param number The initial number.
     56      */
     57     public NumberPickerDialog(Context context,
     58             OnNumberSetListener callBack,
     59             int number,
     60             int rangeMin,
     61             int rangeMax,
     62             int title) {
     63         this(context, AlertDialog.THEME_HOLO_LIGHT, callBack, number, rangeMin, rangeMax, title);
     64     }
     65 
     66     /**
     67      * @param context Parent.
     68      * @param theme the theme to apply to this dialog
     69      * @param callBack How parent is notified.
     70      * @param number The initial number.
     71      */
     72     public NumberPickerDialog(Context context,
     73             int theme,
     74             OnNumberSetListener callBack,
     75             int number,
     76             int rangeMin,
     77             int rangeMax,
     78             int title) {
     79         super(context, theme);
     80         mCallback = callBack;
     81 
     82         setTitle(title);
     83 
     84         setButton(DialogInterface.BUTTON_POSITIVE, context.getText(R.string.set), this);
     85         setButton(DialogInterface.BUTTON_NEGATIVE, context.getText(R.string.no),
     86                 (OnClickListener) null);
     87 
     88         LayoutInflater inflater =
     89                 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     90         View view = inflater.inflate(R.layout.number_picker_dialog, null);
     91         setView(view);
     92         mNumberPicker = (NumberPicker) view.findViewById(R.id.number_picker);
     93 
     94         // initialize state
     95         mNumberPicker.setMinValue(rangeMin);
     96         mNumberPicker.setMaxValue(rangeMax);
     97         mNumberPicker.setValue(number);
     98         mNumberPicker.setOnLongPressUpdateInterval(100); // make the repeat rate three times as fast
     99                                                          // as normal since the range is so large.
    100         mNumberPicker.setWrapSelectorWheel(false);       // don't wrap from min->max
    101     }
    102 
    103     public void onClick(DialogInterface dialog, int which) {
    104         if (mCallback != null) {
    105             mNumberPicker.clearFocus();
    106             mCallback.onNumberSet(mNumberPicker.getValue());
    107             dialog.dismiss();
    108         }
    109     }
    110 
    111     @Override
    112     public Bundle onSaveInstanceState() {
    113         Bundle state = super.onSaveInstanceState();
    114         state.putInt(NUMBER, mNumberPicker.getValue());
    115         return state;
    116     }
    117 
    118     @Override
    119     public void onRestoreInstanceState(Bundle savedInstanceState) {
    120         super.onRestoreInstanceState(savedInstanceState);
    121         int number = savedInstanceState.getInt(NUMBER);
    122         mNumberPicker.setValue(number);
    123     }
    124 }
    125