Home | History | Annotate | Download | only in app
      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.text.format.DateFormat;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.widget.TimePicker;
     27 import android.widget.TimePicker.OnTimeChangedListener;
     28 
     29 import com.android.internal.R;
     30 
     31 import java.util.Calendar;
     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}resources/tutorials/views/hello-timepicker.html">Time Picker
     37  * tutorial</a>.</p>
     38  */
     39 public class TimePickerDialog extends AlertDialog implements OnClickListener,
     40         OnTimeChangedListener {
     41 
     42     /**
     43      * The callback interface used to indicate the user is done filling in
     44      * the time (they clicked on the 'Set' button).
     45      */
     46     public interface OnTimeSetListener {
     47 
     48         /**
     49          * @param view The view associated with this listener.
     50          * @param hourOfDay The hour that was set.
     51          * @param minute The minute that was set.
     52          */
     53         void onTimeSet(TimePicker view, int hourOfDay, int minute);
     54     }
     55 
     56     private static final String HOUR = "hour";
     57     private static final String MINUTE = "minute";
     58     private static final String IS_24_HOUR = "is24hour";
     59 
     60     private final TimePicker mTimePicker;
     61     private final OnTimeSetListener mCallback;
     62     private final Calendar mCalendar;
     63     private final java.text.DateFormat mDateFormat;
     64 
     65     int mInitialHourOfDay;
     66     int mInitialMinute;
     67     boolean mIs24HourView;
     68 
     69     /**
     70      * @param context Parent.
     71      * @param callBack How parent is notified.
     72      * @param hourOfDay The initial hour.
     73      * @param minute The initial minute.
     74      * @param is24HourView Whether this is a 24 hour view, or AM/PM.
     75      */
     76     public TimePickerDialog(Context context,
     77             OnTimeSetListener callBack,
     78             int hourOfDay, int minute, boolean is24HourView) {
     79         this(context, com.android.internal.R.style.Theme_Dialog_Alert,
     80                 callBack, hourOfDay, minute, is24HourView);
     81     }
     82 
     83     /**
     84      * @param context Parent.
     85      * @param theme the theme to apply to this dialog
     86      * @param callBack How parent is notified.
     87      * @param hourOfDay The initial hour.
     88      * @param minute The initial minute.
     89      * @param is24HourView Whether this is a 24 hour view, or AM/PM.
     90      */
     91     public TimePickerDialog(Context context,
     92             int theme,
     93             OnTimeSetListener callBack,
     94             int hourOfDay, int minute, boolean is24HourView) {
     95         super(context, theme);
     96         mCallback = callBack;
     97         mInitialHourOfDay = hourOfDay;
     98         mInitialMinute = minute;
     99         mIs24HourView = is24HourView;
    100 
    101         mDateFormat = DateFormat.getTimeFormat(context);
    102         mCalendar = Calendar.getInstance();
    103         updateTitle(mInitialHourOfDay, mInitialMinute);
    104 
    105         setButton(context.getText(R.string.date_time_set), this);
    106         setButton2(context.getText(R.string.cancel), (OnClickListener) null);
    107         setIcon(R.drawable.ic_dialog_time);
    108 
    109         LayoutInflater inflater =
    110                 (LayoutInflater) context.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.setCurrentHour(mInitialHourOfDay);
    117         mTimePicker.setCurrentMinute(mInitialMinute);
    118         mTimePicker.setIs24HourView(mIs24HourView);
    119         mTimePicker.setOnTimeChangedListener(this);
    120     }
    121 
    122     public void onClick(DialogInterface dialog, int which) {
    123         if (mCallback != null) {
    124             mTimePicker.clearFocus();
    125             mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
    126                     mTimePicker.getCurrentMinute());
    127         }
    128     }
    129 
    130     public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
    131         updateTitle(hourOfDay, minute);
    132     }
    133 
    134     public void updateTime(int hourOfDay, int minutOfHour) {
    135         mTimePicker.setCurrentHour(hourOfDay);
    136         mTimePicker.setCurrentMinute(minutOfHour);
    137     }
    138 
    139     private void updateTitle(int hour, int minute) {
    140         mCalendar.set(Calendar.HOUR_OF_DAY, hour);
    141         mCalendar.set(Calendar.MINUTE, minute);
    142         setTitle(mDateFormat.format(mCalendar.getTime()));
    143     }
    144 
    145     @Override
    146     public Bundle onSaveInstanceState() {
    147         Bundle state = super.onSaveInstanceState();
    148         state.putInt(HOUR, mTimePicker.getCurrentHour());
    149         state.putInt(MINUTE, mTimePicker.getCurrentMinute());
    150         state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
    151         return state;
    152     }
    153 
    154     @Override
    155     public void onRestoreInstanceState(Bundle savedInstanceState) {
    156         super.onRestoreInstanceState(savedInstanceState);
    157         int hour = savedInstanceState.getInt(HOUR);
    158         int minute = savedInstanceState.getInt(MINUTE);
    159         mTimePicker.setCurrentHour(hour);
    160         mTimePicker.setCurrentMinute(minute);
    161         mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
    162         mTimePicker.setOnTimeChangedListener(this);
    163         updateTitle(hour, minute);
    164     }
    165 }
    166