Search in sources :

Example 1 with DatePickerDialog

use of com.android.contacts.datepicker.DatePickerDialog in project packages_apps_Contacts by AOKP.

the class EventFieldEditorView method createDatePickerDialog.

/**
 * Prepare dialog for entering a date
 */
private Dialog createDatePickerDialog() {
    final String column = getKind().fieldList.get(0).column;
    final String oldValue = getEntry().getAsString(column);
    final DataKind kind = getKind();
    final Calendar calendar = Calendar.getInstance(DateUtils.UTC_TIMEZONE, Locale.US);
    final int defaultYear = calendar.get(Calendar.YEAR);
    // Check whether the year is optional
    final boolean isYearOptional = getType().isYearOptional();
    final int oldYear, oldMonth, oldDay;
    if (TextUtils.isEmpty(oldValue)) {
        // Default to the current date
        oldYear = defaultYear;
        oldMonth = calendar.get(Calendar.MONTH);
        oldDay = calendar.get(Calendar.DAY_OF_MONTH);
    } else {
        // Try parsing with year
        Calendar cal = DateUtils.parseDate(oldValue, false);
        if (cal != null) {
            if (DateUtils.isYearSet(cal)) {
                oldYear = cal.get(Calendar.YEAR);
            } else {
                // cal.set(Calendar.YEAR, 0);
                oldYear = isYearOptional ? DatePickerDialog.NO_YEAR : defaultYear;
            }
            oldMonth = cal.get(Calendar.MONTH);
            oldDay = cal.get(Calendar.DAY_OF_MONTH);
        } else {
            return null;
        }
    }
    final OnDateSetListener callBack = new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            if (year == 0 && !isYearOptional)
                throw new IllegalStateException();
            final Calendar outCalendar = Calendar.getInstance(DateUtils.UTC_TIMEZONE, Locale.US);
            // If no year specified, set it to 2000 (we could pick any leap year here).
            // The format string will ignore that year.
            // For formats other than Exchange, the time of the day is ignored
            outCalendar.clear();
            outCalendar.set(year == DatePickerDialog.NO_YEAR ? 2000 : year, monthOfYear, dayOfMonth, CommonDateUtils.DEFAULT_HOUR, 0, 0);
            final String resultString;
            if (year == 0) {
                resultString = kind.dateFormatWithoutYear.format(outCalendar.getTime());
            } else {
                resultString = kind.dateFormatWithYear.format(outCalendar.getTime());
            }
            onFieldChanged(column, resultString);
            rebuildDateView();
        }
    };
    final DatePickerDialog resultDialog = new DatePickerDialog(getContext(), callBack, oldYear, oldMonth, oldDay, isYearOptional);
    return resultDialog;
}
Also used : OnDateSetListener(com.android.contacts.datepicker.DatePickerDialog.OnDateSetListener) DatePickerDialog(com.android.contacts.datepicker.DatePickerDialog) DataKind(com.android.contacts.common.model.dataitem.DataKind) Calendar(java.util.Calendar) DatePicker(com.android.contacts.datepicker.DatePicker)

Aggregations

DataKind (com.android.contacts.common.model.dataitem.DataKind)1 DatePicker (com.android.contacts.datepicker.DatePicker)1 DatePickerDialog (com.android.contacts.datepicker.DatePickerDialog)1 OnDateSetListener (com.android.contacts.datepicker.DatePickerDialog.OnDateSetListener)1 Calendar (java.util.Calendar)1