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 | |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | /** |
| 33 | * A dialog that prompts the user for the time of day using a {@link TimePicker}. |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 34 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 35 | * <p>See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a> |
| 36 | * guide.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | */ |
Svetoslav Ganov | 206316a | 2010-11-19 00:04:05 -0800 | [diff] [blame] | 38 | public class TimePickerDialog extends AlertDialog |
| 39 | implements OnClickListener, OnTimeChangedListener { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * The callback interface used to indicate the user is done filling in |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 43 | * the time (they clicked on the 'Done' button). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | */ |
| 45 | public interface OnTimeSetListener { |
| 46 | |
| 47 | /** |
| 48 | * @param view The view associated with this listener. |
| 49 | * @param hourOfDay The hour that was set. |
| 50 | * @param minute The minute that was set. |
| 51 | */ |
| 52 | void onTimeSet(TimePicker view, int hourOfDay, int minute); |
| 53 | } |
| 54 | |
| 55 | private static final String HOUR = "hour"; |
| 56 | private static final String MINUTE = "minute"; |
| 57 | private static final String IS_24_HOUR = "is24hour"; |
Christian Mehlmauer | a471307 | 2010-06-25 20:03:42 +0200 | [diff] [blame] | 58 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | private final TimePicker mTimePicker; |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 60 | private final OnTimeSetListener mTimeSetCallback; |
Christian Mehlmauer | a471307 | 2010-06-25 20:03:42 +0200 | [diff] [blame] | 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | int mInitialHourOfDay; |
| 63 | int mInitialMinute; |
| 64 | boolean mIs24HourView; |
| 65 | |
| 66 | /** |
| 67 | * @param context Parent. |
| 68 | * @param callBack How parent is notified. |
| 69 | * @param hourOfDay The initial hour. |
| 70 | * @param minute The initial minute. |
| 71 | * @param is24HourView Whether this is a 24 hour view, or AM/PM. |
| 72 | */ |
| 73 | public TimePickerDialog(Context context, |
| 74 | OnTimeSetListener callBack, |
| 75 | int hourOfDay, int minute, boolean is24HourView) { |
Svetoslav Ganov | bf80562 | 2010-12-17 16:00:18 -0800 | [diff] [blame] | 76 | this(context, 0, callBack, hourOfDay, minute, is24HourView); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 79 | static int resolveDialogTheme(Context context, int resid) { |
| 80 | if (resid == 0) { |
| 81 | TypedValue outValue = new TypedValue(); |
| 82 | context.getTheme().resolveAttribute(R.attr.timePickerDialogTheme, outValue, true); |
| 83 | return outValue.resourceId; |
| 84 | } else { |
| 85 | return resid; |
| 86 | } |
| 87 | } |
| 88 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | /** |
| 90 | * @param context Parent. |
| 91 | * @param theme the theme to apply to this dialog |
| 92 | * @param callBack How parent is notified. |
| 93 | * @param hourOfDay The initial hour. |
| 94 | * @param minute The initial minute. |
| 95 | * @param is24HourView Whether this is a 24 hour view, or AM/PM. |
| 96 | */ |
| 97 | public TimePickerDialog(Context context, |
| 98 | int theme, |
| 99 | OnTimeSetListener callBack, |
| 100 | int hourOfDay, int minute, boolean is24HourView) { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 101 | super(context, resolveDialogTheme(context, theme)); |
| 102 | mTimeSetCallback = callBack; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | mInitialHourOfDay = hourOfDay; |
| 104 | mInitialMinute = minute; |
| 105 | mIs24HourView = is24HourView; |
| 106 | |
Svetoslav Ganov | 9f086d8 | 2011-11-29 18:27:23 -0800 | [diff] [blame] | 107 | Context themeContext = getContext(); |
Christian Mehlmauer | a471307 | 2010-06-25 20:03:42 +0200 | [diff] [blame] | 108 | |
| 109 | LayoutInflater inflater = |
Svetoslav Ganov | 9f086d8 | 2011-11-29 18:27:23 -0800 | [diff] [blame] | 110 | (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | View view = inflater.inflate(R.layout.time_picker_dialog, null); |
| 112 | setView(view); |
| 113 | mTimePicker = (TimePicker) view.findViewById(R.id.timePicker); |
| 114 | |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 115 | // Initialize state |
| 116 | mTimePicker.setLegacyMode(false /* will show new UI */); |
| 117 | mTimePicker.setShowDoneButton(true); |
| 118 | mTimePicker.setDismissCallback(new TimePicker.TimePickerDismissCallback() { |
| 119 | @Override |
| 120 | public void dismiss(TimePicker view, boolean isCancel, int hourOfDay, int minute) { |
| 121 | if (!isCancel) { |
| 122 | mTimeSetCallback.onTimeSet(view, hourOfDay, minute); |
| 123 | } |
| 124 | TimePickerDialog.this.dismiss(); |
| 125 | } |
| 126 | }); |
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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | public void onClick(DialogInterface dialog, int which) { |
Svetoslav Ganov | fe41ce4e | 2012-04-02 20:31:05 -0700 | [diff] [blame] | 134 | tryNotifyTimeSet(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | } |
| 136 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | public void updateTime(int hourOfDay, int minutOfHour) { |
| 138 | mTimePicker.setCurrentHour(hourOfDay); |
| 139 | mTimePicker.setCurrentMinute(minutOfHour); |
| 140 | } |
| 141 | |
Svetoslav Ganov | 206316a | 2010-11-19 00:04:05 -0800 | [diff] [blame] | 142 | public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { |
| 143 | /* do nothing */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } |
Christian Mehlmauer | a471307 | 2010-06-25 20:03:42 +0200 | [diff] [blame] | 145 | |
Svetoslav Ganov | fe41ce4e | 2012-04-02 20:31:05 -0700 | [diff] [blame] | 146 | private void tryNotifyTimeSet() { |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 147 | if (mTimeSetCallback != null) { |
Svetoslav Ganov | fe41ce4e | 2012-04-02 20:31:05 -0700 | [diff] [blame] | 148 | mTimePicker.clearFocus(); |
Fabrice Di Meglio | eeff63a | 2013-08-05 12:07:24 -0700 | [diff] [blame] | 149 | mTimeSetCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(), |
Svetoslav Ganov | fe41ce4e | 2012-04-02 20:31:05 -0700 | [diff] [blame] | 150 | mTimePicker.getCurrentMinute()); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | protected void onStop() { |
| 156 | tryNotifyTimeSet(); |
| 157 | super.onStop(); |
| 158 | } |
| 159 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | @Override |
| 161 | public Bundle onSaveInstanceState() { |
| 162 | Bundle state = super.onSaveInstanceState(); |
| 163 | state.putInt(HOUR, mTimePicker.getCurrentHour()); |
| 164 | state.putInt(MINUTE, mTimePicker.getCurrentMinute()); |
| 165 | state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView()); |
| 166 | return state; |
| 167 | } |
Christian Mehlmauer | a471307 | 2010-06-25 20:03:42 +0200 | [diff] [blame] | 168 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | @Override |
| 170 | public void onRestoreInstanceState(Bundle savedInstanceState) { |
| 171 | super.onRestoreInstanceState(savedInstanceState); |
| 172 | int hour = savedInstanceState.getInt(HOUR); |
| 173 | int minute = savedInstanceState.getInt(MINUTE); |
Brian Muramatsu | 87879e8 | 2011-02-01 17:23:52 -0800 | [diff] [blame] | 174 | mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | mTimePicker.setCurrentHour(hour); |
| 176 | mTimePicker.setCurrentMinute(minute); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | } |
| 178 | } |