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