TimePickerDialog.java revision d26276d9e39ed9d96102d41977de972d884bb7cd
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.Button;
27import android.widget.TimePicker;
28import android.widget.TimePicker.OnTimeChangedListener;
29import android.widget.TimePicker.ValidationCallback;
30
31import com.android.internal.R;
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}guide/topics/ui/controls/pickers.html">Pickers</a>
37 * guide.</p>
38 */
39public class TimePickerDialog extends AlertDialog implements OnClickListener,
40        OnTimeChangedListener {
41
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 mTimeSetCallback;
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 (they clicked on the 'Done' button).
56     */
57    public interface OnTimeSetListener {
58
59        /**
60         * @param view The view associated with this listener.
61         * @param hourOfDay The hour that was set.
62         * @param minute The minute that was set.
63         */
64        void onTimeSet(TimePicker view, int hourOfDay, int minute);
65    }
66
67    /**
68     * @param context Parent.
69     * @param callBack How parent is notified.
70     * @param hourOfDay The initial hour.
71     * @param minute The initial minute.
72     * @param is24HourView Whether this is a 24 hour view, or AM/PM.
73     */
74    public TimePickerDialog(Context context,
75            OnTimeSetListener callBack,
76            int hourOfDay, int minute, boolean is24HourView) {
77        this(context, 0, callBack, hourOfDay, minute, is24HourView);
78    }
79
80    static int resolveDialogTheme(Context context, int resid) {
81        if (resid == 0) {
82            final TypedValue outValue = new TypedValue();
83            context.getTheme().resolveAttribute(R.attr.timePickerDialogTheme, outValue, true);
84            return outValue.resourceId;
85        } else {
86            return resid;
87        }
88    }
89
90    /**
91     * @param context Parent.
92     * @param theme the theme to apply to this dialog
93     * @param callBack How parent is notified.
94     * @param hourOfDay The initial hour.
95     * @param minute The initial minute.
96     * @param is24HourView Whether this is a 24 hour view, or AM/PM.
97     */
98    public TimePickerDialog(Context context, int theme, OnTimeSetListener callBack, int hourOfDay,
99            int minute, boolean is24HourView) {
100        super(context, resolveDialogTheme(context, theme));
101
102        mTimeSetCallback = callBack;
103        mInitialHourOfDay = hourOfDay;
104        mInitialMinute = minute;
105        mIs24HourView = is24HourView;
106
107        final Context themeContext = getContext();
108        final LayoutInflater inflater = LayoutInflater.from(themeContext);
109        final View view = inflater.inflate(R.layout.time_picker_dialog, null);
110        setView(view);
111        setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this);
112        setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this);
113        setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);
114
115        mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
116        mTimePicker.setIs24HourView(mIs24HourView);
117        mTimePicker.setCurrentHour(mInitialHourOfDay);
118        mTimePicker.setCurrentMinute(mInitialMinute);
119        mTimePicker.setOnTimeChangedListener(this);
120        mTimePicker.setValidationCallback(mValidationCallback);
121    }
122
123    @Override
124    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
125        /* do nothing */
126    }
127
128    @Override
129    public void onClick(DialogInterface dialog, int which) {
130        switch (which) {
131            case BUTTON_POSITIVE:
132                if (mTimeSetCallback != null) {
133                    mTimeSetCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
134                            mTimePicker.getCurrentMinute());
135                }
136                break;
137            case BUTTON_NEGATIVE:
138                cancel();
139                break;
140        }
141    }
142
143    /**
144     * Sets the current time.
145     *
146     * @param hourOfDay The current hour within the day.
147     * @param minuteOfHour The current minute within the hour.
148     */
149    public void updateTime(int hourOfDay, int minuteOfHour) {
150        mTimePicker.setCurrentHour(hourOfDay);
151        mTimePicker.setCurrentMinute(minuteOfHour);
152    }
153
154    @Override
155    public Bundle onSaveInstanceState() {
156        final Bundle state = super.onSaveInstanceState();
157        state.putInt(HOUR, mTimePicker.getCurrentHour());
158        state.putInt(MINUTE, mTimePicker.getCurrentMinute());
159        state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
160        return state;
161    }
162
163    @Override
164    public void onRestoreInstanceState(Bundle savedInstanceState) {
165        super.onRestoreInstanceState(savedInstanceState);
166        final int hour = savedInstanceState.getInt(HOUR);
167        final int minute = savedInstanceState.getInt(MINUTE);
168        mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
169        mTimePicker.setCurrentHour(hour);
170        mTimePicker.setCurrentMinute(minute);
171    }
172
173    private final ValidationCallback mValidationCallback = new ValidationCallback() {
174        @Override
175        public void onValidationChanged(boolean valid) {
176            final Button positive = getButton(BUTTON_POSITIVE);
177            if (positive != null) {
178                positive.setEnabled(valid);
179            }
180        }
181    };
182}
183