DatePickerDialog.java revision e8db3a3f4016184b08ff39b0cdc2cc02e852f7e7
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.TextUtils.TruncateAt;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.widget.DatePicker;
27import android.widget.TextView;
28import android.widget.DatePicker.OnDateChangedListener;
29
30import com.android.internal.R;
31
32import java.text.DateFormatSymbols;
33import java.util.Calendar;
34
35/**
36 * A simple dialog containing an {@link android.widget.DatePicker}.
37 */
38public class DatePickerDialog extends AlertDialog implements OnClickListener,
39        OnDateChangedListener {
40
41    private static final String YEAR = "year";
42    private static final String MONTH = "month";
43    private static final String DAY = "day";
44
45    private final DatePicker mDatePicker;
46    private final OnDateSetListener mCallBack;
47    private final Calendar mCalendar;
48    private final java.text.DateFormat mTitleDateFormat;
49    private final String[] mWeekDays;
50
51    private int mInitialYear;
52    private int mInitialMonth;
53    private int mInitialDay;
54
55    /**
56     * The callback used to indicate the user is done filling in the date.
57     */
58    public interface OnDateSetListener {
59
60        /**
61         * @param view The view associated with this listener.
62         * @param year The year that was set.
63         * @param monthOfYear The month that was set (0-11) for compatibility
64         *  with {@link java.util.Calendar}.
65         * @param dayOfMonth The day of the month that was set.
66         */
67        void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth);
68    }
69
70    /**
71     * @param context The context the dialog is to run in.
72     * @param callBack How the parent is notified that the date is set.
73     * @param year The initial year of the dialog.
74     * @param monthOfYear The initial month of the dialog.
75     * @param dayOfMonth The initial day of the dialog.
76     */
77    public DatePickerDialog(Context context,
78            OnDateSetListener callBack,
79            int year,
80            int monthOfYear,
81            int dayOfMonth) {
82        this(context, com.android.internal.R.style.Theme_Dialog_Alert,
83                callBack, year, monthOfYear, dayOfMonth);
84    }
85
86    /**
87     * @param context The context the dialog is to run in.
88     * @param theme the theme to apply to this dialog
89     * @param callBack How the parent is notified that the date is set.
90     * @param year The initial year of the dialog.
91     * @param monthOfYear The initial month of the dialog.
92     * @param dayOfMonth The initial day of the dialog.
93     */
94    public DatePickerDialog(Context context,
95            int theme,
96            OnDateSetListener callBack,
97            int year,
98            int monthOfYear,
99            int dayOfMonth) {
100        super(context, theme);
101
102        mCallBack = callBack;
103        mInitialYear = year;
104        mInitialMonth = monthOfYear;
105        mInitialDay = dayOfMonth;
106        DateFormatSymbols symbols = new DateFormatSymbols();
107        mWeekDays = symbols.getShortWeekdays();
108
109        mTitleDateFormat = java.text.DateFormat.
110                                getDateInstance(java.text.DateFormat.FULL);
111        mCalendar = Calendar.getInstance();
112        updateTitle(mInitialYear, mInitialMonth, mInitialDay);
113
114        setButton(BUTTON_POSITIVE, context.getText(R.string.date_time_set), this);
115        setButton(BUTTON_NEGATIVE, 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        setTitle(mTitleDateFormat.format(mCalendar.getTime()));
164    }
165
166    @Override
167    public Bundle onSaveInstanceState() {
168        Bundle state = super.onSaveInstanceState();
169        state.putInt(YEAR, mDatePicker.getYear());
170        state.putInt(MONTH, mDatePicker.getMonth());
171        state.putInt(DAY, mDatePicker.getDayOfMonth());
172        return state;
173    }
174
175    @Override
176    public void onRestoreInstanceState(Bundle savedInstanceState) {
177        super.onRestoreInstanceState(savedInstanceState);
178        int year = savedInstanceState.getInt(YEAR);
179        int month = savedInstanceState.getInt(MONTH);
180        int day = savedInstanceState.getInt(DAY);
181        mDatePicker.init(year, month, day, this);
182        updateTitle(year, month, day);
183    }
184}
185