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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.DialogInterface; |
| 22 | import android.content.DialogInterface.OnClickListener; |
| 23 | import android.os.Bundle; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 24 | import android.util.TypedValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.view.LayoutInflater; |
| 26 | import android.view.View; |
| 27 | import android.widget.TimePicker; |
| 28 | import android.widget.TimePicker.OnTimeChangedListener; |
| 29 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 30 | import com.android.internal.R; |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | /** |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 33 | * A dialog that prompts the user for the time of day using a |
| 34 | * {@link TimePicker}. |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 35 | * |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 36 | * <p> |
| 37 | * See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a> |
| 38 | * guide. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | */ |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 40 | public class TimePickerDialog extends AlertDialog implements OnClickListener, |
| 41 | OnTimeChangedListener { |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 42 | 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 Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 47 | private final OnTimeSetListener mTimeSetListener; |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 48 | |
| 49 | private final int mInitialHourOfDay; |
| 50 | private final int mInitialMinute; |
| 51 | private final boolean mIs24HourView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * The callback interface used to indicate the user is done filling in |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 55 | * 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] | 56 | */ |
| 57 | public interface OnTimeSetListener { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | /** |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 59 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | */ |
Alan Viverette | 4420ae8 | 2015-11-16 16:10:56 -0500 | [diff] [blame] | 66 | void onTimeSet(TimePicker view, int hourOfDay, int minute); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | } |
| 68 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | /** |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 70 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | */ |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 78 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 83 | static int resolveDialogTheme(Context context, int resId) { |
| 84 | if (resId == 0) { |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 85 | final TypedValue outValue = new TypedValue(); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 86 | context.getTheme().resolveAttribute(R.attr.timePickerDialogTheme, outValue, true); |
| 87 | return outValue.resourceId; |
| 88 | } else { |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 89 | return resId; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | /** |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 94 | * Creates a new time picker dialog with the specified theme. |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 95 | * <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 Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 101 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | * @param is24HourView Whether this is a 24 hour view, or AM/PM. |
| 108 | */ |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 109 | public TimePickerDialog(Context context, int themeResId, OnTimeSetListener listener, |
| 110 | int hourOfDay, int minute, boolean is24HourView) { |
| 111 | super(context, resolveDialogTheme(context, themeResId)); |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 112 | |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 113 | mTimeSetListener = listener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | mInitialHourOfDay = hourOfDay; |
| 115 | mInitialMinute = minute; |
| 116 | mIs24HourView = is24HourView; |
| 117 | |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 118 | final Context themeContext = getContext(); |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 119 | final LayoutInflater inflater = LayoutInflater.from(themeContext); |
| 120 | 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] | 121 | setView(view); |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 122 | setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this); |
| 123 | setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this); |
Craig Stout | fd4bc4c | 2014-08-13 12:50:50 -0700 | [diff] [blame] | 124 | setButtonPanelLayoutHint(LAYOUT_HINT_SIDE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | |
Alan Viverette | 5134478 | 2014-07-16 17:39:27 -0700 | [diff] [blame] | 126 | mTimePicker = (TimePicker) view.findViewById(R.id.timePicker); |
Brian Muramatsu | 87879e8 | 2011-02-01 17:23:52 -0800 | [diff] [blame] | 127 | mTimePicker.setIs24HourView(mIs24HourView); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | mTimePicker.setCurrentHour(mInitialHourOfDay); |
| 129 | mTimePicker.setCurrentMinute(mInitialMinute); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | mTimePicker.setOnTimeChangedListener(this); |
| 131 | } |
Christian Mehlmauer | a471307 | 2010-06-25 20:03:42 +0200 | [diff] [blame] | 132 | |
Alan Viverette | 8817aa9 | 2016-09-22 11:16:22 -0400 | [diff] [blame] | 133 | /** |
| 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 Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 142 | @Override |
Svetoslav Ganov | 206316a | 2010-11-19 00:04:05 -0800 | [diff] [blame] | 143 | public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { |
| 144 | /* do nothing */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } |
Christian Mehlmauer | a471307 | 2010-06-25 20:03:42 +0200 | [diff] [blame] | 146 | |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 147 | @Override |
| 148 | public void onClick(DialogInterface dialog, int which) { |
| 149 | switch (which) { |
| 150 | case BUTTON_POSITIVE: |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 151 | if (mTimeSetListener != null) { |
| 152 | mTimeSetListener.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(), |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 153 | mTimePicker.getCurrentMinute()); |
| 154 | } |
| 155 | break; |
Alan Viverette | d26276d | 2014-11-26 16:08:58 -0800 | [diff] [blame] | 156 | case BUTTON_NEGATIVE: |
| 157 | cancel(); |
| 158 | break; |
Svetoslav Ganov | fe41ce4e | 2012-04-02 20:31:05 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 162 | /** |
| 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 Ganov | fe41ce4e | 2012-04-02 20:31:05 -0700 | [diff] [blame] | 171 | } |
| 172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | @Override |
| 174 | public Bundle onSaveInstanceState() { |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 175 | final Bundle state = super.onSaveInstanceState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | state.putInt(HOUR, mTimePicker.getCurrentHour()); |
| 177 | state.putInt(MINUTE, mTimePicker.getCurrentMinute()); |
| 178 | state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView()); |
| 179 | return state; |
| 180 | } |
Christian Mehlmauer | a471307 | 2010-06-25 20:03:42 +0200 | [diff] [blame] | 181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | @Override |
| 183 | public void onRestoreInstanceState(Bundle savedInstanceState) { |
| 184 | super.onRestoreInstanceState(savedInstanceState); |
Alan Viverette | 518ff0d | 2014-08-15 14:20:35 -0700 | [diff] [blame] | 185 | final int hour = savedInstanceState.getInt(HOUR); |
| 186 | final int minute = savedInstanceState.getInt(MINUTE); |
Brian Muramatsu | 87879e8 | 2011-02-01 17:23:52 -0800 | [diff] [blame] | 187 | mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | mTimePicker.setCurrentHour(hour); |
| 189 | mTimePicker.setCurrentMinute(minute); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | } |