blob: 952227fe0c5e757f4ac3ee6cf21e9b448d0f5606 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Svetoslav Ganov206316a2010-11-19 00:04:05 -080019import com.android.internal.R;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
22import android.content.DialogInterface;
23import android.content.DialogInterface.OnClickListener;
24import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.view.LayoutInflater;
26import android.view.View;
27import android.widget.TimePicker;
28import android.widget.TimePicker.OnTimeChangedListener;
29
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030/**
31 * A dialog that prompts the user for the time of day using a {@link TimePicker}.
Scott Main41ec6532010-08-19 16:57:07 -070032 *
Scott Main4c359b72012-07-24 15:51:27 -070033 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
34 * guide.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 */
Svetoslav Ganov206316a2010-11-19 00:04:05 -080036public class TimePickerDialog extends AlertDialog
37 implements OnClickListener, OnTimeChangedListener {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
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";
Christian Mehlmauera4713072010-06-25 20:03:42 +020056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 private final TimePicker mTimePicker;
58 private final OnTimeSetListener mCallback;
Christian Mehlmauera4713072010-06-25 20:03:42 +020059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 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) {
Svetoslav Ganovbf805622010-12-17 16:00:18 -080074 this(context, 0, callBack, hourOfDay, minute, is24HourView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 }
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
Svetoslav Ganov206316a2010-11-19 00:04:05 -080095 setIcon(0);
96 setTitle(R.string.time_picker_dialog_title);
Christian Mehlmauera4713072010-06-25 20:03:42 +020097
Svetoslav Ganov9f086d82011-11-29 18:27:23 -080098 Context themeContext = getContext();
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -070099 setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_done), this);
Christian Mehlmauera4713072010-06-25 20:03:42 +0200100
101 LayoutInflater inflater =
Svetoslav Ganov9f086d82011-11-29 18:27:23 -0800102 (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 View view = inflater.inflate(R.layout.time_picker_dialog, null);
104 setView(view);
105 mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
106
107 // initialize state
Brian Muramatsu87879e82011-02-01 17:23:52 -0800108 mTimePicker.setIs24HourView(mIs24HourView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 mTimePicker.setCurrentHour(mInitialHourOfDay);
110 mTimePicker.setCurrentMinute(mInitialMinute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 mTimePicker.setOnTimeChangedListener(this);
112 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 public void onClick(DialogInterface dialog, int which) {
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -0700115 tryNotifyTimeSet();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 }
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public void updateTime(int hourOfDay, int minutOfHour) {
119 mTimePicker.setCurrentHour(hourOfDay);
120 mTimePicker.setCurrentMinute(minutOfHour);
121 }
122
Svetoslav Ganov206316a2010-11-19 00:04:05 -0800123 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
124 /* do nothing */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200126
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -0700127 private void tryNotifyTimeSet() {
128 if (mCallback != null) {
129 mTimePicker.clearFocus();
130 mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
131 mTimePicker.getCurrentMinute());
132 }
133 }
134
135 @Override
136 protected void onStop() {
137 tryNotifyTimeSet();
138 super.onStop();
139 }
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 @Override
142 public Bundle onSaveInstanceState() {
143 Bundle state = super.onSaveInstanceState();
144 state.putInt(HOUR, mTimePicker.getCurrentHour());
145 state.putInt(MINUTE, mTimePicker.getCurrentMinute());
146 state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
147 return state;
148 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 @Override
151 public void onRestoreInstanceState(Bundle savedInstanceState) {
152 super.onRestoreInstanceState(savedInstanceState);
153 int hour = savedInstanceState.getInt(HOUR);
154 int minute = savedInstanceState.getInt(MINUTE);
Brian Muramatsu87879e82011-02-01 17:23:52 -0800155 mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 mTimePicker.setCurrentHour(hour);
157 mTimePicker.setCurrentMinute(minute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 }
159}