blob: 3f467a01bee2a6cca3462575d973b4e159c42994 [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
Alan Viverette8817aa92016-09-22 11:16:22 -040019import android.annotation.TestApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
21import android.content.DialogInterface;
22import android.content.DialogInterface.OnClickListener;
23import android.os.Bundle;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070024import android.util.TypedValue;
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
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070030import com.android.internal.R;
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032/**
Alan Viverette62c79e92015-02-26 09:47:10 -080033 * A dialog that prompts the user for the time of day using a
34 * {@link TimePicker}.
Scott Main41ec6532010-08-19 16:57:07 -070035 *
Alan Viverette62c79e92015-02-26 09:47:10 -080036 * <p>
37 * See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
38 * guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 */
Alan Viverette518ff0d2014-08-15 14:20:35 -070040public class TimePickerDialog extends AlertDialog implements OnClickListener,
41 OnTimeChangedListener {
Alan Viverette518ff0d2014-08-15 14:20:35 -070042 private static final String HOUR = "hour";
43 private static final String MINUTE = "minute";
44 private static final String IS_24_HOUR = "is24hour";
45
46 private final TimePicker mTimePicker;
Alan Viverette62c79e92015-02-26 09:47:10 -080047 private final OnTimeSetListener mTimeSetListener;
Alan Viverette518ff0d2014-08-15 14:20:35 -070048
49 private final int mInitialHourOfDay;
50 private final int mInitialMinute;
51 private final boolean mIs24HourView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53 /**
54 * The callback interface used to indicate the user is done filling in
Alan Viverette62c79e92015-02-26 09:47:10 -080055 * the time (e.g. they clicked on the 'OK' button).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 */
57 public interface OnTimeSetListener {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 /**
Alan Viverette62c79e92015-02-26 09:47:10 -080059 * Called when the user is done setting a new time and the dialog has
60 * closed.
61 *
62 * @param view the view associated with this listener
63 * @param hourOfDay the hour that was set
64 * @param minute the minute that was set
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 */
Alan Viverette4420ae82015-11-16 16:10:56 -050066 void onTimeSet(TimePicker view, int hourOfDay, int minute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 }
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 /**
Alan Viverette62c79e92015-02-26 09:47:10 -080070 * Creates a new time picker dialog.
71 *
72 * @param context the parent context
73 * @param listener the listener to call when the time is set
74 * @param hourOfDay the initial hour
75 * @param minute the initial minute
76 * @param is24HourView whether this is a 24 hour view or AM/PM
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 */
Alan Viverette62c79e92015-02-26 09:47:10 -080078 public TimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute,
79 boolean is24HourView) {
80 this(context, 0, listener, hourOfDay, minute, is24HourView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 }
82
Alan Viverette62c79e92015-02-26 09:47:10 -080083 static int resolveDialogTheme(Context context, int resId) {
84 if (resId == 0) {
Alan Viverette518ff0d2014-08-15 14:20:35 -070085 final TypedValue outValue = new TypedValue();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070086 context.getTheme().resolveAttribute(R.attr.timePickerDialogTheme, outValue, true);
87 return outValue.resourceId;
88 } else {
Alan Viverette62c79e92015-02-26 09:47:10 -080089 return resId;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070090 }
91 }
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 /**
Alan Viverette62c79e92015-02-26 09:47:10 -080094 * Creates a new time picker dialog with the specified theme.
Alan Viverette8817aa92016-09-22 11:16:22 -040095 * <p>
96 * The theme is overlaid on top of the theme of the parent {@code context}.
97 * If {@code themeResId} is 0, the dialog will be inflated using the theme
98 * specified by the
99 * {@link android.R.attr#timePickerDialogTheme android:timePickerDialogTheme}
100 * attribute on the parent {@code context}'s theme.
Alan Viverette62c79e92015-02-26 09:47:10 -0800101 *
102 * @param context the parent context
103 * @param themeResId the resource ID of the theme to apply to this dialog
104 * @param listener the listener to call when the time is set
105 * @param hourOfDay the initial hour
106 * @param minute the initial minute
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * @param is24HourView Whether this is a 24 hour view, or AM/PM.
108 */
Alan Viverette62c79e92015-02-26 09:47:10 -0800109 public TimePickerDialog(Context context, int themeResId, OnTimeSetListener listener,
110 int hourOfDay, int minute, boolean is24HourView) {
111 super(context, resolveDialogTheme(context, themeResId));
Alan Viverette51344782014-07-16 17:39:27 -0700112
Alan Viverette62c79e92015-02-26 09:47:10 -0800113 mTimeSetListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 mInitialHourOfDay = hourOfDay;
115 mInitialMinute = minute;
116 mIs24HourView = is24HourView;
117
Alan Viverette51344782014-07-16 17:39:27 -0700118 final Context themeContext = getContext();
Alan Viverette51344782014-07-16 17:39:27 -0700119 final LayoutInflater inflater = LayoutInflater.from(themeContext);
120 final View view = inflater.inflate(R.layout.time_picker_dialog, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 setView(view);
Alan Viverette518ff0d2014-08-15 14:20:35 -0700122 setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this);
123 setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this);
Craig Stoutfd4bc4c2014-08-13 12:50:50 -0700124 setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
Alan Viverette51344782014-07-16 17:39:27 -0700126 mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
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
Alan Viverette8817aa92016-09-22 11:16:22 -0400133 /**
134 * @return the time picker displayed in the dialog
135 * @hide For testing only.
136 */
137 @TestApi
138 public TimePicker getTimePicker() {
139 return mTimePicker;
140 }
141
Alan Viverette518ff0d2014-08-15 14:20:35 -0700142 @Override
Svetoslav Ganov206316a2010-11-19 00:04:05 -0800143 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
144 /* do nothing */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200146
Alan Viverette518ff0d2014-08-15 14:20:35 -0700147 @Override
148 public void onClick(DialogInterface dialog, int which) {
149 switch (which) {
150 case BUTTON_POSITIVE:
Alan Viverette62c79e92015-02-26 09:47:10 -0800151 if (mTimeSetListener != null) {
152 mTimeSetListener.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
Alan Viverette518ff0d2014-08-15 14:20:35 -0700153 mTimePicker.getCurrentMinute());
154 }
155 break;
Alan Viveretted26276d2014-11-26 16:08:58 -0800156 case BUTTON_NEGATIVE:
157 cancel();
158 break;
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -0700159 }
160 }
161
Alan Viverette518ff0d2014-08-15 14:20:35 -0700162 /**
163 * Sets the current time.
164 *
165 * @param hourOfDay The current hour within the day.
166 * @param minuteOfHour The current minute within the hour.
167 */
168 public void updateTime(int hourOfDay, int minuteOfHour) {
169 mTimePicker.setCurrentHour(hourOfDay);
170 mTimePicker.setCurrentMinute(minuteOfHour);
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -0700171 }
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 @Override
174 public Bundle onSaveInstanceState() {
Alan Viverette518ff0d2014-08-15 14:20:35 -0700175 final Bundle state = super.onSaveInstanceState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 state.putInt(HOUR, mTimePicker.getCurrentHour());
177 state.putInt(MINUTE, mTimePicker.getCurrentMinute());
178 state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
179 return state;
180 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 @Override
183 public void onRestoreInstanceState(Bundle savedInstanceState) {
184 super.onRestoreInstanceState(savedInstanceState);
Alan Viverette518ff0d2014-08-15 14:20:35 -0700185 final int hour = savedInstanceState.getInt(HOUR);
186 final int minute = savedInstanceState.getInt(MINUTE);
Brian Muramatsu87879e82011-02-01 17:23:52 -0800187 mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 mTimePicker.setCurrentHour(hour);
189 mTimePicker.setCurrentMinute(minute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191}