diff options
Diffstat (limited to 'src/com/android/datetimepicker/date/MonthView.java')
-rw-r--r-- | src/com/android/datetimepicker/date/MonthView.java | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/com/android/datetimepicker/date/MonthView.java b/src/com/android/datetimepicker/date/MonthView.java index ca7fd89..1924890 100644 --- a/src/com/android/datetimepicker/date/MonthView.java +++ b/src/com/android/datetimepicker/date/MonthView.java @@ -120,6 +120,8 @@ public abstract class MonthView extends View { // used for scaling to the device density protected static float mScale = 0; + protected final DatePickerController mController; + // affects the padding on the sides of this view protected int mPadding = 0; @@ -179,12 +181,19 @@ public abstract class MonthView extends View { protected int mDayTextColor; protected int mTodayNumberColor; + protected int mDisabledDayTextColor; protected int mMonthTitleColor; protected int mMonthTitleBGColor; public MonthView(Context context) { + this(context, null); + } + + public MonthView(Context context, DatePickerController controller) { super(context); + mController = controller; + Resources res = context.getResources(); mDayLabelCalendar = Calendar.getInstance(); @@ -195,6 +204,7 @@ public abstract class MonthView extends View { mDayTextColor = res.getColor(R.color.date_picker_text_normal); mTodayNumberColor = res.getColor(R.color.blue); + mDisabledDayTextColor = res.getColor(R.color.date_picker_text_disabled); mMonthTitleColor = res.getColor(R.color.white); mMonthTitleBGColor = res.getColor(R.color.circle_background); @@ -530,10 +540,18 @@ public abstract class MonthView extends View { /** * Called when the user clicks on a day. Handles callbacks to the * {@link OnDayClickListener} if one is set. + * <p/> + * If the day is out of the range set by minDate and/or maxDate, this is a no-op. * * @param day The day that was clicked */ private void onDayClick(int day) { + // If the min / max date are set, only process the click if it's a valid selection. + if (isOutOfRange(mYear, mMonth, day)) { + return; + } + + if (mOnDayClickListener != null) { mOnDayClickListener.onDayClick(this, new CalendarDay(mYear, mMonth, day)); } @@ -543,6 +561,71 @@ public abstract class MonthView extends View { } /** + * @return true if the specified year/month/day are within the range set by minDate and maxDate. + * If one or either have not been set, they are considered as Integer.MIN_VALUE and + * Integer.MAX_VALUE. + */ + protected boolean isOutOfRange(int year, int month, int day) { + if (isBeforeMin(year, month, day)) { + return true; + } else if (isAfterMax(year, month, day)) { + return true; + } + + return false; + } + + private boolean isBeforeMin(int year, int month, int day) { + Calendar minDate = mController.getMinDate(); + if (minDate == null) { + return false; + } + + if (year < minDate.get(Calendar.YEAR)) { + return true; + } else if (year > minDate.get(Calendar.YEAR)) { + return false; + } + + if (month < minDate.get(Calendar.MONTH)) { + return true; + } else if (month > minDate.get(Calendar.MONTH)) { + return false; + } + + if (day < minDate.get(Calendar.DAY_OF_MONTH)) { + return true; + } else { + return false; + } + } + + private boolean isAfterMax(int year, int month, int day) { + Calendar maxDate = mController.getMaxDate(); + if (maxDate == null) { + return false; + } + + if (year > maxDate.get(Calendar.YEAR)) { + return true; + } else if (year < maxDate.get(Calendar.YEAR)) { + return false; + } + + if (month > maxDate.get(Calendar.MONTH)) { + return true; + } else if (month < maxDate.get(Calendar.MONTH)) { + return false; + } + + if (day > maxDate.get(Calendar.DAY_OF_MONTH)) { + return true; + } else { + return false; + } + } + + /** * @return The date that has accessibility focus, or {@code null} if no date * has focus */ |