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; 23 import android.util.TypedValue; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.widget.Button; 27 import android.widget.TimePicker; 28 import android.widget.TimePicker.OnTimeChangedListener; 29 import android.widget.TimePicker.ValidationCallback; 30 31 import com.android.internal.R; 32 33 /** 34 * A dialog that prompts the user for the time of day using a {@link TimePicker}. 35 * 36 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a> 37 * guide.</p> 38 */ 39 public class TimePickerDialog extends AlertDialog implements OnClickListener, 40 OnTimeChangedListener { 41 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; 47 private final OnTimeSetListener mTimeSetCallback; 48 49 private final int mInitialHourOfDay; 50 private final int mInitialMinute; 51 private final boolean mIs24HourView; 52 53 /** 54 * The callback interface used to indicate the user is done filling in 55 * the time (they clicked on the 'Done' button). 56 */ 57 public interface OnTimeSetListener { 58 59 /** 60 * @param view The view associated with this listener. 61 * @param hourOfDay The hour that was set. 62 * @param minute The minute that was set. 63 */ 64 void onTimeSet(TimePicker view, int hourOfDay, int minute); 65 } 66 67 /** 68 * @param context Parent. 69 * @param callBack How parent is notified. 70 * @param hourOfDay The initial hour. 71 * @param minute The initial minute. 72 * @param is24HourView Whether this is a 24 hour view, or AM/PM. 73 */ 74 public TimePickerDialog(Context context, 75 OnTimeSetListener callBack, 76 int hourOfDay, int minute, boolean is24HourView) { 77 this(context, 0, callBack, hourOfDay, minute, is24HourView); 78 } 79 80 static int resolveDialogTheme(Context context, int resid) { 81 if (resid == 0) { 82 final TypedValue outValue = new TypedValue(); 83 context.getTheme().resolveAttribute(R.attr.timePickerDialogTheme, outValue, true); 84 return outValue.resourceId; 85 } else { 86 return resid; 87 } 88 } 89 90 /** 91 * @param context Parent. 92 * @param theme the theme to apply to this dialog 93 * @param callBack How parent is notified. 94 * @param hourOfDay The initial hour. 95 * @param minute The initial minute. 96 * @param is24HourView Whether this is a 24 hour view, or AM/PM. 97 */ 98 public TimePickerDialog(Context context, int theme, OnTimeSetListener callBack, int hourOfDay, 99 int minute, boolean is24HourView) { 100 super(context, resolveDialogTheme(context, theme)); 101 102 mTimeSetCallback = callBack; 103 mInitialHourOfDay = hourOfDay; 104 mInitialMinute = minute; 105 mIs24HourView = is24HourView; 106 107 final Context themeContext = getContext(); 108 final LayoutInflater inflater = LayoutInflater.from(themeContext); 109 final View view = inflater.inflate(R.layout.time_picker_dialog, null); 110 setView(view); 111 setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this); 112 setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this); 113 setButtonPanelLayoutHint(LAYOUT_HINT_SIDE); 114 115 mTimePicker = (TimePicker) view.findViewById(R.id.timePicker); 116 mTimePicker.setIs24HourView(mIs24HourView); 117 mTimePicker.setCurrentHour(mInitialHourOfDay); 118 mTimePicker.setCurrentMinute(mInitialMinute); 119 mTimePicker.setOnTimeChangedListener(this); 120 mTimePicker.setValidationCallback(mValidationCallback); 121 } 122 123 @Override 124 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 125 /* do nothing */ 126 } 127 128 @Override 129 public void onClick(DialogInterface dialog, int which) { 130 switch (which) { 131 case BUTTON_POSITIVE: 132 if (mTimeSetCallback != null) { 133 mTimeSetCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(), 134 mTimePicker.getCurrentMinute()); 135 } 136 break; 137 } 138 } 139 140 /** 141 * Sets the current time. 142 * 143 * @param hourOfDay The current hour within the day. 144 * @param minuteOfHour The current minute within the hour. 145 */ 146 public void updateTime(int hourOfDay, int minuteOfHour) { 147 mTimePicker.setCurrentHour(hourOfDay); 148 mTimePicker.setCurrentMinute(minuteOfHour); 149 } 150 151 @Override 152 public Bundle onSaveInstanceState() { 153 final Bundle state = super.onSaveInstanceState(); 154 state.putInt(HOUR, mTimePicker.getCurrentHour()); 155 state.putInt(MINUTE, mTimePicker.getCurrentMinute()); 156 state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView()); 157 return state; 158 } 159 160 @Override 161 public void onRestoreInstanceState(Bundle savedInstanceState) { 162 super.onRestoreInstanceState(savedInstanceState); 163 final int hour = savedInstanceState.getInt(HOUR); 164 final int minute = savedInstanceState.getInt(MINUTE); 165 mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR)); 166 mTimePicker.setCurrentHour(hour); 167 mTimePicker.setCurrentMinute(minute); 168 } 169 170 private final ValidationCallback mValidationCallback = new ValidationCallback() { 171 @Override 172 public void onValidationChanged(boolean valid) { 173 final Button positive = getButton(BUTTON_POSITIVE); 174 if (positive != null) { 175 positive.setEnabled(valid); 176 } 177 } 178 }; 179 } 180