TimePickerDialog.java revision eeff63a5c347f282b5c8c3ae6a0924bf03fbce8f
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 17package android.app; 18 19import android.content.Context; 20import android.content.DialogInterface; 21import android.content.DialogInterface.OnClickListener; 22import android.os.Bundle; 23import android.util.TypedValue; 24import android.view.LayoutInflater; 25import android.view.View; 26import android.widget.TimePicker; 27import android.widget.TimePicker.OnTimeChangedListener; 28 29import com.android.internal.R; 30 31 32/** 33 * A dialog that prompts the user for the time of day using a {@link TimePicker}. 34 * 35 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a> 36 * guide.</p> 37 */ 38public class TimePickerDialog extends AlertDialog 39 implements OnClickListener, OnTimeChangedListener { 40 41 /** 42 * The callback interface used to indicate the user is done filling in 43 * the time (they clicked on the 'Done' button). 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"; 58 59 private final TimePicker mTimePicker; 60 private final OnTimeSetListener mTimeSetCallback; 61 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) { 76 this(context, 0, callBack, hourOfDay, minute, is24HourView); 77 } 78 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 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) { 101 super(context, resolveDialogTheme(context, theme)); 102 mTimeSetCallback = callBack; 103 mInitialHourOfDay = hourOfDay; 104 mInitialMinute = minute; 105 mIs24HourView = is24HourView; 106 107 Context themeContext = getContext(); 108 109 LayoutInflater inflater = 110 (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 111 View view = inflater.inflate(R.layout.time_picker_dialog, null); 112 setView(view); 113 mTimePicker = (TimePicker) view.findViewById(R.id.timePicker); 114 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 }); 127 mTimePicker.setIs24HourView(mIs24HourView); 128 mTimePicker.setCurrentHour(mInitialHourOfDay); 129 mTimePicker.setCurrentMinute(mInitialMinute); 130 mTimePicker.setOnTimeChangedListener(this); 131 } 132 133 public void onClick(DialogInterface dialog, int which) { 134 tryNotifyTimeSet(); 135 } 136 137 public void updateTime(int hourOfDay, int minutOfHour) { 138 mTimePicker.setCurrentHour(hourOfDay); 139 mTimePicker.setCurrentMinute(minutOfHour); 140 } 141 142 public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 143 /* do nothing */ 144 } 145 146 private void tryNotifyTimeSet() { 147 if (mTimeSetCallback != null) { 148 mTimePicker.clearFocus(); 149 mTimeSetCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(), 150 mTimePicker.getCurrentMinute()); 151 } 152 } 153 154 @Override 155 protected void onStop() { 156 tryNotifyTimeSet(); 157 super.onStop(); 158 } 159 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 } 168 169 @Override 170 public void onRestoreInstanceState(Bundle savedInstanceState) { 171 super.onRestoreInstanceState(savedInstanceState); 172 int hour = savedInstanceState.getInt(HOUR); 173 int minute = savedInstanceState.getInt(MINUTE); 174 mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR)); 175 mTimePicker.setCurrentHour(hour); 176 mTimePicker.setCurrentMinute(minute); 177 } 178} 179