DatePickerDialog.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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.pim.DateFormat;
24import android.text.TextUtils.TruncateAt;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.widget.DatePicker;
28import android.widget.TextView;
29import android.widget.DatePicker.OnDateChangedListener;
30
31import com.android.internal.R;
32
33import java.text.DateFormatSymbols;
34import java.util.Calendar;
35
36/**
37 * A simple dialog containing an {@link android.widget.DatePicker}.
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    private final java.text.DateFormat mDateFormat;
50    private final String[] mWeekDays;
51
52    private int mInitialYear;
53    private int mInitialMonth;
54    private int mInitialDay;
55
56    /**
57     * The callback used to indicate the user is done filling in the date.
58     */
59    public interface OnDateSetListener {
60
61        /**
62         * @param view The view associated with this listener.
63         * @param year The year that was set.
64         * @param monthOfYear The month that was set (0-11) for compatibility
65         *  with {@link java.util.Calendar}.
66         * @param dayOfMonth The day of the month that was set.
67         */
68        void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth);
69    }
70
71    /**
72     * @param context The context the dialog is to run in.
73     * @param callBack How the parent is notified that the date is set.
74     * @param year The initial year of the dialog.
75     * @param monthOfYear The initial month of the dialog.
76     * @param dayOfMonth The initial day of the dialog.
77     */
78    public DatePickerDialog(Context context,
79            OnDateSetListener callBack,
80            int year,
81            int monthOfYear,
82            int dayOfMonth) {
83        this(context, com.android.internal.R.style.Theme_Dialog_Alert,
84                callBack, year, monthOfYear, dayOfMonth);
85    }
86
87    /**
88     * @param context The context the dialog is to run in.
89     * @param theme the theme to apply to this dialog
90     * @param callBack How the parent is notified that the date is set.
91     * @param year The initial year of the dialog.
92     * @param monthOfYear The initial month of the dialog.
93     * @param dayOfMonth The initial day of the dialog.
94     */
95    public DatePickerDialog(Context context,
96            int theme,
97            OnDateSetListener callBack,
98            int year,
99            int monthOfYear,
100            int dayOfMonth) {
101        super(context, theme);
102
103        mCallBack = callBack;
104        mInitialYear = year;
105        mInitialMonth = monthOfYear;
106        mInitialDay = dayOfMonth;
107        DateFormatSymbols symbols = new DateFormatSymbols();
108        mWeekDays = symbols.getShortWeekdays();
109
110        mDateFormat = DateFormat.getLongDateFormat(context);
111        mCalendar = Calendar.getInstance();
112        updateTitle(mInitialYear, mInitialMonth, mInitialDay);
113
114        setButton(context.getText(R.string.date_time_set), this);
115        setButton2(context.getText(R.string.cancel), (OnClickListener) null);
116        setIcon(R.drawable.ic_dialog_time);
117
118        LayoutInflater inflater =
119                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
120        View view = inflater.inflate(R.layout.date_picker_dialog, null);
121        setView(view);
122        mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);
123        mDatePicker.init(mInitialYear, mInitialMonth, mInitialDay, this);
124    }
125
126    @Override
127    public void show() {
128        super.show();
129
130        /* Sometimes the full month is displayed causing the title
131         * to be very long, in those cases ensure it doesn't wrap to
132         * 2 lines (as that looks jumpy) and ensure we ellipsize the end.
133         */
134        TextView title = (TextView) findViewById(R.id.alertTitle);
135        title.setSingleLine();
136        title.setEllipsize(TruncateAt.END);
137    }
138
139    public void onClick(DialogInterface dialog, int which) {
140        if (mCallBack != null) {
141            mDatePicker.clearFocus();
142            mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(),
143                    mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
144        }
145    }
146
147    public void onDateChanged(DatePicker view, int year,
148            int month, int day) {
149        updateTitle(year, month, day);
150    }
151
152    public void updateDate(int year, int monthOfYear, int dayOfMonth) {
153        mInitialYear = year;
154        mInitialMonth = monthOfYear;
155        mInitialDay = dayOfMonth;
156        mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
157    }
158
159    private void updateTitle(int year, int month, int day) {
160        mCalendar.set(Calendar.YEAR, year);
161        mCalendar.set(Calendar.MONTH, month);
162        mCalendar.set(Calendar.DAY_OF_MONTH, day);
163        String weekday = mWeekDays[mCalendar.get(Calendar.DAY_OF_WEEK)];
164        setTitle(weekday + ", " + mDateFormat.format(mCalendar.getTime()));
165    }
166
167    @Override
168    public Bundle onSaveInstanceState() {
169        Bundle state = super.onSaveInstanceState();
170        state.putInt(YEAR, mDatePicker.getYear());
171        state.putInt(MONTH, mDatePicker.getMonth());
172        state.putInt(DAY, mDatePicker.getDayOfMonth());
173        return state;
174    }
175
176    @Override
177    public void onRestoreInstanceState(Bundle savedInstanceState) {
178        super.onRestoreInstanceState(savedInstanceState);
179        int year = savedInstanceState.getInt(YEAR);
180        int month = savedInstanceState.getInt(MONTH);
181        int day = savedInstanceState.getInt(DAY);
182        mDatePicker.init(year, month, day, this);
183        updateTitle(year, month, day);
184    }
185}
186