CalendarPickerView.java 27.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
// Copyright 2012 Square, Inc.
package com.squareup.timessquare;

import static java.util.Calendar.DATE;
import static java.util.Calendar.DAY_OF_MONTH;
import static java.util.Calendar.DAY_OF_WEEK;
import static java.util.Calendar.HOUR_OF_DAY;
import static java.util.Calendar.MILLISECOND;
import static java.util.Calendar.MINUTE;
import static java.util.Calendar.MONTH;
import static java.util.Calendar.SECOND;
import static java.util.Calendar.YEAR;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.ectrip.trips.check.R;
import com.squareup.timessquare.MonthCellDescriptor.RangeState;

/**
 * Android component to allow picking a date from a calendar view (a list of
 * months). Must be initialized after inflation with {@link #init(Date, Date)}
 * and can be customized with any of the {@link FluentInitializer} methods
 * returned. The currently selected date can be retrieved with
 * {@link #getSelectedDate()}.
 */
public class CalendarPickerView extends ListView {
	public enum SelectionMode {
		/**
		 * Only one date will be selectable. If there is already a selected date
		 * and you select a new one, the old date will be unselected.
		 */
		SINGLE,
		/**
		 * Multiple dates will be selectable. Selecting an already-selected date
		 * will un-select it.
		 */
		MULTIPLE,
		/**
		 * Allows you to select a date range. Previous selections are cleared
		 * when you either:
		 * <ul>
		 * <li>Have a range selected and select another date (even if it's in
		 * the current range).</li>
		 * <li>Have one date selected and then select an earlier date.</li>
		 * </ul>
		 */
		RANGE
	}

	private final CalendarPickerView.MonthAdapter adapter;
	private final List<List<List<MonthCellDescriptor>>> cells = new ArrayList<List<List<MonthCellDescriptor>>>();
	final MonthView.Listener listener = new CellClickedListener();
	final List<MonthDescriptor> months = new ArrayList<MonthDescriptor>();
	final List<MonthCellDescriptor> selectedCells = new ArrayList<MonthCellDescriptor>();
	final List<MonthCellDescriptor> highlightedCells = new ArrayList<MonthCellDescriptor>();
	final List<Calendar> selectedCals = new ArrayList<Calendar>();
	final List<Calendar> highlightedCals = new ArrayList<Calendar>();
	private Locale locale;
	private DateFormat monthNameFormat;
	private DateFormat weekdayNameFormat;
	private DateFormat fullDateFormat;
	private Calendar minCal;
	private Calendar maxCal;
	private Calendar monthCounter;
	private boolean displayOnly;
	SelectionMode selectionMode;
	Calendar today;
	private int dividerColor;
	private int dayBackgroundResId;
	private int dayTextColorResId;
	private int titleTextColor;
	private int headerTextColor;

	private OnDateSelectedListener dateListener;
	private DateSelectableFilter dateConfiguredListener;
	private OnInvalidDateSelectedListener invalidDateListener = new DefaultOnInvalidDateSelectedListener();

	public CalendarPickerView(Context context, AttributeSet attrs) {
		super(context, attrs);

		Resources res = context.getResources();
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.CalendarPickerView);
		final int bg = a.getColor(
				R.styleable.CalendarPickerView_android_background,
				res.getColor(R.color.calendar_bg));
		dividerColor = a.getColor(R.styleable.CalendarPickerView_dividerColor,
				res.getColor(R.color.calendar_divider));
		dayBackgroundResId = a.getResourceId(
				R.styleable.CalendarPickerView_dayBackground,
				R.drawable.mr_calendar_bg_selector);
		dayTextColorResId = a.getResourceId(
				R.styleable.CalendarPickerView_dayTextColor,
				R.color.mr_calendar_text_selector);
		titleTextColor = a.getColor(
				R.styleable.CalendarPickerView_titleTextColor,
				R.color.calendar_text_active);
		headerTextColor = a.getColor(
				R.styleable.CalendarPickerView_headerTextColor,
				R.color.calendar_text_active);
		a.recycle();

		adapter = new MonthAdapter();
		setDivider(null);
		setDividerHeight(0);
		setBackgroundColor(bg);
		setCacheColorHint(bg);
		locale = Locale.getDefault();
		today = Calendar.getInstance(locale);
		minCal = Calendar.getInstance(locale);
		maxCal = Calendar.getInstance(locale);
		monthCounter = Calendar.getInstance(locale);
		monthNameFormat = new SimpleDateFormat(
				context.getString(R.string.month_name_format), locale);
		weekdayNameFormat = new SimpleDateFormat(
				context.getString(R.string.day_name_format), locale);
		fullDateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);

		if (isInEditMode()) {
			Calendar nextYear = Calendar.getInstance(locale);
			nextYear.add(Calendar.YEAR, 1);

			init(new Date(), nextYear.getTime()) //
					.withSelectedDate(new Date());
		}
	}

	/**
	 * Both date parameters must be non-null and their {@link Date#getTime()}
	 * must not return 0. Time of day will be ignored. For instance, if you pass
	 * in {@code minDate} as 11/16/2012 5:15pm and {@code maxDate} as 11/16/2013
	 * 4:30am, 11/16/2012 will be the first selectable date and 11/15/2013 will
	 * be the last selectable date ({@code maxDate} is exclusive).
	 * <p>
	 * This will implicitly set the {@link SelectionMode} to
	 * {@link SelectionMode#SINGLE}. If you want a different selection mode, use
	 * {@link FluentInitializer#inMode(SelectionMode)} on the
	 * {@link FluentInitializer} this method returns.
	 * <p>
	 * The calendar will be constructed using the given locale. This means that
	 * all names (months, days) will be in the language of the locale and the
	 * weeks start with the day specified by the locale.
	 * 
	 * @param minDate
	 *            Earliest selectable date, inclusive. Must be earlier than
	 *            {@code maxDate}.
	 * @param maxDate
	 *            Latest selectable date, exclusive. Must be later than
	 *            {@code minDate}.
	 */
	public FluentInitializer init(Date minDate, Date maxDate, Locale locale) {
		if (minDate == null || maxDate == null) {
			throw new IllegalArgumentException(
					"minDate and maxDate must be non-null.  "
							+ dbg(minDate, maxDate));
		}
		if (minDate.after(maxDate)) {
			throw new IllegalArgumentException(
					"minDate must be before maxDate.  " + dbg(minDate, maxDate));
		}
		if (minDate.getTime() == 0 || maxDate.getTime() == 0) {
			throw new IllegalArgumentException(
					"minDate and maxDate must be non-zero.  "
							+ dbg(minDate, maxDate));
		}
		if (locale == null) {
			throw new IllegalArgumentException("Locale is null.");
		}

		// Make sure that all calendar instances use the same locale.
		this.locale = locale;
		today = Calendar.getInstance(locale);
		minCal = Calendar.getInstance(locale);
		maxCal = Calendar.getInstance(locale);
		monthCounter = Calendar.getInstance(locale);
		monthNameFormat = new SimpleDateFormat(getContext().getString(
				R.string.month_name_format), locale);
		for (MonthDescriptor month : months) {
			month.setLabel(monthNameFormat.format(month.getDate()));
		}
		weekdayNameFormat = new SimpleDateFormat(getContext().getString(
				R.string.day_name_format), locale);
		fullDateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);

		this.selectionMode = SelectionMode.SINGLE;
		// Clear out any previously-selected dates/cells.
		selectedCals.clear();
		selectedCells.clear();
		highlightedCells.clear();

		// Clear previous state.
		cells.clear();
		months.clear();
		minCal.setTime(minDate);
		maxCal.setTime(maxDate);
		setMidnight(minCal);
		setMidnight(maxCal);
		displayOnly = false;

		// maxDate is exclusive: bump back to the previous day so if maxDate is
		// the first of a month,
		// we don't accidentally include that month in the view.
		maxCal.add(MINUTE, -1);

		// Now iterate between minCal and maxCal and build up our list of months
		// to show.
		monthCounter.setTime(minCal.getTime());
		final int maxMonth = maxCal.get(MONTH);
		final int maxYear = maxCal.get(YEAR);
		while ((monthCounter.get(MONTH) <= maxMonth // Up to, including the
													// month.
				|| monthCounter.get(YEAR) < maxYear) // Up to the year.
				&& monthCounter.get(YEAR) < maxYear + 1) { // But not > next yr.
			Date date = monthCounter.getTime();
			MonthDescriptor month = new MonthDescriptor(
					monthCounter.get(MONTH), monthCounter.get(YEAR), date,
					monthNameFormat.format(date));
			cells.add(getMonthCells(month, monthCounter));
			months.add(month);
			monthCounter.add(MONTH, 1);
		}

		validateAndUpdate();
		return new FluentInitializer();
	}

	/**
	 * Both date parameters must be non-null and their {@link Date#getTime()}
	 * must not return 0. Time of day will be ignored. For instance, if you pass
	 * in {@code minDate} as 11/16/2012 5:15pm and {@code maxDate} as 11/16/2013
	 * 4:30am, 11/16/2012 will be the first selectable date and 11/15/2013 will
	 * be the last selectable date ({@code maxDate} is exclusive).
	 * <p>
	 * This will implicitly set the {@link SelectionMode} to
	 * {@link SelectionMode#SINGLE}. If you want a different selection mode, use
	 * {@link FluentInitializer#inMode(SelectionMode)} on the
	 * {@link FluentInitializer} this method returns.
	 * <p>
	 * The calendar will be constructed using the default locale as returned by
	 * {@link java.util.Locale#getDefault()}. If you wish the calendar to be
	 * constructed using a different locale, use
	 * {@link #init(java.util.Date, java.util.Date, java.util.Locale)}.
	 * 
	 * @param minDate
	 *            Earliest selectable date, inclusive. Must be earlier than
	 *            {@code maxDate}.
	 * @param maxDate
	 *            Latest selectable date, exclusive. Must be later than
	 *            {@code minDate}.
	 */
	public FluentInitializer init(Date minDate, Date maxDate) {
		return init(minDate, maxDate, Locale.getDefault());
	}

	public class FluentInitializer {
		/**
		 * Override the {@link SelectionMode} from the default (
		 * {@link SelectionMode#SINGLE}).
		 */
		public FluentInitializer inMode(SelectionMode mode) {
			selectionMode = mode;
			validateAndUpdate();
			return this;
		}

		/**
		 * Set an initially-selected date. The calendar will scroll to that date
		 * if it's not already visible.
		 */
		public FluentInitializer withSelectedDate(Date selectedDates) {
			return withSelectedDates(Arrays.asList(selectedDates));
		}

		/**
		 * Set multiple selected dates. This will throw an
		 * {@link IllegalArgumentException} if you pass in multiple dates and
		 * haven't already called {@link #inMode(SelectionMode)}.
		 */
		public FluentInitializer withSelectedDates(
				Collection<Date> selectedDates) {
			if (selectionMode == SelectionMode.SINGLE
					&& selectedDates.size() > 1) {
				throw new IllegalArgumentException(
						"SINGLE mode can't be used with multiple selectedDates");
			}
			if (selectedDates != null) {
				for (Date date : selectedDates) {
					selectDate(date);
				}
			}
			scrollToSelectedDates();

			validateAndUpdate();
			return this;
		}

		public FluentInitializer withHighlightedDates(Collection<Date> dates) {
			highlightDates(dates);
			return this;
		}

		public FluentInitializer withHighlightedDate(Date date) {
			return withHighlightedDates(Arrays.asList(date));
		}

		public FluentInitializer displayOnly() {
			displayOnly = true;
			return this;
		}
	}

	private void validateAndUpdate() {
		if (getAdapter() == null) {
			setAdapter(adapter);
		}
		adapter.notifyDataSetChanged();
	}

	private void scrollToSelectedMonth(final int selectedIndex) {
		post(new Runnable() {
			@Override
			public void run() {
				setSelection(selectedIndex);
			}
		});
	}

	private void scrollToSelectedDates() {
		Integer selectedIndex = null;
		Integer todayIndex = null;
		Calendar today = Calendar.getInstance(locale);
		for (int c = 0; c < months.size(); c++) {
			MonthDescriptor month = months.get(c);
			if (selectedIndex == null) {
				for (Calendar selectedCal : selectedCals) {
					if (sameMonth(selectedCal, month)) {
						selectedIndex = c;
						break;
					}
				}
				if (selectedIndex == null && todayIndex == null
						&& sameMonth(today, month)) {
					todayIndex = c;
				}
			}
		}
		if (selectedIndex != null) {
			scrollToSelectedMonth(selectedIndex);
		} else if (todayIndex != null) {
			scrollToSelectedMonth(todayIndex);
		}
	}

	/**
	 * This method should only be called if the calendar is contained in a
	 * dialog, and it should only be called once, right after the dialog is
	 * shown (using {@link android.content.DialogInterface.OnShowListener} or
	 * {@link android.app.DialogFragment#onStart()}).
	 */
	public void fixDialogDimens() {
		// Fix the layout height/width after the dialog has been shown.
		getLayoutParams().height = getMeasuredHeight();
		getLayoutParams().width = getMeasuredWidth();
		// Post this runnable so it runs _after_ the dimen changes have been
		// applied/re-measured.
		post(new Runnable() {
			@Override
			public void run() {
				scrollToSelectedDates();
			}
		});
	}

	/**
	 * This method should only be called if the calendar is contained in a
	 * dialog, and it should only be called when the screen has been rotated and
	 * the dialog should be re-measured.
	 */
	public void unfixDialogDimens() {
		// Fix the layout height/width after the dialog has been shown.
		getLayoutParams().height = LayoutParams.MATCH_PARENT;
		getLayoutParams().width = LayoutParams.MATCH_PARENT;
		requestLayout();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		if (months.isEmpty()) {
			throw new IllegalStateException(
					"Must have at least one month to display.  Did you forget to call init()?");
		}
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	public Date getSelectedDate() {
		return (selectedCals.size() > 0 ? selectedCals.get(0).getTime() : null);
	}

	public List<Date> getSelectedDates() {
		List<Date> selectedDates = new ArrayList<Date>();
		for (MonthCellDescriptor cal : selectedCells) {
			selectedDates.add(cal.getDate());
		}
		Collections.sort(selectedDates);
		return selectedDates;
	}

	/** Returns a string summarizing what the client sent us for init() params. */
	private static String dbg(Date minDate, Date maxDate) {
		return "minDate: " + minDate + "\nmaxDate: " + maxDate;
	}

	/** Clears out the hours/minutes/seconds/millis of a Calendar. */
	static void setMidnight(Calendar cal) {
		cal.set(HOUR_OF_DAY, 0);
		cal.set(MINUTE, 0);
		cal.set(SECOND, 0);
		cal.set(MILLISECOND, 0);
	}

	private class CellClickedListener implements MonthView.Listener {
		@Override
		public void handleClick(MonthCellDescriptor cell) {
			Date clickedDate = cell.getDate();

			if (!betweenDates(clickedDate, minCal, maxCal)
					|| !isDateSelectable(clickedDate)) {
				if (invalidDateListener != null) {
					invalidDateListener.onInvalidDateSelected(clickedDate);
				}
			} else {
				boolean wasSelected = doSelectDate(clickedDate, cell);

				if (dateListener != null) {
					if (wasSelected) {
						dateListener.onDateSelected(clickedDate);
					} else {
						dateListener.onDateUnselected(clickedDate);
					}
				}
			}
		}
	}

	/**
	 * Select a new date. Respects the {@link SelectionMode} this
	 * CalendarPickerView is configured with: if you are in
	 * {@link SelectionMode#SINGLE}, the previously selected date will be
	 * un-selected. In {@link SelectionMode#MULTIPLE}, the new date will be
	 * added to the list of selected dates.
	 * <p>
	 * If the selection was made (selectable date, in range), the view will
	 * scroll to the newly selected date if it's not already visible.
	 * 
	 * @return - whether we were able to set the date
	 */
	public boolean selectDate(Date date) {
		validateDate(date);

		MonthCellWithMonthIndex monthCellWithMonthIndex = getMonthCellWithIndexByDate(date);
		if (monthCellWithMonthIndex == null || !isDateSelectable(date)) {
			return false;
		}
		boolean wasSelected = doSelectDate(date, monthCellWithMonthIndex.cell);
		if (wasSelected) {
			scrollToSelectedMonth(monthCellWithMonthIndex.monthIndex);
		}
		return wasSelected;
	}

	private void validateDate(Date date) {
		if (date == null) {
			throw new IllegalArgumentException(
					"Selected date must be non-null.");
		}
		if (date.getTime() == 0) {
			throw new IllegalArgumentException(
					"Selected date must be non-zero.  " + date);
		}
		if (date.before(minCal.getTime()) || date.after(maxCal.getTime())) {
			throw new IllegalArgumentException(String.format(
					"SelectedDate must be between minDate and maxDate."
							+ "%nminDate: %s%nmaxDate: %s%nselectedDate: %s",
					minCal.getTime(), maxCal.getTime(), date));
		}
	}

	private boolean doSelectDate(Date date, MonthCellDescriptor cell) {
		Calendar newlySelectedCal = Calendar.getInstance(locale);
		newlySelectedCal.setTime(date);
		// Sanitize input: clear out the hours/minutes/seconds/millis.
		setMidnight(newlySelectedCal);

		// Clear any remaining range state.
		for (MonthCellDescriptor selectedCell : selectedCells) {
			selectedCell.setRangeState(RangeState.NONE);
		}

		switch (selectionMode) {
		case RANGE:
			if (selectedCals.size() > 1) {
				// We've already got a range selected: clear the old one.
				clearOldSelections();
			} else if (selectedCals.size() == 1
					&& newlySelectedCal.before(selectedCals.get(0))) {
				// We're moving the start of the range back in time: clear the
				// old start date.
				clearOldSelections();
			}
			break;

		case MULTIPLE:
			date = applyMultiSelect(date, newlySelectedCal);
			break;

		case SINGLE:
			clearOldSelections();
			break;
		default:
			throw new IllegalStateException("Unknown selectionMode "
					+ selectionMode);
		}

		if (date != null) {
			// Select a new cell.
			if (selectedCells.size() == 0 || !selectedCells.get(0).equals(cell)) {
				selectedCells.add(cell);
				cell.setSelected(true);
			}
			selectedCals.add(newlySelectedCal);

			if (selectionMode == SelectionMode.RANGE
					&& selectedCells.size() > 1) {
				// Select all days in between start and end.
				Date start = selectedCells.get(0).getDate();
				Date end = selectedCells.get(1).getDate();
				selectedCells.get(0).setRangeState(
						MonthCellDescriptor.RangeState.FIRST);
				selectedCells.get(1).setRangeState(
						MonthCellDescriptor.RangeState.LAST);

				for (List<List<MonthCellDescriptor>> month : cells) {
					for (List<MonthCellDescriptor> week : month) {
						for (MonthCellDescriptor singleCell : week) {
							if (singleCell.getDate().after(start)
									&& singleCell.getDate().before(end)
									&& singleCell.isSelectable()) {
								singleCell.setSelected(true);
								singleCell
										.setRangeState(MonthCellDescriptor.RangeState.MIDDLE);
								selectedCells.add(singleCell);
							}
						}
					}
				}
			}
		}

		// Update the adapter.
		validateAndUpdate();
		return date != null;
	}

	private void clearOldSelections() {
		for (MonthCellDescriptor selectedCell : selectedCells) {
			// De-select the currently-selected cell.
			selectedCell.setSelected(false);
		}
		selectedCells.clear();
		selectedCals.clear();
	}

	private Date applyMultiSelect(Date date, Calendar selectedCal) {
		for (MonthCellDescriptor selectedCell : selectedCells) {
			if (selectedCell.getDate().equals(date)) {
				// De-select the currently-selected cell.
				selectedCell.setSelected(false);
				selectedCells.remove(selectedCell);
				date = null;
				break;
			}
		}
		for (Calendar cal : selectedCals) {
			if (sameDate(cal, selectedCal)) {
				selectedCals.remove(cal);
				break;
			}
		}
		return date;
	}

	public void highlightDates(Collection<Date> dates) {
		for (Date date : dates) {
			validateDate(date);

			MonthCellWithMonthIndex monthCellWithMonthIndex = getMonthCellWithIndexByDate(date);
			if (monthCellWithMonthIndex != null) {
				Calendar newlyHighlightedCal = Calendar.getInstance();
				newlyHighlightedCal.setTime(date);
				MonthCellDescriptor cell = monthCellWithMonthIndex.cell;

				highlightedCells.add(cell);
				highlightedCals.add(newlyHighlightedCal);
				cell.setHighlighted(true);
			}
		}

		adapter.notifyDataSetChanged();
		setAdapter(adapter);
	}

	/** Hold a cell with a month-index. */
	private static class MonthCellWithMonthIndex {
		public MonthCellDescriptor cell;
		public int monthIndex;

		public MonthCellWithMonthIndex(MonthCellDescriptor cell, int monthIndex) {
			this.cell = cell;
			this.monthIndex = monthIndex;
		}
	}

	/** Return cell and month-index (for scrolling) for a given Date. */
	private MonthCellWithMonthIndex getMonthCellWithIndexByDate(Date date) {
		int index = 0;
		Calendar searchCal = Calendar.getInstance(locale);
		searchCal.setTime(date);
		Calendar actCal = Calendar.getInstance(locale);

		for (List<List<MonthCellDescriptor>> monthCells : cells) {
			for (List<MonthCellDescriptor> weekCells : monthCells) {
				for (MonthCellDescriptor actCell : weekCells) {
					actCal.setTime(actCell.getDate());
					if (sameDate(actCal, searchCal) && actCell.isSelectable()) {
						return new MonthCellWithMonthIndex(actCell, index);
					}
				}
			}
			index++;
		}
		return null;
	}

	private class MonthAdapter extends BaseAdapter {
		private final LayoutInflater inflater;

		private MonthAdapter() {
			inflater = LayoutInflater.from(getContext());
		}

		@Override
		public boolean isEnabled(int position) {
			// Disable selectability: each cell will handle that itself.
			return false;
		}

		@Override
		public int getCount() {
			return months.size();
		}

		@Override
		public Object getItem(int position) {
			return months.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MonthView monthView = (MonthView) convertView;
			if (monthView == null) {
				monthView = MonthView.create(parent, inflater,
						weekdayNameFormat, listener, today, dividerColor,
						dayBackgroundResId, dayTextColorResId, titleTextColor,
						headerTextColor);
			}
			monthView.init(months.get(position), cells.get(position),
					displayOnly);
			return monthView;
		}
	}

	List<List<MonthCellDescriptor>> getMonthCells(MonthDescriptor month,
			Calendar startCal) {
		Calendar cal = Calendar.getInstance(locale);
		cal.setTime(startCal.getTime());
		List<List<MonthCellDescriptor>> cells = new ArrayList<List<MonthCellDescriptor>>();
		cal.set(DAY_OF_MONTH, 1);
		int firstDayOfWeek = cal.get(DAY_OF_WEEK);
		int offset = cal.getFirstDayOfWeek() - firstDayOfWeek;
		if (offset > 0) {
			offset -= 7;
		}
		cal.add(Calendar.DATE, offset);

		Calendar minSelectedCal = minDate(selectedCals);
		Calendar maxSelectedCal = maxDate(selectedCals);

		while ((cal.get(MONTH) < month.getMonth() + 1 || cal.get(YEAR) < month
				.getYear()) //
				&& cal.get(YEAR) <= month.getYear()) {
			List<MonthCellDescriptor> weekCells = new ArrayList<MonthCellDescriptor>();
			cells.add(weekCells);
			for (int c = 0; c < 7; c++) {
				Date date = cal.getTime();
				boolean isCurrentMonth = cal.get(MONTH) == month.getMonth();
				boolean isSelected = isCurrentMonth
						&& containsDate(selectedCals, cal);
				boolean isSelectable = isCurrentMonth
						&& betweenDates(cal, minCal, maxCal)
						&& isDateSelectable(date);
				boolean isToday = sameDate(cal, today);
				boolean isHighlighted = containsDate(highlightedCals, cal);
				int value = cal.get(DAY_OF_MONTH);

				MonthCellDescriptor.RangeState rangeState = MonthCellDescriptor.RangeState.NONE;
				if (selectedCals.size() > 1) {
					if (sameDate(minSelectedCal, cal)) {
						rangeState = MonthCellDescriptor.RangeState.FIRST;
					} else if (sameDate(maxDate(selectedCals), cal)) {
						rangeState = MonthCellDescriptor.RangeState.LAST;
					} else if (betweenDates(cal, minSelectedCal, maxSelectedCal)) {
						rangeState = MonthCellDescriptor.RangeState.MIDDLE;
					}
				}

				weekCells.add(new MonthCellDescriptor(date, isCurrentMonth,
						isSelectable, isSelected, isToday, isHighlighted,
						value, rangeState));
				cal.add(DATE, 1);
			}
		}
		return cells;
	}

	private static boolean containsDate(List<Calendar> selectedCals,
			Calendar cal) {
		for (Calendar selectedCal : selectedCals) {
			if (sameDate(cal, selectedCal)) {
				return true;
			}
		}
		return false;
	}

	private static Calendar minDate(List<Calendar> selectedCals) {
		if (selectedCals == null || selectedCals.size() == 0) {
			return null;
		}
		Collections.sort(selectedCals);
		return selectedCals.get(0);
	}

	private static Calendar maxDate(List<Calendar> selectedCals) {
		if (selectedCals == null || selectedCals.size() == 0) {
			return null;
		}
		Collections.sort(selectedCals);
		return selectedCals.get(selectedCals.size() - 1);
	}

	private static boolean sameDate(Calendar cal, Calendar selectedDate) {
		return cal.get(MONTH) == selectedDate.get(MONTH)
				&& cal.get(YEAR) == selectedDate.get(YEAR)
				&& cal.get(DAY_OF_MONTH) == selectedDate.get(DAY_OF_MONTH);
	}

	private static boolean betweenDates(Calendar cal, Calendar minCal,
			Calendar maxCal) {
		final Date date = cal.getTime();
		return betweenDates(date, minCal, maxCal);
	}

	static boolean betweenDates(Date date, Calendar minCal, Calendar maxCal) {
		final Date min = minCal.getTime();
		return (date.equals(min) || date.after(min)) // >= minCal
				&& date.before(maxCal.getTime()); // && < maxCal
	}

	private static boolean sameMonth(Calendar cal, MonthDescriptor month) {
		return (cal.get(MONTH) == month.getMonth() && cal.get(YEAR) == month
				.getYear());
	}

	private boolean isDateSelectable(Date date) {
		return dateConfiguredListener == null
				|| dateConfiguredListener.isDateSelectable(date);
	}

	public void setOnDateSelectedListener(OnDateSelectedListener listener) {
		dateListener = listener;
	}

	/**
	 * Set a listener to react to user selection of a disabled date.
	 * 
	 * @param listener
	 *            the listener to set, or null for no reaction
	 */
	public void setOnInvalidDateSelectedListener(
			OnInvalidDateSelectedListener listener) {
		invalidDateListener = listener;
	}

	/**
	 * Set a listener used to discriminate between selectable and unselectable
	 * dates. Set this to disable arbitrary dates as they are rendered.
	 * <p>
	 * Important: set this before you call {@link #init(Date, Date)} methods. If
	 * called afterwards, it will not be consistently applied.
	 */
	public void setDateSelectableFilter(DateSelectableFilter listener) {
		dateConfiguredListener = listener;
	}

	/**
	 * Interface to be notified when a new date is selected or unselected. This
	 * will only be called when the user initiates the date selection. If you
	 * call {@link #selectDate(Date)} this listener will not be notified.
	 * 
	 * @see #setOnDateSelectedListener(OnDateSelectedListener)
	 */
	public interface OnDateSelectedListener {
		void onDateSelected(Date date);

		void onDateUnselected(Date date);
	}

	/**
	 * Interface to be notified when an invalid date is selected by the user.
	 * This will only be called when the user initiates the date selection. If
	 * you call {@link #selectDate(Date)} this listener will not be notified.
	 * 
	 * @see #setOnInvalidDateSelectedListener(OnInvalidDateSelectedListener)
	 */
	public interface OnInvalidDateSelectedListener {
		void onInvalidDateSelected(Date date);
	}

	/**
	 * Interface used for determining the selectability of a date cell when it
	 * is configured for display on the calendar.
	 * 
	 * @see #setDateSelectableFilter(DateSelectableFilter)
	 */
	public interface DateSelectableFilter {
		boolean isDateSelectable(Date date);
	}

	private class DefaultOnInvalidDateSelectedListener implements
			OnInvalidDateSelectedListener {
		@Override
		public void onInvalidDateSelected(Date date) {
			String errMessage = getResources().getString(R.string.invalid_date,
					fullDateFormat.format(minCal.getTime()),
					fullDateFormat.format(maxCal.getTime()));
			Toast.makeText(getContext(), errMessage, Toast.LENGTH_SHORT).show();
		}
	}
}