The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 19 | import android.annotation.TestApi; |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.DialogInterface; |
| 23 | import android.content.DialogInterface.OnClickListener; |
| 24 | import android.os.Bundle; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 25 | import android.util.TypedValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.view.LayoutInflater; |
| 27 | import android.view.View; |
| 28 | import android.widget.TimePicker; |
| 29 | import android.widget.TimePicker.OnTimeChangedListener; |
| 30 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 31 | import com.android.internal.R; |
| 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | /** |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 34 | * A dialog that prompts the user for the time of day using a |
| 35 | * {@link TimePicker}. |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 36 | * |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 37 | * <p> |
| 38 | * See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a> |
| 39 | * guide. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | */ |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 41 | public class TimePickerDialog extends AlertDialog implements OnClickListener, |
| 42 | OnTimeChangedListener { |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 43 | private static final String HOUR = "hour"; |
| 44 | private static final String MINUTE = "minute"; |
| 45 | private static final String IS_24_HOUR = "is24hour"; |
| 46 | |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 47 | @UnsupportedAppUsage |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 48 | private final TimePicker mTimePicker; |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 49 | private final OnTimeSetListener mTimeSetListener; |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 50 | |
| 51 | private final int mInitialHourOfDay; |
| 52 | private final int mInitialMinute; |
| 53 | private final boolean mIs24HourView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * The callback interface used to indicate the user is done filling in |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 57 | * the time (e.g. they clicked on the 'OK' button). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | */ |
| 59 | public interface OnTimeSetListener { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | /** |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 61 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | */ |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 68 | void onTimeSet(TimePicker view, int hourOfDay, int minute); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | } |
| 70 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | /** |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 72 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | */ |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 80 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 85 | static int resolveDialogTheme(Context context, int resId) { |
| 86 | if (resId == 0) { |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 87 | final TypedValue outValue = new TypedValue(); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 88 | context.getTheme().resolveAttribute(R.attr.timePickerDialogTheme, outValue, true); |
| 89 | return outValue.resourceId; |
| 90 | } else { |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 91 | return resId; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | /** |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 96 | * Creates a new time picker dialog with the specified theme. |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 97 | * <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 Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 103 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | * @param is24HourView Whether this is a 24 hour view, or AM/PM. |
| 110 | */ |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 111 | public TimePickerDialog(Context context, int themeResId, OnTimeSetListener listener, |
| 112 | int hourOfDay, int minute, boolean is24HourView) { |
| 113 | super(context, resolveDialogTheme(context, themeResId)); |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 114 | |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 115 | mTimeSetListener = listener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | mInitialHourOfDay = hourOfDay; |
| 117 | mInitialMinute = minute; |
| 118 | mIs24HourView = is24HourView; |
| 119 | |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 120 | final Context themeContext = getContext(); |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 121 | final LayoutInflater inflater = LayoutInflater.from(themeContext); |
| 122 | final View view = inflater.inflate(R.layout.time_picker_dialog, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | setView(view); |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 124 | setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this); |
| 125 | setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this); |
Craig Stout | fd4bc4c | 2014-08-13 12:50:50 -0700 | [diff] [blame] | 126 | setButtonPanelLayoutHint(LAYOUT_HINT_SIDE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 128 | mTimePicker = (TimePicker) view.findViewById(R.id.timePicker); |
Brian Muramatsu | 87879e8 | 2011-02-01 17:23:52 -0800 | [diff] [blame] | 129 | mTimePicker.setIs24HourView(mIs24HourView); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | mTimePicker.setCurrentHour(mInitialHourOfDay); |
| 131 | mTimePicker.setCurrentMinute(mInitialMinute); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | mTimePicker.setOnTimeChangedListener(this); |
| 133 | } |
Christian Mehlmauer | a471307 | 2010-06-25 20:03:42 +0200 | [diff] [blame] | 134 | |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 135 | /** |
| 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 Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 144 | @Override |
Svetoslav Ganov | 206316a | 2010-11-19 00:04:05 -0800 | [diff] [blame] | 145 | public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { |
| 146 | /* do nothing */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | } |
Christian Mehlmauer | a471307 | 2010-06-25 20:03:42 +0200 | [diff] [blame] | 148 | |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 149 | @Override |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 150 | 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 Liutikas | 553303f | 2017-05-31 15:10:25 -0700 | [diff] [blame] | 156 | TimePickerDialog.this.onClick(TimePickerDialog.this, BUTTON_POSITIVE); |
Tianran Li | 10864b0 | 2017-03-22 20:38:19 +0900 | [diff] [blame] | 157 | // Clearing focus forces the dialog to commit any pending |
| 158 | // changes, e.g. typed text in a NumberPicker. |
| 159 | mTimePicker.clearFocus(); |
Aurimas Liutikas | ab14d82 | 2017-01-24 17:46:10 -0800 | [diff] [blame] | 160 | dismiss(); |
| 161 | } |
| 162 | } |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | @Override |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 167 | public void onClick(DialogInterface dialog, int which) { |
| 168 | switch (which) { |
Aurimas Liutikas | 9a28a09 | 2017-03-13 16:24:36 -0700 | [diff] [blame] | 169 | 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 Viverette | d26276d | 2014-11-26 16:08:58 -0800 | [diff] [blame] | 178 | case BUTTON_NEGATIVE: |
| 179 | cancel(); |
| 180 | break; |
Svetoslav Ganov | fe41ce4e | 2012-04-02 20:31:05 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 184 | /** |
| 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 Ganov | fe41ce4e | 2012-04-02 20:31:05 -0700 | [diff] [blame] | 193 | } |
| 194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | @Override |
| 196 | public Bundle onSaveInstanceState() { |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 197 | final Bundle state = super.onSaveInstanceState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | state.putInt(HOUR, mTimePicker.getCurrentHour()); |
| 199 | state.putInt(MINUTE, mTimePicker.getCurrentMinute()); |
| 200 | state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView()); |
| 201 | return state; |
| 202 | } |
Christian Mehlmauer | a471307 | 2010-06-25 20:03:42 +0200 | [diff] [blame] | 203 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | @Override |
| 205 | public void onRestoreInstanceState(Bundle savedInstanceState) { |
| 206 | super.onRestoreInstanceState(savedInstanceState); |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 207 | final int hour = savedInstanceState.getInt(HOUR); |
| 208 | final int minute = savedInstanceState.getInt(MINUTE); |
Brian Muramatsu | 87879e8 | 2011-02-01 17:23:52 -0800 | [diff] [blame] | 209 | mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | mTimePicker.setCurrentHour(hour); |
| 211 | mTimePicker.setCurrentMinute(minute); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | } |