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 com.android.internal.R;
20
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.DialogInterface.OnClickListener;
24import android.os.Bundle;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.widget.TimePicker;
28import android.widget.TimePicker.OnTimeChangedListener;
29
30/**
31 * A dialog that prompts the user for the time of day using a {@link TimePicker}.
32 *
33 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
34 * guide.</p>
35 */
36public class TimePickerDialog extends AlertDialog
37        implements OnClickListener, OnTimeChangedListener {
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";
56
57    private final TimePicker mTimePicker;
58    private final OnTimeSetListener mCallback;
59
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) {
74        this(context, 0, callBack, hourOfDay, minute, is24HourView);
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
95        setIcon(0);
96        setTitle(R.string.time_picker_dialog_title);
97
98        Context themeContext = getContext();
99        setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_done), this);
100
101        LayoutInflater inflater =
102                (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
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
108        mTimePicker.setIs24HourView(mIs24HourView);
109        mTimePicker.setCurrentHour(mInitialHourOfDay);
110        mTimePicker.setCurrentMinute(mInitialMinute);
111        mTimePicker.setOnTimeChangedListener(this);
112    }
113
114    public void onClick(DialogInterface dialog, int which) {
115        tryNotifyTimeSet();
116    }
117
118    public void updateTime(int hourOfDay, int minutOfHour) {
119        mTimePicker.setCurrentHour(hourOfDay);
120        mTimePicker.setCurrentMinute(minutOfHour);
121    }
122
123    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
124        /* do nothing */
125    }
126
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
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    }
149
150    @Override
151    public void onRestoreInstanceState(Bundle savedInstanceState) {
152        super.onRestoreInstanceState(savedInstanceState);
153        int hour = savedInstanceState.getInt(HOUR);
154        int minute = savedInstanceState.getInt(MINUTE);
155        mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
156        mTimePicker.setCurrentHour(hour);
157        mTimePicker.setCurrentMinute(minute);
158    }
159}
160