diff options
Diffstat (limited to 'core/java/android/widget/CalendarView.java')
| -rw-r--r-- | core/java/android/widget/CalendarView.java | 319 |
1 files changed, 287 insertions, 32 deletions
diff --git a/core/java/android/widget/CalendarView.java b/core/java/android/widget/CalendarView.java index 85252af68f01..2a74f6a5d797 100644 --- a/core/java/android/widget/CalendarView.java +++ b/core/java/android/widget/CalendarView.java @@ -154,21 +154,25 @@ public class CalendarView extends FrameLayout { private final int mWeekSeperatorLineWidth; - private final int mDateTextSize; + private int mDateTextSize; - private final Drawable mSelectedDateVerticalBar; + private Drawable mSelectedDateVerticalBar; private final int mSelectedDateVerticalBarWidth; - private final int mSelectedWeekBackgroundColor; + private int mSelectedWeekBackgroundColor; - private final int mFocusedMonthDateColor; + private int mFocusedMonthDateColor; - private final int mUnfocusedMonthDateColor; + private int mUnfocusedMonthDateColor; - private final int mWeekSeparatorLineColor; + private int mWeekSeparatorLineColor; - private final int mWeekNumberColor; + private int mWeekNumberColor; + + private int mWeekDayTextAppearanceResId; + + private int mDateTextAppearanceResId; /** * The top offset of the weeks list. @@ -366,15 +370,11 @@ public class CalendarView extends FrameLayout { mSelectedDateVerticalBar = attributesArray.getDrawable( R.styleable.CalendarView_selectedDateVerticalBar); - int dateTextAppearanceResId= attributesArray.getResourceId( + mDateTextAppearanceResId = attributesArray.getResourceId( R.styleable.CalendarView_dateTextAppearance, R.style.TextAppearance_Small); - TypedArray dateTextAppearance = context.obtainStyledAttributes(dateTextAppearanceResId, - com.android.internal.R.styleable.TextAppearance); - mDateTextSize = dateTextAppearance.getDimensionPixelSize( - R.styleable.TextAppearance_textSize, DEFAULT_DATE_TEXT_SIZE); - dateTextAppearance.recycle(); + updateDateTextSize(); - int weekDayTextAppearanceResId = attributesArray.getResourceId( + mWeekDayTextAppearanceResId = attributesArray.getResourceId( R.styleable.CalendarView_weekDayTextAppearance, DEFAULT_WEEK_DAY_TEXT_APPEARANCE_RES_ID); attributesArray.recycle(); @@ -400,7 +400,7 @@ public class CalendarView extends FrameLayout { mDayNamesHeader = (ViewGroup) content.findViewById(com.android.internal.R.id.day_names); mMonthName = (TextView) content.findViewById(com.android.internal.R.id.month_name); - setUpHeader(weekDayTextAppearanceResId); + setUpHeader(); setUpListView(); setUpAdapter(); @@ -417,6 +417,235 @@ public class CalendarView extends FrameLayout { invalidate(); } + /** + * Sets the number of weeks to be shown. + * + * @param count The shown week count. + */ + public void setShownWeekCount(int count) { + if (mShownWeekCount != count) { + mShownWeekCount = count; + invalidate(); + } + } + + /** + * Gets the number of weeks to be shown. + * + * @return The shown week count. + */ + public int getShownWeekCount() { + return mShownWeekCount; + } + + /** + * Sets the background color for the selected week. + * + * @param color The week background color. + */ + public void setSelectedWeekBackgroundColor(int color) { + if (mSelectedWeekBackgroundColor != color) { + mSelectedWeekBackgroundColor = color; + final int childCount = mListView.getChildCount(); + for (int i = 0; i < childCount; i++) { + WeekView weekView = (WeekView) mListView.getChildAt(i); + if (weekView.mHasSelectedDay) { + weekView.invalidate(); + } + } + } + } + + /** + * Gets the background color for the selected week. + * + * @return The week background color. + */ + public int getSelectedWeekBackgroundColor() { + return mSelectedWeekBackgroundColor; + } + + /** + * Sets the color for the dates of the focused month. + * + * @param color The focused month date color. + */ + public void setFocusedMonthDateColor(int color) { + if (mFocusedMonthDateColor != color) { + mFocusedMonthDateColor = color; + final int childCount = mListView.getChildCount(); + for (int i = 0; i < childCount; i++) { + WeekView weekView = (WeekView) mListView.getChildAt(i); + if (weekView.mHasFocusedDay) { + weekView.invalidate(); + } + } + } + } + + /** + * Gets the color for the dates in the focused month. + * + * @return The focused month date color. + */ + public int getFocusedMonthDateColor() { + return mFocusedMonthDateColor; + } + + /** + * Sets the color for the dates of a not focused month. + * + * @param color A not focused month date color. + */ + public void setUnfocusedMonthDateColor(int color) { + if (mUnfocusedMonthDateColor != color) { + mUnfocusedMonthDateColor = color; + final int childCount = mListView.getChildCount(); + for (int i = 0; i < childCount; i++) { + WeekView weekView = (WeekView) mListView.getChildAt(i); + if (weekView.mHasUnfocusedDay) { + weekView.invalidate(); + } + } + } + } + + /** + * Gets the color for the dates in a not focused month. + * + * @return A not focused month date color. + */ + public int getUnfocusedMonthDateColor() { + return mFocusedMonthDateColor; + } + + /** + * Sets the color for the week numbers. + * + * @param color The week number color. + */ + public void setWeekNumberColor(int color) { + if (mWeekNumberColor != color) { + mWeekNumberColor = color; + if (mShowWeekNumber) { + invalidateAllWeekViews(); + } + } + } + + /** + * Gets the color for the week numbers. + * + * @return The week number color. + */ + public int getWeekNumberColor() { + return mWeekNumberColor; + } + + /** + * Sets the color for the separator line between weeks. + * + * @param color The week separator color. + */ + public void setWeekSeparatorLineColor(int color) { + if (mWeekSeparatorLineColor != color) { + mWeekSeparatorLineColor = color; + invalidateAllWeekViews(); + } + } + + /** + * Gets the color for the separator line between weeks. + * + * @return The week separator color. + */ + public int getWeekSeparatorLineColor() { + return mWeekSeparatorLineColor; + } + + /** + * Sets the drawable for the vertical bar shown at the beginning and at + * the end of the selected date. + * + * @param resourceId The vertical bar drawable resource id. + */ + public void setSelectedDateVerticalBar(int resourceId) { + Drawable drawable = getResources().getDrawable(resourceId); + setSelectedDateVerticalBar(drawable); + } + + /** + * Sets the drawable for the vertical bar shown at the beginning and at + * the end of the selected date. + * + * @param drawable The vertical bar drawable. + */ + public void setSelectedDateVerticalBar(Drawable drawable) { + if (mSelectedDateVerticalBar != drawable) { + mSelectedDateVerticalBar = drawable; + final int childCount = mListView.getChildCount(); + for (int i = 0; i < childCount; i++) { + WeekView weekView = (WeekView) mListView.getChildAt(i); + if (weekView.mHasSelectedDay) { + weekView.invalidate(); + } + } + } + } + + /** + * Gets the drawable for the vertical bar shown at the beginning and at + * the end of the selected date. + * + * @return The vertical bar drawable. + */ + public Drawable getSelectedDateVerticalBar() { + return mSelectedDateVerticalBar; + } + + /** + * Sets the text appearance for the week day abbreviation of the calendar header. + * + * @param resourceId The text appearance resource id. + */ + public void setWeekDayTextAppearance(int resourceId) { + if (mWeekDayTextAppearanceResId != resourceId) { + mWeekDayTextAppearanceResId = resourceId; + setUpHeader(); + } + } + + /** + * Gets the text appearance for the week day abbreviation of the calendar header. + * + * @return The text appearance resource id. + */ + public int getWeekDayTextAppearance() { + return mWeekDayTextAppearanceResId; + } + + /** + * Sets the text appearance for the calendar dates. + * + * @param resourceId The text appearance resource id. + */ + public void setDateTextAppearance(int resourceId) { + if (mDateTextAppearanceResId != resourceId) { + mDateTextAppearanceResId = resourceId; + updateDateTextSize(); + invalidateAllWeekViews(); + } + } + + /** + * Gets the text appearance for the calendar dates. + * + * @return The text appearance resource id. + */ + public int getDateTextAppearance() { + return mDateTextAppearanceResId; + } + @Override public void setEnabled(boolean enabled) { mListView.setEnabled(enabled); @@ -545,7 +774,7 @@ public class CalendarView extends FrameLayout { } mShowWeekNumber = showWeekNumber; mAdapter.notifyDataSetChanged(); - setUpHeader(DEFAULT_WEEK_DAY_TEXT_APPEARANCE_RES_ID); + setUpHeader(); } /** @@ -594,7 +823,7 @@ public class CalendarView extends FrameLayout { mFirstDayOfWeek = firstDayOfWeek; mAdapter.init(); mAdapter.notifyDataSetChanged(); - setUpHeader(DEFAULT_WEEK_DAY_TEXT_APPEARANCE_RES_ID); + setUpHeader(); } /** @@ -655,6 +884,25 @@ public class CalendarView extends FrameLayout { goTo(mTempDate, animate, true, center); } + private void updateDateTextSize() { + TypedArray dateTextAppearance = getContext().obtainStyledAttributes( + mDateTextAppearanceResId, R.styleable.TextAppearance); + mDateTextSize = dateTextAppearance.getDimensionPixelSize( + R.styleable.TextAppearance_textSize, DEFAULT_DATE_TEXT_SIZE); + dateTextAppearance.recycle(); + } + + /** + * Invalidates all week views. + */ + private void invalidateAllWeekViews() { + final int childCount = mListView.getChildCount(); + for (int i = 0; i < childCount; i++) { + View view = mListView.getChildAt(i); + view.invalidate(); + } + } + /** * Sets the current locale. * @@ -727,7 +975,7 @@ public class CalendarView extends FrameLayout { /** * Sets up the strings to be used by the header. */ - private void setUpHeader(int weekDayTextAppearanceResId) { + private void setUpHeader() { mDayLabels = new String[mDaysPerWeek]; for (int i = mFirstDayOfWeek, count = mFirstDayOfWeek + mDaysPerWeek; i < count; i++) { int calendarDay = (i > Calendar.SATURDAY) ? i - Calendar.SATURDAY : i; @@ -743,8 +991,8 @@ public class CalendarView extends FrameLayout { } for (int i = 1, count = mDayNamesHeader.getChildCount(); i < count; i++) { label = (TextView) mDayNamesHeader.getChildAt(i); - if (weekDayTextAppearanceResId > -1) { - label.setTextAppearance(mContext, weekDayTextAppearanceResId); + if (mWeekDayTextAppearanceResId > -1) { + label.setTextAppearance(mContext, mWeekDayTextAppearanceResId); } if (i < mDaysPerWeek + 1) { label.setText(mDayLabels[i - 1]); @@ -1198,6 +1446,12 @@ public class CalendarView extends FrameLayout { // Quick lookup for checking which days are in the focus month private boolean[] mFocusDay; + // Whether this view has a focused day. + private boolean mHasFocusedDay; + + // Whether this view has only focused days. + private boolean mHasUnfocusedDay; + // The first day displayed by this item private Calendar mFirstDay; @@ -1235,11 +1489,8 @@ public class CalendarView extends FrameLayout { public WeekView(Context context) { super(context); - mHeight = (mListView.getHeight() - mListView.getPaddingTop() - mListView - .getPaddingBottom()) / mShownWeekCount; - // Sets up any standard paints that will be used - setPaintProperties(); + initilaizePaints(); } /** @@ -1281,8 +1532,12 @@ public class CalendarView extends FrameLayout { mFirstDay = (Calendar) mTempDate.clone(); mMonthOfFirstWeekDay = mTempDate.get(Calendar.MONTH); + mHasUnfocusedDay = true; for (; i < mNumCells; i++) { - mFocusDay[i] = (mTempDate.get(Calendar.MONTH) == focusedMonth); + final boolean isFocusedDay = (mTempDate.get(Calendar.MONTH) == focusedMonth); + mFocusDay[i] = isFocusedDay; + mHasFocusedDay |= isFocusedDay; + mHasUnfocusedDay &= !isFocusedDay; // do not draw dates outside the valid range to avoid user confusion if (mTempDate.before(mMinDate) || mTempDate.after(mMaxDate)) { mDayNumbers[i] = ""; @@ -1302,18 +1557,15 @@ public class CalendarView extends FrameLayout { } /** - * Sets up the text and style properties for painting. + * Initialize the paint isntances. */ - private void setPaintProperties() { + private void initilaizePaints() { mDrawPaint.setFakeBoldText(false); mDrawPaint.setAntiAlias(true); - mDrawPaint.setTextSize(mDateTextSize); mDrawPaint.setStyle(Style.FILL); mMonthNumDrawPaint.setFakeBoldText(true); mMonthNumDrawPaint.setAntiAlias(true); - mMonthNumDrawPaint.setTextSize(mDateTextSize); - mMonthNumDrawPaint.setColor(mFocusedMonthDateColor); mMonthNumDrawPaint.setStyle(Style.FILL); mMonthNumDrawPaint.setTextAlign(Align.CENTER); } @@ -1369,7 +1621,7 @@ public class CalendarView extends FrameLayout { @Override protected void onDraw(Canvas canvas) { drawBackground(canvas); - drawWeekNumbers(canvas); + drawWeekNumbersAndDates(canvas); drawWeekSeparators(canvas); drawSelectedDateVerticalBars(canvas); } @@ -1401,12 +1653,13 @@ public class CalendarView extends FrameLayout { * * @param canvas The canvas to draw on */ - private void drawWeekNumbers(Canvas canvas) { + private void drawWeekNumbersAndDates(Canvas canvas) { float textHeight = mDrawPaint.getTextSize(); int y = (int) ((mHeight + textHeight) / 2) - mWeekSeperatorLineWidth; int nDays = mNumCells; mDrawPaint.setTextAlign(Align.CENTER); + mDrawPaint.setTextSize(mDateTextSize); int i = 0; int divisor = 2 * nDays; if (mShowWeekNumber) { @@ -1487,6 +1740,8 @@ public class CalendarView extends FrameLayout { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + mHeight = (mListView.getHeight() - mListView.getPaddingTop() - mListView + .getPaddingBottom()) / mShownWeekCount; setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mHeight); } } |
