TimePickerDialog.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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 android.content.Context;
20import android.content.DialogInterface;
21import android.content.DialogInterface.OnClickListener;
22import android.os.Bundle;
23import android.text.format.DateFormat;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.widget.TimePicker;
27import android.widget.TimePicker.OnTimeChangedListener;
28
29import com.android.internal.R;
30
31import java.util.Calendar;
32
33/**
34 * A dialog that prompts the user for the time of day using a {@link TimePicker}.
35 */
36public class TimePickerDialog extends AlertDialog implements OnClickListener,
37        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    private final Calendar mCalendar;
60    private final java.text.DateFormat mDateFormat;
61
62    int mInitialHourOfDay;
63    int mInitialMinute;
64    boolean mIs24HourView;
65
66    /**
67     * @param context Parent.
68     * @param callBack How parent is notified.
69     * @param hourOfDay The initial hour.
70     * @param minute The initial minute.
71     * @param is24HourView Whether this is a 24 hour view, or AM/PM.
72     */
73    public TimePickerDialog(Context context,
74            OnTimeSetListener callBack,
75            int hourOfDay, int minute, boolean is24HourView) {
76        this(context, com.android.internal.R.style.Theme_Dialog_Alert,
77                callBack, hourOfDay, minute, is24HourView);
78    }
79
80    /**
81     * @param context Parent.
82     * @param theme the theme to apply to this dialog
83     * @param callBack How parent is notified.
84     * @param hourOfDay The initial hour.
85     * @param minute The initial minute.
86     * @param is24HourView Whether this is a 24 hour view, or AM/PM.
87     */
88    public TimePickerDialog(Context context,
89            int theme,
90            OnTimeSetListener callBack,
91            int hourOfDay, int minute, boolean is24HourView) {
92        super(context, theme);
93        mCallback = callBack;
94        mInitialHourOfDay = hourOfDay;
95        mInitialMinute = minute;
96        mIs24HourView = is24HourView;
97
98        mDateFormat = DateFormat.getTimeFormat(context);
99        mCalendar = Calendar.getInstance();
100        updateTitle(mInitialHourOfDay, mInitialMinute);
101
102        setButton(context.getText(R.string.date_time_set), this);
103        setButton2(context.getText(R.string.cancel), (OnClickListener) null);
104        setIcon(R.drawable.ic_dialog_time);
105
106        LayoutInflater inflater =
107                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
108        View view = inflater.inflate(R.layout.time_picker_dialog, null);
109        setView(view);
110        mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
111
112        // initialize state
113        mTimePicker.setCurrentHour(mInitialHourOfDay);
114        mTimePicker.setCurrentMinute(mInitialMinute);
115        mTimePicker.setIs24HourView(mIs24HourView);
116        mTimePicker.setOnTimeChangedListener(this);
117    }
118
119    public void onClick(DialogInterface dialog, int which) {
120        if (mCallback != null) {
121            mTimePicker.clearFocus();
122            mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
123                    mTimePicker.getCurrentMinute());
124        }
125    }
126
127    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
128        updateTitle(hourOfDay, minute);
129    }
130
131    public void updateTime(int hourOfDay, int minutOfHour) {
132        mTimePicker.setCurrentHour(hourOfDay);
133        mTimePicker.setCurrentMinute(minutOfHour);
134    }
135
136    private void updateTitle(int hour, int minute) {
137        mCalendar.set(Calendar.HOUR_OF_DAY, hour);
138        mCalendar.set(Calendar.MINUTE, minute);
139        setTitle(mDateFormat.format(mCalendar.getTime()));
140    }
141
142    @Override
143    public Bundle onSaveInstanceState() {
144        Bundle state = super.onSaveInstanceState();
145        state.putInt(HOUR, mTimePicker.getCurrentHour());
146        state.putInt(MINUTE, mTimePicker.getCurrentMinute());
147        state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
148        return state;
149    }
150
151    @Override
152    public void onRestoreInstanceState(Bundle savedInstanceState) {
153        super.onRestoreInstanceState(savedInstanceState);
154        int hour = savedInstanceState.getInt(HOUR);
155        int minute = savedInstanceState.getInt(MINUTE);
156        mTimePicker.setCurrentHour(hour);
157        mTimePicker.setCurrentMinute(minute);
158        mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
159        mTimePicker.setOnTimeChangedListener(this);
160        updateTitle(hour, minute);
161    }
162}
163