TimePickerDialog.java revision 9f086d812ff04703de4b14e02a7702ba7c8acb33
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 android.app;
18
19import com.android.internal.R;
20
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.DialogInterface.OnClickListener;
24import android.os.Bundle;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.widget.TimePicker;
28import android.widget.TimePicker.OnTimeChangedListener;
29
30/**
31 * A dialog that prompts the user for the time of day using a {@link TimePicker}.
32 *
33 * <p>See the <a href="{@docRoot}resources/tutorials/views/hello-timepicker.html">Time Picker
34 * tutorial</a>.</p>
35 */
36public class TimePickerDialog extends AlertDialog
37        implements OnClickListener, OnTimeChangedListener {
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 OnTimeSetListener {
44
45        /**
46         * @param view The view associated with this listener.
47         * @param hourOfDay The hour that was set.
48         * @param minute The minute that was set.
49         */
50        void onTimeSet(TimePicker view, int hourOfDay, int minute);
51    }
52
53    private static final String HOUR = "hour";
54    private static final String MINUTE = "minute";
55    private static final String IS_24_HOUR = "is24hour";
56
57    private final TimePicker mTimePicker;
58    private final OnTimeSetListener mCallback;
59
60    int mInitialHourOfDay;
61    int mInitialMinute;
62    boolean mIs24HourView;
63
64    /**
65     * @param context Parent.
66     * @param callBack How parent is notified.
67     * @param hourOfDay The initial hour.
68     * @param minute The initial minute.
69     * @param is24HourView Whether this is a 24 hour view, or AM/PM.
70     */
71    public TimePickerDialog(Context context,
72            OnTimeSetListener callBack,
73            int hourOfDay, int minute, boolean is24HourView) {
74        this(context, 0, callBack, hourOfDay, minute, is24HourView);
75    }
76
77    /**
78     * @param context Parent.
79     * @param theme the theme to apply to this dialog
80     * @param callBack How parent is notified.
81     * @param hourOfDay The initial hour.
82     * @param minute The initial minute.
83     * @param is24HourView Whether this is a 24 hour view, or AM/PM.
84     */
85    public TimePickerDialog(Context context,
86            int theme,
87            OnTimeSetListener callBack,
88            int hourOfDay, int minute, boolean is24HourView) {
89        super(context, theme);
90        mCallback = callBack;
91        mInitialHourOfDay = hourOfDay;
92        mInitialMinute = minute;
93        mIs24HourView = is24HourView;
94
95        setIcon(0);
96        setTitle(R.string.time_picker_dialog_title);
97
98        Context themeContext = getContext();
99        setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_set), this);
100        setButton(BUTTON_NEGATIVE, themeContext.getText(R.string.cancel),
101                (OnClickListener) null);
102
103        LayoutInflater inflater =
104                (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
105        View view = inflater.inflate(R.layout.time_picker_dialog, null);
106        setView(view);
107        mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
108
109        // initialize state
110        mTimePicker.setIs24HourView(mIs24HourView);
111        mTimePicker.setCurrentHour(mInitialHourOfDay);
112        mTimePicker.setCurrentMinute(mInitialMinute);
113        mTimePicker.setOnTimeChangedListener(this);
114    }
115
116    public void onClick(DialogInterface dialog, int which) {
117        if (mCallback != null) {
118            mTimePicker.clearFocus();
119            mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
120                    mTimePicker.getCurrentMinute());
121        }
122    }
123
124    public void updateTime(int hourOfDay, int minutOfHour) {
125        mTimePicker.setCurrentHour(hourOfDay);
126        mTimePicker.setCurrentMinute(minutOfHour);
127    }
128
129    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
130        /* do nothing */
131    }
132
133    @Override
134    public Bundle onSaveInstanceState() {
135        Bundle state = super.onSaveInstanceState();
136        state.putInt(HOUR, mTimePicker.getCurrentHour());
137        state.putInt(MINUTE, mTimePicker.getCurrentMinute());
138        state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
139        return state;
140    }
141
142    @Override
143    public void onRestoreInstanceState(Bundle savedInstanceState) {
144        super.onRestoreInstanceState(savedInstanceState);
145        int hour = savedInstanceState.getInt(HOUR);
146        int minute = savedInstanceState.getInt(MINUTE);
147        mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
148        mTimePicker.setCurrentHour(hour);
149        mTimePicker.setCurrentMinute(minute);
150    }
151}
152