blob: a85c61f2321f80a355e3d27f79a0eb9e3921d2d8 [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
19import android.content.Context;
20import android.content.DialogInterface;
21import android.content.DialogInterface.OnClickListener;
22import android.os.Bundle;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070023import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.view.LayoutInflater;
25import android.view.View;
26import android.widget.TimePicker;
27import android.widget.TimePicker.OnTimeChangedListener;
28
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070029import com.android.internal.R;
30
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032/**
33 * A dialog that prompts the user for the time of day using a {@link TimePicker}.
Scott Main41ec6532010-08-19 16:57:07 -070034 *
Scott Main4c359b72012-07-24 15:51:27 -070035 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
36 * guide.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 */
Svetoslav Ganov206316a2010-11-19 00:04:05 -080038public class TimePickerDialog extends AlertDialog
39 implements OnClickListener, OnTimeChangedListener {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41 /**
42 * The callback interface used to indicate the user is done filling in
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070043 * the time (they clicked on the 'Done' button).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 */
45 public interface OnTimeSetListener {
46
47 /**
48 * @param view The view associated with this listener.
49 * @param hourOfDay The hour that was set.
50 * @param minute The minute that was set.
51 */
52 void onTimeSet(TimePicker view, int hourOfDay, int minute);
53 }
54
55 private static final String HOUR = "hour";
56 private static final String MINUTE = "minute";
57 private static final String IS_24_HOUR = "is24hour";
Christian Mehlmauera4713072010-06-25 20:03:42 +020058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 private final TimePicker mTimePicker;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070060 private final OnTimeSetListener mTimeSetCallback;
Christian Mehlmauera4713072010-06-25 20:03:42 +020061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 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) {
Svetoslav Ganovbf805622010-12-17 16:00:18 -080076 this(context, 0, callBack, hourOfDay, minute, is24HourView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 }
78
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070079 static int resolveDialogTheme(Context context, int resid) {
80 if (resid == 0) {
81 TypedValue outValue = new TypedValue();
82 context.getTheme().resolveAttribute(R.attr.timePickerDialogTheme, outValue, true);
83 return outValue.resourceId;
84 } else {
85 return resid;
86 }
87 }
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 /**
90 * @param context Parent.
91 * @param theme the theme to apply to this dialog
92 * @param callBack How parent is notified.
93 * @param hourOfDay The initial hour.
94 * @param minute The initial minute.
95 * @param is24HourView Whether this is a 24 hour view, or AM/PM.
96 */
97 public TimePickerDialog(Context context,
98 int theme,
99 OnTimeSetListener callBack,
100 int hourOfDay, int minute, boolean is24HourView) {
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700101 super(context, resolveDialogTheme(context, theme));
102 mTimeSetCallback = callBack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 mInitialHourOfDay = hourOfDay;
104 mInitialMinute = minute;
105 mIs24HourView = is24HourView;
106
Svetoslav Ganov9f086d82011-11-29 18:27:23 -0800107 Context themeContext = getContext();
Christian Mehlmauera4713072010-06-25 20:03:42 +0200108
109 LayoutInflater inflater =
Svetoslav Ganov9f086d82011-11-29 18:27:23 -0800110 (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 View view = inflater.inflate(R.layout.time_picker_dialog, null);
112 setView(view);
113 mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
114
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700115 // Initialize state
116 mTimePicker.setLegacyMode(false /* will show new UI */);
117 mTimePicker.setShowDoneButton(true);
118 mTimePicker.setDismissCallback(new TimePicker.TimePickerDismissCallback() {
119 @Override
120 public void dismiss(TimePicker view, boolean isCancel, int hourOfDay, int minute) {
121 if (!isCancel) {
122 mTimeSetCallback.onTimeSet(view, hourOfDay, minute);
123 }
124 TimePickerDialog.this.dismiss();
125 }
126 });
Brian Muramatsu87879e82011-02-01 17:23:52 -0800127 mTimePicker.setIs24HourView(mIs24HourView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 mTimePicker.setCurrentHour(mInitialHourOfDay);
129 mTimePicker.setCurrentMinute(mInitialMinute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 mTimePicker.setOnTimeChangedListener(this);
131 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 public void onClick(DialogInterface dialog, int which) {
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -0700134 tryNotifyTimeSet();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 public void updateTime(int hourOfDay, int minutOfHour) {
138 mTimePicker.setCurrentHour(hourOfDay);
139 mTimePicker.setCurrentMinute(minutOfHour);
140 }
141
Svetoslav Ganov206316a2010-11-19 00:04:05 -0800142 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
143 /* do nothing */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200145
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -0700146 private void tryNotifyTimeSet() {
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700147 if (mTimeSetCallback != null) {
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -0700148 mTimePicker.clearFocus();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -0700149 mTimeSetCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -0700150 mTimePicker.getCurrentMinute());
151 }
152 }
153
154 @Override
155 protected void onStop() {
156 tryNotifyTimeSet();
157 super.onStop();
158 }
159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 @Override
161 public Bundle onSaveInstanceState() {
162 Bundle state = super.onSaveInstanceState();
163 state.putInt(HOUR, mTimePicker.getCurrentHour());
164 state.putInt(MINUTE, mTimePicker.getCurrentMinute());
165 state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
166 return state;
167 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 @Override
170 public void onRestoreInstanceState(Bundle savedInstanceState) {
171 super.onRestoreInstanceState(savedInstanceState);
172 int hour = savedInstanceState.getInt(HOUR);
173 int minute = savedInstanceState.getInt(MINUTE);
Brian Muramatsu87879e82011-02-01 17:23:52 -0800174 mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 mTimePicker.setCurrentHour(hour);
176 mTimePicker.setCurrentMinute(minute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 }
178}