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.text.format.DateUtils;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.widget.DatePicker;
27import android.widget.DatePicker.OnDateChangedListener;
28
29import com.android.internal.R;
30
31import java.util.Calendar;
32
33/**
34 * A simple dialog containing an {@link android.widget.DatePicker}.
35 *
36 * <p>See the <a href="{@docRoot}guide/topics/ui/controls/pickers.html">Pickers</a>
37 * guide.</p>
38 */
39public class DatePickerDialog extends AlertDialog implements OnClickListener,
40        OnDateChangedListener {
41
42    private static final String YEAR = "year";
43    private static final String MONTH = "month";
44    private static final String DAY = "day";
45
46    private final DatePicker mDatePicker;
47    private final OnDateSetListener mCallBack;
48    private final Calendar mCalendar;
49
50    private boolean mTitleNeedsUpdate = true;
51
52    /**
53     * The callback used to indicate the user is done filling in the date.
54     */
55    public interface OnDateSetListener {
56
57        /**
58         * @param view The view associated with this listener.
59         * @param year The year that was set.
60         * @param monthOfYear The month that was set (0-11) for compatibility
61         *  with {@link java.util.Calendar}.
62         * @param dayOfMonth The day of the month that was set.
63         */
64        void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth);
65    }
66
67    /**
68     * @param context The context the dialog is to run in.
69     * @param callBack How the parent is notified that the date is set.
70     * @param year The initial year of the dialog.
71     * @param monthOfYear The initial month of the dialog.
72     * @param dayOfMonth The initial day of the dialog.
73     */
74    public DatePickerDialog(Context context,
75            OnDateSetListener callBack,
76            int year,
77            int monthOfYear,
78            int dayOfMonth) {
79        this(context, 0, callBack, year, monthOfYear, dayOfMonth);
80    }
81
82    /**
83     * @param context The context the dialog is to run in.
84     * @param theme the theme to apply to this dialog
85     * @param callBack How the parent is notified that the date is set.
86     * @param year The initial year of the dialog.
87     * @param monthOfYear The initial month of the dialog.
88     * @param dayOfMonth The initial day of the dialog.
89     */
90    public DatePickerDialog(Context context,
91            int theme,
92            OnDateSetListener callBack,
93            int year,
94            int monthOfYear,
95            int dayOfMonth) {
96        super(context, theme);
97
98        mCallBack = callBack;
99
100        mCalendar = Calendar.getInstance();
101
102        Context themeContext = getContext();
103        setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_done), this);
104        setIcon(0);
105
106        LayoutInflater inflater =
107                (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
108        View view = inflater.inflate(R.layout.date_picker_dialog, null);
109        setView(view);
110        mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);
111        mDatePicker.init(year, monthOfYear, dayOfMonth, this);
112        updateTitle(year, monthOfYear, dayOfMonth);
113    }
114
115    public void onClick(DialogInterface dialog, int which) {
116        tryNotifyDateSet();
117    }
118
119    public void onDateChanged(DatePicker view, int year,
120            int month, int day) {
121        mDatePicker.init(year, month, day, this);
122        updateTitle(year, month, day);
123    }
124
125    /**
126     * Gets the {@link DatePicker} contained in this dialog.
127     *
128     * @return The calendar view.
129     */
130    public DatePicker getDatePicker() {
131        return mDatePicker;
132    }
133
134    /**
135     * Sets the current date.
136     *
137     * @param year The date year.
138     * @param monthOfYear The date month.
139     * @param dayOfMonth The date day of month.
140     */
141    public void updateDate(int year, int monthOfYear, int dayOfMonth) {
142        mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
143    }
144
145    private void tryNotifyDateSet() {
146        if (mCallBack != null) {
147            mDatePicker.clearFocus();
148            mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(),
149                    mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
150        }
151    }
152
153    @Override
154    protected void onStop() {
155        tryNotifyDateSet();
156        super.onStop();
157    }
158
159    private void updateTitle(int year, int month, int day) {
160        if (!mDatePicker.getCalendarViewShown()) {
161            mCalendar.set(Calendar.YEAR, year);
162            mCalendar.set(Calendar.MONTH, month);
163            mCalendar.set(Calendar.DAY_OF_MONTH, day);
164            String title = DateUtils.formatDateTime(mContext,
165                    mCalendar.getTimeInMillis(),
166                    DateUtils.FORMAT_SHOW_DATE
167                    | DateUtils.FORMAT_SHOW_WEEKDAY
168                    | DateUtils.FORMAT_SHOW_YEAR
169                    | DateUtils.FORMAT_ABBREV_MONTH
170                    | DateUtils.FORMAT_ABBREV_WEEKDAY);
171            setTitle(title);
172            mTitleNeedsUpdate = true;
173        } else {
174            if (mTitleNeedsUpdate) {
175                mTitleNeedsUpdate = false;
176                setTitle(R.string.date_picker_dialog_title);
177            }
178        }
179    }
180
181    @Override
182    public Bundle onSaveInstanceState() {
183        Bundle state = super.onSaveInstanceState();
184        state.putInt(YEAR, mDatePicker.getYear());
185        state.putInt(MONTH, mDatePicker.getMonth());
186        state.putInt(DAY, mDatePicker.getDayOfMonth());
187        return state;
188    }
189
190    @Override
191    public void onRestoreInstanceState(Bundle savedInstanceState) {
192        super.onRestoreInstanceState(savedInstanceState);
193        int year = savedInstanceState.getInt(YEAR);
194        int month = savedInstanceState.getInt(MONTH);
195        int day = savedInstanceState.getInt(DAY);
196        mDatePicker.init(year, month, day, this);
197    }
198}
199