Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bd9152f6 authored by Fabrice Di Meglio's avatar Fabrice Di Meglio
Browse files

Update DatePicker widget and its related dialog

- the old DatePicker widget is still there for obvious layout compatibility reasons
- add a new delegate implementation for having a new UI
- use the new delegate only for the DatePickerDialog (which does not need to be
the same)
- added support for Theming and light/dark Themes
- added support for RTL
- added support for Accessibility
- verified support for Keyboard
- verified that CTS tests for DatePicker are passing (for both the legacy and the
new widgets)

Also added a new HapticFeedbackConstants.CALENDAR_DATE and its related code for
enabling day selection vibration

Change-Id: I256bd7c21edd8f3b910413ca15ce26d3a5ef7d9c
parent d8176941
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -359,6 +359,7 @@ package android {
    field public static final int buttonTint = 16843889; // 0x1010471
    field public static final int buttonTintMode = 16843890; // 0x1010472
    field public static final int cacheColorHint = 16843009; // 0x1010101
    field public static final int calendarTextColor = 16843931; // 0x101049b
    field public static final int calendarViewShown = 16843596; // 0x101034c
    field public static final int calendarViewStyle = 16843613; // 0x101035d
    field public static final int canRequestEnhancedWebAccessibility = 16843736; // 0x10103d8
@@ -447,6 +448,14 @@ package android {
    field public static final int dashWidth = 16843174; // 0x10101a6
    field public static final int data = 16842798; // 0x101002e
    field public static final int datePickerStyle = 16843612; // 0x101035c
    field public static final int dateSelectorBackgroundColor = 16843925; // 0x1010495
    field public static final int dateSelectorDayOfMonthTextAppearance = 16843927; // 0x1010497
    field public static final int dateSelectorDayOfWeekBackgroundColor = 16843923; // 0x1010493
    field public static final int dateSelectorDayOfWeekTextAppearance = 16843924; // 0x1010494
    field public static final int dateSelectorMonthTextAppearance = 16843926; // 0x1010496
    field public static final int dateSelectorYearListItemTextAppearance = 16843929; // 0x1010499
    field public static final int dateSelectorYearListSelectedCircleColor = 16843930; // 0x101049a
    field public static final int dateSelectorYearTextAppearance = 16843928; // 0x1010498
    field public static final int dateTextAppearance = 16843593; // 0x1010349
    field public static final int debuggable = 16842767; // 0x101000f
    field public static final int defaultValue = 16843245; // 0x10101ed
@@ -37152,8 +37161,16 @@ package android.widget {
    ctor public DatePicker(android.content.Context, android.util.AttributeSet);
    ctor public DatePicker(android.content.Context, android.util.AttributeSet, int);
    ctor public DatePicker(android.content.Context, android.util.AttributeSet, int, int);
    method public android.content.res.ColorStateList getCalendarTextColor();
    method public android.widget.CalendarView getCalendarView();
    method public boolean getCalendarViewShown();
    method public int getDateSelectorBackgroundColor();
    method public int getDateSelectorDayOfMonthTextAppearance();
    method public int getDateSelectorDayOfWeekBackgroundColor();
    method public int getDateSelectorDayOfWeekTextAppearance();
    method public int getDateSelectorMonthTextAppearance();
    method public int getDateSelectorYearListItemTextAppearance();
    method public int getDateSelectorYearTextAppearance();
    method public int getDayOfMonth();
    method public long getMaxDate();
    method public long getMinDate();
@@ -37161,7 +37178,15 @@ package android.widget {
    method public boolean getSpinnersShown();
    method public int getYear();
    method public void init(int, int, int, android.widget.DatePicker.OnDateChangedListener);
    method public void setCalendarTextColor(android.content.res.ColorStateList);
    method public void setCalendarViewShown(boolean);
    method public void setDateSelectorBackgroundColor(int);
    method public void setDateSelectorDayOfMonthTextAppearance(int);
    method public void setDateSelectorDayOfWeekBackgroundColor(int);
    method public void setDateSelectorDayOfWeekTextAppearance(int);
    method public void setDateSelectorMonthTextAppearance(int);
    method public void setDateSelectorYearListItemTextAppearance(int);
    method public void setDateSelectorYearTextAppearance(int);
    method public void setMaxDate(long);
    method public void setMinDate(long);
    method public void setSpinnersShown(boolean);
+51 −23
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
@@ -44,10 +45,11 @@ public class DatePickerDialog extends AlertDialog implements OnClickListener,
    private static final String DAY = "day";

    private final DatePicker mDatePicker;
    private final OnDateSetListener mCallBack;
    private final OnDateSetListener mDateSetListener;
    private final Calendar mCalendar;

    private boolean mTitleNeedsUpdate = true;
    private boolean mIsCanceled = false;

    /**
     * The callback used to indicate the user is done filling in the date.
@@ -79,29 +81,36 @@ public class DatePickerDialog extends AlertDialog implements OnClickListener,
        this(context, 0, callBack, year, monthOfYear, dayOfMonth);
    }

    static int resolveDialogTheme(Context context, int resid) {
        if (resid == 0) {
            TypedValue outValue = new TypedValue();
            context.getTheme().resolveAttribute(R.attr.datePickerDialogTheme, outValue, true);
            return outValue.resourceId;
        } else {
            return resid;
        }
    }

    /**
     * @param context The context the dialog is to run in.
     * @param theme the theme to apply to this dialog
     * @param callBack How the parent is notified that the date is set.
     * @param listener How the parent is notified that the date is set.
     * @param year The initial year of the dialog.
     * @param monthOfYear The initial month of the dialog.
     * @param dayOfMonth The initial day of the dialog.
     */
    public DatePickerDialog(Context context,
            int theme,
            OnDateSetListener callBack,
            OnDateSetListener listener,
            int year,
            int monthOfYear,
            int dayOfMonth) {
        super(context, theme);

        mCallBack = callBack;
        super(context, resolveDialogTheme(context, theme));

        mDateSetListener = listener;
        mCalendar = Calendar.getInstance();

        Context themeContext = getContext();
        setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_done), this);
        setIcon(0);

        LayoutInflater inflater =
                (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@@ -109,20 +118,53 @@ public class DatePickerDialog extends AlertDialog implements OnClickListener,
        setView(view);
        setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);
        mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);

        // Initialize state
        mDatePicker.setLegacyMode(false, null);
        mDatePicker.setShowDoneButton(true);
        mDatePicker.setDismissCallback(new DatePicker.DatePickerDismissCallback() {
            @Override
            public void dismiss(DatePicker view, boolean isCancel, int year, int month, int dayOfMonth) {
                mIsCanceled = isCancel;
                if (!isCancel) {
                    mDateSetListener.onDateSet(view, year, month, dayOfMonth);
                }
                DatePickerDialog.this.dismiss();
            }
        });
        mDatePicker.init(year, monthOfYear, dayOfMonth, this);
        updateTitle(year, monthOfYear, dayOfMonth);
    }

    public void onClick(DialogInterface dialog, int which) {
        tryNotifyDateSet();
    }

    @Override
    public void cancel() {
        mIsCanceled = true;
        super.cancel();
    }

    @Override
    protected void onStop() {
        tryNotifyDateSet();
        super.onStop();
    }

    public void onDateChanged(DatePicker view, int year,
            int month, int day) {
        mDatePicker.init(year, month, day, this);
        updateTitle(year, month, day);
    }

    private void tryNotifyDateSet() {
        if (mDateSetListener != null && !mIsCanceled) {
            mDatePicker.clearFocus();
            mDateSetListener.onDateSet(mDatePicker, mDatePicker.getYear(),
                    mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
        }
    }

    /**
     * Gets the {@link DatePicker} contained in this dialog.
     *
@@ -143,20 +185,6 @@ public class DatePickerDialog extends AlertDialog implements OnClickListener,
        mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
    }

    private void tryNotifyDateSet() {
        if (mCallBack != null) {
            mDatePicker.clearFocus();
            mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(),
                    mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
        }
    }

    @Override
    protected void onStop() {
        tryNotifyDateSet();
        super.onStop();
    }

    private void updateTitle(int year, int month, int day) {
        if (!mDatePicker.getCalendarViewShown()) {
            mCalendar.set(Calendar.YEAR, year);
+6 −0
Original line number Diff line number Diff line
@@ -45,6 +45,12 @@ public class HapticFeedbackConstants {
     */
    public static final int CLOCK_TICK = 4;

    /**
     * The user has pressed either a day or month or year date of a Calendar.
     * @hide
     */
    public static final int CALENDAR_DATE = 5;

    /**
     * This is a private constant.  Feel free to renumber as desired.
     * @hide
+402 −15

File changed.

Preview size limit exceeded, changes collapsed.

+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.widget;

import java.util.Calendar;

/**
 * Controller class to communicate among the various components of the date picker dialog.
 *
 * @hide
 */
interface DatePickerController {

    void onYearSelected(int year);

    void onDayOfMonthSelected(int year, int month, int day);

    void registerOnDateChangedListener(OnDateChangedListener listener);

    void unregisterOnDateChangedListener(OnDateChangedListener listener);

    Calendar getSelectedDay();

    int getFirstDayOfWeek();

    int getMinYear();
    int getMaxYear();

    int getMinMonth();
    int getMaxMonth();

    int getMinDay();
    int getMaxDay();

    void setMinDate(long minDate);
    Calendar getMinDate();

    void setMaxDate(long maxDate);
    Calendar getMaxDate();

    void tryVibrate();
}
Loading