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