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 com.android.mms.ui.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 int mInitialNumber;
     36 
     37     private static final String NUMBER = "number";
     38 
     39     /**
     40      * The callback interface used to indicate the user is done filling in
     41      * the time (they clicked on the 'Set' button).
     42      */
     43     public interface OnNumberSetListener {
     44 
     45         /**
     46          * @param number The number that was set.
     47          */
     48         void onNumberSet(int number);
     49     }
     50 
     51     private final NonWrapNumberPicker mNumberPicker;
     52     private final OnNumberSetListener mCallback;
     53 
     54     /**
     55      * @param context Parent.
     56      * @param callBack How parent is notified.
     57      * @param number The initial number.
     58      */
     59     public NumberPickerDialog(Context context,
     60             OnNumberSetListener callBack,
     61             int number,
     62             int rangeMin,
     63             int rangeMax,
     64             int title) {
     65         this(context, com.android.internal.R.style.Theme_Dialog_Alert,
     66                 callBack, number, rangeMin, rangeMax, title);
     67     }
     68 
     69     /**
     70      * @param context Parent.
     71      * @param theme the theme to apply to this dialog
     72      * @param callBack How parent is notified.
     73      * @param number The initial number.
     74      */
     75     public NumberPickerDialog(Context context,
     76             int theme,
     77             OnNumberSetListener callBack,
     78             int number,
     79             int rangeMin,
     80             int rangeMax,
     81             int title) {
     82         super(context, theme);
     83         mCallback = callBack;
     84         mInitialNumber = number;
     85 
     86         setTitle(title);
     87 
     88         setButton(DialogInterface.BUTTON_POSITIVE, context.getText(R.string.set), this);
     89         setButton(DialogInterface.BUTTON_NEGATIVE, context.getText(R.string.no),
     90                 (OnClickListener) null);
     91 
     92         LayoutInflater inflater =
     93                 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     94         View view = inflater.inflate(R.layout.number_picker_dialog, null);
     95         setView(view);
     96         mNumberPicker = (NonWrapNumberPicker) view.findViewById(R.id.number_picker);
     97 
     98         // initialize state
     99         mNumberPicker.setRange(rangeMin, rangeMax);
    100         mNumberPicker.setCurrent(number);
    101         mNumberPicker.setSpeed(150);    // make the repeat rate twice as fast as normal since the
    102                                         // range is so large.
    103     }
    104 
    105     public void onClick(DialogInterface dialog, int which) {
    106         if (mCallback != null) {
    107             mNumberPicker.clearFocus();
    108             mCallback.onNumberSet(mNumberPicker.getCurrent());
    109             dialog.dismiss();
    110         }
    111     }
    112 
    113     @Override
    114     public Bundle onSaveInstanceState() {
    115         Bundle state = super.onSaveInstanceState();
    116         state.putInt(NUMBER, mNumberPicker.getCurrent());
    117         return state;
    118     }
    119 
    120     @Override
    121     public void onRestoreInstanceState(Bundle savedInstanceState) {
    122         super.onRestoreInstanceState(savedInstanceState);
    123         int number = savedInstanceState.getInt(NUMBER);
    124         mNumberPicker.setCurrent(number);
    125     }
    126 
    127     public static class NonWrapNumberPicker extends NumberPicker {
    128 
    129         public NonWrapNumberPicker(Context context) {
    130             this(context, null);
    131         }
    132 
    133         public NonWrapNumberPicker(Context context, AttributeSet attrs) {
    134             this(context, attrs, 0);
    135         }
    136 
    137         @SuppressWarnings({"UnusedDeclaration"})
    138         public NonWrapNumberPicker(Context context, AttributeSet attrs, int defStyle) {
    139             super(context, attrs);
    140         }
    141 
    142         @Override
    143         protected void changeCurrent(int current) {
    144             // Don't wrap. Pin instead.
    145             if (current > getEndRange()) {
    146                 current = getEndRange();
    147             } else if (current < getBeginRange()) {
    148                 current = getBeginRange();
    149             }
    150             super.changeCurrent(current);
    151         }
    152 
    153     }
    154 
    155 }
    156