blob: aca07636e26ea4f52136a68f0af3cc459e904483 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031/**
Alan Viverette62c79e92015-02-26 09:47:10 -080032 * A dialog that prompts the user for the time of day using a
33 * {@link TimePicker}.
Scott Main41ec6532010-08-19 16:57:07 -070034 *
Alan Viverette62c79e92015-02-26 09:47:10 -080035 * <p>
36 * See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
37 * guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 */
Alan Viverette518ff0d2014-08-15 14:20:35 -070039public class TimePickerDialog extends AlertDialog implements OnClickListener,
40 OnTimeChangedListener {
Alan Viverette518ff0d2014-08-15 14:20:35 -070041 private static final String HOUR = "hour";
42 private static final String MINUTE = "minute";
43 private static final String IS_24_HOUR = "is24hour";
44
45 private final TimePicker mTimePicker;
Alan Viverette62c79e92015-02-26 09:47:10 -080046 private final OnTimeSetListener mTimeSetListener;
Alan Viverette518ff0d2014-08-15 14:20:35 -070047
48 private final int mInitialHourOfDay;
49 private final int mInitialMinute;
50 private final boolean mIs24HourView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
52 /**
53 * The callback interface used to indicate the user is done filling in
Alan Viverette62c79e92015-02-26 09:47:10 -080054 * the time (e.g. they clicked on the 'OK' button).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 */
56 public interface OnTimeSetListener {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 /**
Alan Viverette62c79e92015-02-26 09:47:10 -080058 * Called when the user is done setting a new time and the dialog has
59 * closed.
60 *
61 * @param view the view associated with this listener
62 * @param hourOfDay the hour that was set
63 * @param minute the minute that was set
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 */
Alan Viverette4420ae82015-11-16 16:10:56 -050065 void onTimeSet(TimePicker view, int hourOfDay, int minute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 }
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 /**
Alan Viverette62c79e92015-02-26 09:47:10 -080069 * Creates a new time picker dialog.
70 *
71 * @param context the parent context
72 * @param listener the listener to call when the time is set
73 * @param hourOfDay the initial hour
74 * @param minute the initial minute
75 * @param is24HourView whether this is a 24 hour view or AM/PM
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 */
Alan Viverette62c79e92015-02-26 09:47:10 -080077 public TimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute,
78 boolean is24HourView) {
79 this(context, 0, listener, hourOfDay, minute, is24HourView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 }
81
Alan Viverette62c79e92015-02-26 09:47:10 -080082 static int resolveDialogTheme(Context context, int resId) {
83 if (resId == 0) {
Alan Viverette518ff0d2014-08-15 14:20:35 -070084 final TypedValue outValue = new TypedValue();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070085 context.getTheme().resolveAttribute(R.attr.timePickerDialogTheme, outValue, true);
86 return outValue.resourceId;
87 } else {
Alan Viverette62c79e92015-02-26 09:47:10 -080088 return resId;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070089 }
90 }
91
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /**
Alan Viverette62c79e92015-02-26 09:47:10 -080093 * Creates a new time picker dialog with the specified theme.
94 *
95 * @param context the parent context
96 * @param themeResId the resource ID of the theme to apply to this dialog
97 * @param listener the listener to call when the time is set
98 * @param hourOfDay the initial hour
99 * @param minute the initial minute
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * @param is24HourView Whether this is a 24 hour view, or AM/PM.
101 */
Alan Viverette62c79e92015-02-26 09:47:10 -0800102 public TimePickerDialog(Context context, int themeResId, OnTimeSetListener listener,
103 int hourOfDay, int minute, boolean is24HourView) {
104 super(context, resolveDialogTheme(context, themeResId));
Alan Viverette51344782014-07-16 17:39:27 -0700105
Alan Viverette62c79e92015-02-26 09:47:10 -0800106 mTimeSetListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 mInitialHourOfDay = hourOfDay;
108 mInitialMinute = minute;
109 mIs24HourView = is24HourView;
110
Alan Viverette51344782014-07-16 17:39:27 -0700111 final Context themeContext = getContext();
Alan Viverette62c79e92015-02-26 09:47:10 -0800112
113
114 final TypedValue outValue = new TypedValue();
115 context.getTheme().resolveAttribute(R.attr.timePickerDialogTheme, outValue, true);
Alan Viverette62c79e92015-02-26 09:47:10 -0800116
Alan Viverette51344782014-07-16 17:39:27 -0700117 final LayoutInflater inflater = LayoutInflater.from(themeContext);
118 final View view = inflater.inflate(R.layout.time_picker_dialog, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 setView(view);
Alan Viverette518ff0d2014-08-15 14:20:35 -0700120 setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this);
121 setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this);
Craig Stoutfd4bc4c2014-08-13 12:50:50 -0700122 setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
Alan Viverette51344782014-07-16 17:39:27 -0700124 mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
Brian Muramatsu87879e82011-02-01 17:23:52 -0800125 mTimePicker.setIs24HourView(mIs24HourView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 mTimePicker.setCurrentHour(mInitialHourOfDay);
127 mTimePicker.setCurrentMinute(mInitialMinute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 mTimePicker.setOnTimeChangedListener(this);
129 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200130
Alan Viverette518ff0d2014-08-15 14:20:35 -0700131 @Override
Svetoslav Ganov206316a2010-11-19 00:04:05 -0800132 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
133 /* do nothing */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200135
Alan Viverette518ff0d2014-08-15 14:20:35 -0700136 @Override
137 public void onClick(DialogInterface dialog, int which) {
138 switch (which) {
139 case BUTTON_POSITIVE:
Alan Viverette62c79e92015-02-26 09:47:10 -0800140 if (mTimeSetListener != null) {
141 mTimeSetListener.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
Alan Viverette518ff0d2014-08-15 14:20:35 -0700142 mTimePicker.getCurrentMinute());
143 }
144 break;
Alan Viveretted26276d2014-11-26 16:08:58 -0800145 case BUTTON_NEGATIVE:
146 cancel();
147 break;
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -0700148 }
149 }
150
Alan Viverette518ff0d2014-08-15 14:20:35 -0700151 /**
152 * Sets the current time.
153 *
154 * @param hourOfDay The current hour within the day.
155 * @param minuteOfHour The current minute within the hour.
156 */
157 public void updateTime(int hourOfDay, int minuteOfHour) {
158 mTimePicker.setCurrentHour(hourOfDay);
159 mTimePicker.setCurrentMinute(minuteOfHour);
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -0700160 }
161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 @Override
163 public Bundle onSaveInstanceState() {
Alan Viverette518ff0d2014-08-15 14:20:35 -0700164 final Bundle state = super.onSaveInstanceState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 state.putInt(HOUR, mTimePicker.getCurrentHour());
166 state.putInt(MINUTE, mTimePicker.getCurrentMinute());
167 state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
168 return state;
169 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 @Override
172 public void onRestoreInstanceState(Bundle savedInstanceState) {
173 super.onRestoreInstanceState(savedInstanceState);
Alan Viverette518ff0d2014-08-15 14:20:35 -0700174 final int hour = savedInstanceState.getInt(HOUR);
175 final int minute = savedInstanceState.getInt(MINUTE);
Brian Muramatsu87879e82011-02-01 17:23:52 -0800176 mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 mTimePicker.setCurrentHour(hour);
178 mTimePicker.setCurrentMinute(minute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180}