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