blob: 1b281d521957a517c0f34ac747cb37555430cd04 [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;
Mathew Inwood4fb17d12018-08-14 14:25:44 +010020import android.annotation.UnsupportedAppUsage;
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;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070025import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.view.LayoutInflater;
27import android.view.View;
28import android.widget.TimePicker;
29import android.widget.TimePicker.OnTimeChangedListener;
30
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070031import com.android.internal.R;
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033/**
Alan Viverette62c79e92015-02-26 09:47:10 -080034 * A dialog that prompts the user for the time of day using a
35 * {@link TimePicker}.
Scott Main41ec6532010-08-19 16:57:07 -070036 *
Alan Viverette62c79e92015-02-26 09:47:10 -080037 * <p>
38 * See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
39 * guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 */
Alan Viverette518ff0d2014-08-15 14:20:35 -070041public class TimePickerDialog extends AlertDialog implements OnClickListener,
42 OnTimeChangedListener {
Alan Viverette518ff0d2014-08-15 14:20:35 -070043 private static final String HOUR = "hour";
44 private static final String MINUTE = "minute";
45 private static final String IS_24_HOUR = "is24hour";
46
Mathew Inwood4fb17d12018-08-14 14:25:44 +010047 @UnsupportedAppUsage
Alan Viverette518ff0d2014-08-15 14:20:35 -070048 private final TimePicker mTimePicker;
Alan Viverette62c79e92015-02-26 09:47:10 -080049 private final OnTimeSetListener mTimeSetListener;
Alan Viverette518ff0d2014-08-15 14:20:35 -070050
51 private final int mInitialHourOfDay;
52 private final int mInitialMinute;
53 private final boolean mIs24HourView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55 /**
56 * The callback interface used to indicate the user is done filling in
Alan Viverette62c79e92015-02-26 09:47:10 -080057 * the time (e.g. they clicked on the 'OK' button).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 */
59 public interface OnTimeSetListener {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 /**
Alan Viverette62c79e92015-02-26 09:47:10 -080061 * Called when the user is done setting a new time and the dialog has
62 * closed.
63 *
64 * @param view the view associated with this listener
65 * @param hourOfDay the hour that was set
66 * @param minute the minute that was set
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 */
Alan Viverette4420ae82015-11-16 16:10:56 -050068 void onTimeSet(TimePicker view, int hourOfDay, int minute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 }
70
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 /**
Alan Viverette62c79e92015-02-26 09:47:10 -080072 * Creates a new time picker dialog.
73 *
74 * @param context the parent context
75 * @param listener the listener to call when the time is set
76 * @param hourOfDay the initial hour
77 * @param minute the initial minute
78 * @param is24HourView whether this is a 24 hour view or AM/PM
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 */
Alan Viverette62c79e92015-02-26 09:47:10 -080080 public TimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute,
81 boolean is24HourView) {
82 this(context, 0, listener, hourOfDay, minute, is24HourView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 }
84
Alan Viverette62c79e92015-02-26 09:47:10 -080085 static int resolveDialogTheme(Context context, int resId) {
86 if (resId == 0) {
Alan Viverette518ff0d2014-08-15 14:20:35 -070087 final TypedValue outValue = new TypedValue();
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070088 context.getTheme().resolveAttribute(R.attr.timePickerDialogTheme, outValue, true);
89 return outValue.resourceId;
90 } else {
Alan Viverette62c79e92015-02-26 09:47:10 -080091 return resId;
Fabrice Di Meglioeeff63a2013-08-05 12:07:24 -070092 }
93 }
94
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 /**
Alan Viverette62c79e92015-02-26 09:47:10 -080096 * Creates a new time picker dialog with the specified theme.
Alan Viverette8817aa92016-09-22 11:16:22 -040097 * <p>
98 * The theme is overlaid on top of the theme of the parent {@code context}.
99 * If {@code themeResId} is 0, the dialog will be inflated using the theme
100 * specified by the
101 * {@link android.R.attr#timePickerDialogTheme android:timePickerDialogTheme}
102 * attribute on the parent {@code context}'s theme.
Alan Viverette62c79e92015-02-26 09:47:10 -0800103 *
104 * @param context the parent context
105 * @param themeResId the resource ID of the theme to apply to this dialog
106 * @param listener the listener to call when the time is set
107 * @param hourOfDay the initial hour
108 * @param minute the initial minute
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * @param is24HourView Whether this is a 24 hour view, or AM/PM.
110 */
Alan Viverette62c79e92015-02-26 09:47:10 -0800111 public TimePickerDialog(Context context, int themeResId, OnTimeSetListener listener,
112 int hourOfDay, int minute, boolean is24HourView) {
113 super(context, resolveDialogTheme(context, themeResId));
Alan Viverette51344782014-07-16 17:39:27 -0700114
Alan Viverette62c79e92015-02-26 09:47:10 -0800115 mTimeSetListener = listener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 mInitialHourOfDay = hourOfDay;
117 mInitialMinute = minute;
118 mIs24HourView = is24HourView;
119
Alan Viverette51344782014-07-16 17:39:27 -0700120 final Context themeContext = getContext();
Alan Viverette51344782014-07-16 17:39:27 -0700121 final LayoutInflater inflater = LayoutInflater.from(themeContext);
122 final View view = inflater.inflate(R.layout.time_picker_dialog, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 setView(view);
Alan Viverette518ff0d2014-08-15 14:20:35 -0700124 setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this);
125 setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this);
Craig Stoutfd4bc4c2014-08-13 12:50:50 -0700126 setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
Alan Viverette51344782014-07-16 17:39:27 -0700128 mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
Brian Muramatsu87879e82011-02-01 17:23:52 -0800129 mTimePicker.setIs24HourView(mIs24HourView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 mTimePicker.setCurrentHour(mInitialHourOfDay);
131 mTimePicker.setCurrentMinute(mInitialMinute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 mTimePicker.setOnTimeChangedListener(this);
133 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200134
Alan Viverette8817aa92016-09-22 11:16:22 -0400135 /**
136 * @return the time picker displayed in the dialog
137 * @hide For testing only.
138 */
139 @TestApi
140 public TimePicker getTimePicker() {
141 return mTimePicker;
142 }
143
Alan Viverette518ff0d2014-08-15 14:20:35 -0700144 @Override
Svetoslav Ganov206316a2010-11-19 00:04:05 -0800145 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
146 /* do nothing */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200148
Alan Viverette518ff0d2014-08-15 14:20:35 -0700149 @Override
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800150 public void show() {
151 super.show();
152 getButton(BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
153 @Override
154 public void onClick(View view) {
155 if (mTimePicker.validateInput()) {
Aurimas Liutikas553303f2017-05-31 15:10:25 -0700156 TimePickerDialog.this.onClick(TimePickerDialog.this, BUTTON_POSITIVE);
Tianran Li10864b02017-03-22 20:38:19 +0900157 // Clearing focus forces the dialog to commit any pending
158 // changes, e.g. typed text in a NumberPicker.
159 mTimePicker.clearFocus();
Aurimas Liutikasab14d822017-01-24 17:46:10 -0800160 dismiss();
161 }
162 }
163 });
164 }
165
166 @Override
Alan Viverette518ff0d2014-08-15 14:20:35 -0700167 public void onClick(DialogInterface dialog, int which) {
168 switch (which) {
Aurimas Liutikas9a28a092017-03-13 16:24:36 -0700169 case BUTTON_POSITIVE:
170 // Note this skips input validation and just uses the last valid time and hour
171 // entry. This will only be invoked programmatically. User clicks on BUTTON_POSITIVE
172 // are handled in show().
173 if (mTimeSetListener != null) {
174 mTimeSetListener.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
175 mTimePicker.getCurrentMinute());
176 }
177 break;
Alan Viveretted26276d2014-11-26 16:08:58 -0800178 case BUTTON_NEGATIVE:
179 cancel();
180 break;
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -0700181 }
182 }
183
Alan Viverette518ff0d2014-08-15 14:20:35 -0700184 /**
185 * Sets the current time.
186 *
187 * @param hourOfDay The current hour within the day.
188 * @param minuteOfHour The current minute within the hour.
189 */
190 public void updateTime(int hourOfDay, int minuteOfHour) {
191 mTimePicker.setCurrentHour(hourOfDay);
192 mTimePicker.setCurrentMinute(minuteOfHour);
Svetoslav Ganovfe41ce4e2012-04-02 20:31:05 -0700193 }
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 @Override
196 public Bundle onSaveInstanceState() {
Alan Viverette518ff0d2014-08-15 14:20:35 -0700197 final Bundle state = super.onSaveInstanceState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 state.putInt(HOUR, mTimePicker.getCurrentHour());
199 state.putInt(MINUTE, mTimePicker.getCurrentMinute());
200 state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
201 return state;
202 }
Christian Mehlmauera4713072010-06-25 20:03:42 +0200203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 @Override
205 public void onRestoreInstanceState(Bundle savedInstanceState) {
206 super.onRestoreInstanceState(savedInstanceState);
Alan Viverette518ff0d2014-08-15 14:20:35 -0700207 final int hour = savedInstanceState.getInt(HOUR);
208 final int minute = savedInstanceState.getInt(MINUTE);
Brian Muramatsu87879e82011-02-01 17:23:52 -0800209 mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 mTimePicker.setCurrentHour(hour);
211 mTimePicker.setCurrentMinute(minute);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213}