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.annotation.TestApi;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.content.DialogInterface.OnClickListener;
23import android.os.Bundle;
24import android.util.TypedValue;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.widget.TimePicker;
28import android.widget.TimePicker.OnTimeChangedListener;
29
30import com.android.internal.R;
31
32/**
33 * A dialog that prompts the user for the time of day using a
34 * {@link TimePicker}.
35 *
36 * <p>
37 * See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
38 * guide.
39 */
40public class TimePickerDialog extends AlertDialog implements OnClickListener,
41        OnTimeChangedListener {
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 mTimeSetListener;
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 (e.g. they clicked on the 'OK' button).
56     */
57    public interface OnTimeSetListener {
58        /**
59         * Called when the user is done setting a new time and the dialog has
60         * closed.
61         *
62         * @param view the view associated with this listener
63         * @param hourOfDay the hour that was set
64         * @param minute the minute that was set
65         */
66        void onTimeSet(TimePicker view, int hourOfDay, int minute);
67    }
68
69    /**
70     * Creates a new time picker dialog.
71     *
72     * @param context the parent context
73     * @param listener the listener to call when the time is set
74     * @param hourOfDay the initial hour
75     * @param minute the initial minute
76     * @param is24HourView whether this is a 24 hour view or AM/PM
77     */
78    public TimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute,
79            boolean is24HourView) {
80        this(context, 0, listener, hourOfDay, minute, is24HourView);
81    }
82
83    static int resolveDialogTheme(Context context, int resId) {
84        if (resId == 0) {
85            final TypedValue outValue = new TypedValue();
86            context.getTheme().resolveAttribute(R.attr.timePickerDialogTheme, outValue, true);
87            return outValue.resourceId;
88        } else {
89            return resId;
90        }
91    }
92
93    /**
94     * Creates a new time picker dialog with the specified theme.
95     * <p>
96     * The theme is overlaid on top of the theme of the parent {@code context}.
97     * If {@code themeResId} is 0, the dialog will be inflated using the theme
98     * specified by the
99     * {@link android.R.attr#timePickerDialogTheme android:timePickerDialogTheme}
100     * attribute on the parent {@code context}'s theme.
101     *
102     * @param context the parent context
103     * @param themeResId the resource ID of the theme to apply to this dialog
104     * @param listener the listener to call when the time is set
105     * @param hourOfDay the initial hour
106     * @param minute the initial minute
107     * @param is24HourView Whether this is a 24 hour view, or AM/PM.
108     */
109    public TimePickerDialog(Context context, int themeResId, OnTimeSetListener listener,
110            int hourOfDay, int minute, boolean is24HourView) {
111        super(context, resolveDialogTheme(context, themeResId));
112
113        mTimeSetListener = listener;
114        mInitialHourOfDay = hourOfDay;
115        mInitialMinute = minute;
116        mIs24HourView = is24HourView;
117
118        final Context themeContext = getContext();
119        final LayoutInflater inflater = LayoutInflater.from(themeContext);
120        final View view = inflater.inflate(R.layout.time_picker_dialog, null);
121        setView(view);
122        setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this);
123        setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this);
124        setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);
125
126        mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
127        mTimePicker.setIs24HourView(mIs24HourView);
128        mTimePicker.setCurrentHour(mInitialHourOfDay);
129        mTimePicker.setCurrentMinute(mInitialMinute);
130        mTimePicker.setOnTimeChangedListener(this);
131    }
132
133    /**
134     * @return the time picker displayed in the dialog
135     * @hide For testing only.
136     */
137    @TestApi
138    public TimePicker getTimePicker() {
139        return mTimePicker;
140    }
141
142    @Override
143    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
144        /* do nothing */
145    }
146
147    @Override
148    public void show() {
149        super.show();
150        getButton(BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
151            @Override
152            public void onClick(View view) {
153                if (mTimePicker.validateInput()) {
154                    TimePickerDialog.this.onClick(TimePickerDialog.this, BUTTON_POSITIVE);
155                    // Clearing focus forces the dialog to commit any pending
156                    // changes, e.g. typed text in a NumberPicker.
157                    mTimePicker.clearFocus();
158                    dismiss();
159                }
160            }
161        });
162    }
163
164    @Override
165    public void onClick(DialogInterface dialog, int which) {
166        switch (which) {
167            case BUTTON_POSITIVE:
168                // Note this skips input validation and just uses the last valid time and hour
169                // entry. This will only be invoked programmatically. User clicks on BUTTON_POSITIVE
170                // are handled in show().
171                if (mTimeSetListener != null) {
172                    mTimeSetListener.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
173                            mTimePicker.getCurrentMinute());
174                }
175                break;
176            case BUTTON_NEGATIVE:
177                cancel();
178                break;
179        }
180    }
181
182    /**
183     * Sets the current time.
184     *
185     * @param hourOfDay The current hour within the day.
186     * @param minuteOfHour The current minute within the hour.
187     */
188    public void updateTime(int hourOfDay, int minuteOfHour) {
189        mTimePicker.setCurrentHour(hourOfDay);
190        mTimePicker.setCurrentMinute(minuteOfHour);
191    }
192
193    @Override
194    public Bundle onSaveInstanceState() {
195        final Bundle state = super.onSaveInstanceState();
196        state.putInt(HOUR, mTimePicker.getCurrentHour());
197        state.putInt(MINUTE, mTimePicker.getCurrentMinute());
198        state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
199        return state;
200    }
201
202    @Override
203    public void onRestoreInstanceState(Bundle savedInstanceState) {
204        super.onRestoreInstanceState(savedInstanceState);
205        final int hour = savedInstanceState.getInt(HOUR);
206        final int minute = savedInstanceState.getInt(MINUTE);
207        mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
208        mTimePicker.setCurrentHour(hour);
209        mTimePicker.setCurrentMinute(minute);
210    }
211}
212