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