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

Commit 698b751d authored by Kirill Grouchnikov's avatar Kirill Grouchnikov
Browse files

Add @TestApi-guarded way to get bounds of specific day in CalendarView

Two separate implementation paths, one for Material look / layout, and
one for legacy / pre-Material one.

Bug: 28037149
Change-Id: Id1946802c0a93218d9eb0b73c81ad76dc027763c
parent db976a28
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -46394,6 +46394,7 @@ package android.widget {
    ctor public CalendarView(android.content.Context, android.util.AttributeSet);
    ctor public CalendarView(android.content.Context, android.util.AttributeSet);
    ctor public CalendarView(android.content.Context, android.util.AttributeSet, int);
    ctor public CalendarView(android.content.Context, android.util.AttributeSet, int);
    ctor public CalendarView(android.content.Context, android.util.AttributeSet, int, int);
    ctor public CalendarView(android.content.Context, android.util.AttributeSet, int, int);
    method public boolean getBoundsForDate(long, android.graphics.Rect);
    method public long getDate();
    method public long getDate();
    method public int getDateTextAppearance();
    method public int getDateTextAppearance();
    method public int getFirstDayOfWeek();
    method public int getFirstDayOfWeek();
+17 −0
Original line number Original line Diff line number Diff line
@@ -22,10 +22,12 @@ import android.annotation.DrawableRes;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.annotation.StyleRes;
import android.annotation.StyleRes;
import android.annotation.TestApi;
import android.annotation.Widget;
import android.annotation.Widget;
import android.content.Context;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Log;
@@ -544,6 +546,19 @@ public class CalendarView extends FrameLayout {
        mDelegate.setDate(date, animate, center);
        mDelegate.setDate(date, animate, center);
    }
    }


    /**
     * Retrieves the screen bounds for the specific date in the coordinate system of this
     * view. If the passed date is being currently displayed, this method returns true and
     * the caller can query the fields of the passed {@link Rect} object. Otherwise the
     * method returns false and does not touch the passed {@link Rect} object.
     *
     * @hide
     */
    @TestApi
    public boolean getBoundsForDate(long date, Rect outBounds) {
        return mDelegate.getBoundsForDate(date, outBounds);
    }

    @Override
    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        super.onConfigurationChanged(newConfig);
@@ -605,6 +620,8 @@ public class CalendarView extends FrameLayout {
        void setDate(long date, boolean animate, boolean center);
        void setDate(long date, boolean animate, boolean center);
        long getDate();
        long getDate();


        boolean getBoundsForDate(long date, Rect outBounds);

        void setOnDateChangeListener(OnDateChangeListener listener);
        void setOnDateChangeListener(OnDateChangeListener listener);


        void onConfigurationChanged(Configuration newConfig);
        void onConfigurationChanged(Configuration newConfig);
+61 −9
Original line number Original line Diff line number Diff line
@@ -103,7 +103,7 @@ class CalendarViewLegacyDelegate extends CalendarView.AbstractCalendarViewDelega


    private static final int DEFAULT_WEEK_DAY_TEXT_APPEARANCE_RES_ID = -1;
    private static final int DEFAULT_WEEK_DAY_TEXT_APPEARANCE_RES_ID = -1;


    private final int mWeekSeperatorLineWidth;
    private final int mWeekSeparatorLineWidth;


    private int mDateTextSize;
    private int mDateTextSize;


@@ -308,7 +308,7 @@ class CalendarViewLegacyDelegate extends CalendarView.AbstractCalendarViewDelega
                UNSCALED_BOTTOM_BUFFER, displayMetrics);
                UNSCALED_BOTTOM_BUFFER, displayMetrics);
        mSelectedDateVerticalBarWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
        mSelectedDateVerticalBarWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                UNSCALED_SELECTED_DATE_VERTICAL_BAR_WIDTH, displayMetrics);
                UNSCALED_SELECTED_DATE_VERTICAL_BAR_WIDTH, displayMetrics);
        mWeekSeperatorLineWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
        mWeekSeparatorLineWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                UNSCALED_WEEK_SEPARATOR_LINE_WIDTH, displayMetrics);
                UNSCALED_WEEK_SEPARATOR_LINE_WIDTH, displayMetrics);


        LayoutInflater layoutInflater = (LayoutInflater) mContext
        LayoutInflater layoutInflater = (LayoutInflater) mContext
@@ -601,6 +601,30 @@ class CalendarViewLegacyDelegate extends CalendarView.AbstractCalendarViewDelega
        mOnDateChangeListener = listener;
        mOnDateChangeListener = listener;
    }
    }


    @Override
    public boolean getBoundsForDate(long date, Rect outBounds) {
        Calendar calendarDate = Calendar.getInstance();
        calendarDate.setTimeInMillis(date);
        int listViewEntryCount = mListView.getCount();
        for (int i = 0; i < listViewEntryCount; i++) {
            WeekView currWeekView = (WeekView) mListView.getChildAt(i);
            if (currWeekView.getBoundsForDate(calendarDate, outBounds)) {
                // Found the date in this week. Now need to offset vertically to return correct
                // bounds in the coordinate system of the entire layout
                final int[] weekViewPositionOnScreen = new int[2];
                final int[] delegatorPositionOnScreen = new int[2];
                currWeekView.getLocationOnScreen(weekViewPositionOnScreen);
                mDelegator.getLocationOnScreen(delegatorPositionOnScreen);
                final int extraVerticalOffset =
                        weekViewPositionOnScreen[1] - delegatorPositionOnScreen[1];
                outBounds.top += extraVerticalOffset;
                outBounds.bottom += extraVerticalOffset;
                return true;
            }
        }
        return false;
    }

    @Override
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    public void onConfigurationChanged(Configuration newConfig) {
        setCurrentLocale(newConfig.locale);
        setCurrentLocale(newConfig.locale);
@@ -1199,7 +1223,7 @@ class CalendarViewLegacyDelegate extends CalendarView.AbstractCalendarViewDelega
            super(context);
            super(context);


            // Sets up any standard paints that will be used
            // Sets up any standard paints that will be used
            initilaizePaints();
            initializePaints();
        }
        }


        /**
        /**
@@ -1270,7 +1294,7 @@ class CalendarViewLegacyDelegate extends CalendarView.AbstractCalendarViewDelega
        /**
        /**
         * Initialize the paint instances.
         * Initialize the paint instances.
         */
         */
        private void initilaizePaints() {
        private void initializePaints() {
            mDrawPaint.setFakeBoldText(false);
            mDrawPaint.setFakeBoldText(false);
            mDrawPaint.setAntiAlias(true);
            mDrawPaint.setAntiAlias(true);
            mDrawPaint.setStyle(Paint.Style.FILL);
            mDrawPaint.setStyle(Paint.Style.FILL);
@@ -1348,6 +1372,34 @@ class CalendarViewLegacyDelegate extends CalendarView.AbstractCalendarViewDelega
            return true;
            return true;
        }
        }


        public boolean getBoundsForDate(Calendar date, Rect outBounds) {
            Calendar currDay = Calendar.getInstance();
            currDay.setTime(mFirstDay.getTime());
            for (int i = 0; i < mDaysPerWeek; i++) {
                if ((date.get(Calendar.YEAR) == currDay.get(Calendar.YEAR))
                    && (date.get(Calendar.MONTH) == currDay.get(Calendar.MONTH))
                    && (date.get(Calendar.DAY_OF_MONTH) == currDay.get(Calendar.DAY_OF_MONTH))) {
                    // We found the matching date. Follow the logic in the draw pass that divides
                    // the available horizontal space equally between all the entries in this week.
                    // Note that if we're showing week number, the start entry will be that number.
                    int cellSize = mWidth / mNumCells;
                    if (isLayoutRtl()) {
                        outBounds.left = cellSize *
                                (mShowWeekNumber ? (mNumCells - i - 2) : (mNumCells - i - 1));
                    } else {
                        outBounds.left = cellSize * (mShowWeekNumber ? i + 1 : i);
                    }
                    outBounds.top = 0;
                    outBounds.right = outBounds.left + cellSize;
                    outBounds.bottom = getHeight();
                    return true;
                }
                // Add one day
                currDay.add(Calendar.DAY_OF_MONTH, 1);
            }
            return false;
        }

        @Override
        @Override
        protected void onDraw(Canvas canvas) {
        protected void onDraw(Canvas canvas) {
            drawBackground(canvas);
            drawBackground(canvas);
@@ -1367,7 +1419,7 @@ class CalendarViewLegacyDelegate extends CalendarView.AbstractCalendarViewDelega
            }
            }
            mDrawPaint.setColor(mSelectedWeekBackgroundColor);
            mDrawPaint.setColor(mSelectedWeekBackgroundColor);


            mTempRect.top = mWeekSeperatorLineWidth;
            mTempRect.top = mWeekSeparatorLineWidth;
            mTempRect.bottom = mHeight;
            mTempRect.bottom = mHeight;


            final boolean isLayoutRtl = isLayoutRtl();
            final boolean isLayoutRtl = isLayoutRtl();
@@ -1398,7 +1450,7 @@ class CalendarViewLegacyDelegate extends CalendarView.AbstractCalendarViewDelega
         */
         */
        private void drawWeekNumbersAndDates(Canvas canvas) {
        private void drawWeekNumbersAndDates(Canvas canvas) {
            final float textHeight = mDrawPaint.getTextSize();
            final float textHeight = mDrawPaint.getTextSize();
            final int y = (int) ((mHeight + textHeight) / 2) - mWeekSeperatorLineWidth;
            final int y = (int) ((mHeight + textHeight) / 2) - mWeekSeparatorLineWidth;
            final int nDays = mNumCells;
            final int nDays = mNumCells;
            final int divisor = 2 * nDays;
            final int divisor = 2 * nDays;


@@ -1450,7 +1502,7 @@ class CalendarViewLegacyDelegate extends CalendarView.AbstractCalendarViewDelega
                return;
                return;
            }
            }
            mDrawPaint.setColor(mWeekSeparatorLineColor);
            mDrawPaint.setColor(mWeekSeparatorLineColor);
            mDrawPaint.setStrokeWidth(mWeekSeperatorLineWidth);
            mDrawPaint.setStrokeWidth(mWeekSeparatorLineWidth);
            float startX;
            float startX;
            float stopX;
            float stopX;
            if (isLayoutRtl()) {
            if (isLayoutRtl()) {
@@ -1474,13 +1526,13 @@ class CalendarViewLegacyDelegate extends CalendarView.AbstractCalendarViewDelega
            }
            }
            mSelectedDateVerticalBar.setBounds(
            mSelectedDateVerticalBar.setBounds(
                    mSelectedLeft - mSelectedDateVerticalBarWidth / 2,
                    mSelectedLeft - mSelectedDateVerticalBarWidth / 2,
                    mWeekSeperatorLineWidth,
                    mWeekSeparatorLineWidth,
                    mSelectedLeft + mSelectedDateVerticalBarWidth / 2,
                    mSelectedLeft + mSelectedDateVerticalBarWidth / 2,
                    mHeight);
                    mHeight);
            mSelectedDateVerticalBar.draw(canvas);
            mSelectedDateVerticalBar.draw(canvas);
            mSelectedDateVerticalBar.setBounds(
            mSelectedDateVerticalBar.setBounds(
                    mSelectedRight - mSelectedDateVerticalBarWidth / 2,
                    mSelectedRight - mSelectedDateVerticalBarWidth / 2,
                    mWeekSeperatorLineWidth,
                    mWeekSeparatorLineWidth,
                    mSelectedRight + mSelectedDateVerticalBarWidth / 2,
                    mSelectedRight + mSelectedDateVerticalBarWidth / 2,
                    mHeight);
                    mHeight);
            mSelectedDateVerticalBar.draw(canvas);
            mSelectedDateVerticalBar.draw(canvas);
+20 −0
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@ package android.widget;


import android.annotation.StyleRes;
import android.annotation.StyleRes;
import android.content.Context;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.AttributeSet;
import android.widget.DayPickerView.OnDaySelectedListener;
import android.widget.DayPickerView.OnDaySelectedListener;


@@ -110,6 +111,25 @@ class CalendarViewMaterialDelegate extends CalendarView.AbstractCalendarViewDele
        mOnDateChangeListener = listener;
        mOnDateChangeListener = listener;
    }
    }


    @Override
    public boolean getBoundsForDate(long date, Rect outBounds) {
        boolean result = mDayPickerView.getBoundsForDate(date, outBounds);
        if (result) {
            // Found the date in the current picker. Now need to offset vertically to return correct
            // bounds in the coordinate system of the entire layout
            final int[] dayPickerPositionOnScreen = new int[2];
            final int[] delegatorPositionOnScreen = new int[2];
            mDayPickerView.getLocationOnScreen(dayPickerPositionOnScreen);
            mDelegator.getLocationOnScreen(delegatorPositionOnScreen);
            final int extraVerticalOffset =
                    dayPickerPositionOnScreen[1] - delegatorPositionOnScreen[1];
            outBounds.top += extraVerticalOffset;
            outBounds.bottom += extraVerticalOffset;
            return true;
        }
        return false;
    }

    private final OnDaySelectedListener mOnDaySelectedListener = new OnDaySelectedListener() {
    private final OnDaySelectedListener mOnDaySelectedListener = new OnDaySelectedListener() {
        @Override
        @Override
        public void onDaySelected(DayPickerView view, Calendar day) {
        public void onDaySelected(DayPickerView view, Calendar day) {
+12 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package android.widget;
package android.widget;


import android.graphics.Rect;
import com.android.internal.widget.PagerAdapter;
import com.android.internal.widget.PagerAdapter;


import android.annotation.IdRes;
import android.annotation.IdRes;
@@ -108,6 +109,17 @@ class DayPickerPagerAdapter extends PagerAdapter {
        return mFirstDayOfWeek;
        return mFirstDayOfWeek;
    }
    }


    public boolean getBoundsForDate(Calendar day, Rect outBounds) {
        final int position = getPositionForDay(day);
        final ViewHolder monthView = mItems.get(position, null);
        if (monthView == null) {
            return false;
        } else {
            final int dayOfMonth = day.get(Calendar.DAY_OF_MONTH);
            return monthView.calendar.getBoundsForDay(dayOfMonth, outBounds);
        }
    }

    /**
    /**
     * Sets the selected day.
     * Sets the selected day.
     *
     *
Loading