1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.widget; 18 19 import android.annotation.AttrRes; 20 import android.annotation.ColorInt; 21 import android.annotation.DrawableRes; 22 import android.annotation.NonNull; 23 import android.annotation.Nullable; 24 import android.annotation.StyleRes; 25 import android.annotation.TestApi; 26 import android.annotation.UnsupportedAppUsage; 27 import android.annotation.Widget; 28 import android.content.Context; 29 import android.content.res.Configuration; 30 import android.content.res.TypedArray; 31 import android.graphics.Rect; 32 import android.graphics.drawable.Drawable; 33 import android.icu.util.Calendar; 34 import android.icu.util.TimeZone; 35 import android.util.AttributeSet; 36 import android.util.Log; 37 import android.view.inspector.InspectableProperty; 38 39 import com.android.internal.R; 40 41 import java.text.DateFormat; 42 import java.text.ParseException; 43 import java.text.SimpleDateFormat; 44 import java.util.Date; 45 import java.util.Locale; 46 47 /** 48 * This class is a calendar widget for displaying and selecting dates. The 49 * range of dates supported by this calendar is configurable. 50 * <p> 51 * The exact appearance and interaction model of this widget may vary between 52 * OS versions and themes (e.g. Holo versus Material), but in general a user 53 * can select a date by tapping on it and can scroll or fling the calendar to a 54 * desired date. 55 * 56 * @attr ref android.R.styleable#CalendarView_showWeekNumber 57 * @attr ref android.R.styleable#CalendarView_firstDayOfWeek 58 * @attr ref android.R.styleable#CalendarView_minDate 59 * @attr ref android.R.styleable#CalendarView_maxDate 60 * @attr ref android.R.styleable#CalendarView_shownWeekCount 61 * @attr ref android.R.styleable#CalendarView_selectedWeekBackgroundColor 62 * @attr ref android.R.styleable#CalendarView_focusedMonthDateColor 63 * @attr ref android.R.styleable#CalendarView_unfocusedMonthDateColor 64 * @attr ref android.R.styleable#CalendarView_weekNumberColor 65 * @attr ref android.R.styleable#CalendarView_weekSeparatorLineColor 66 * @attr ref android.R.styleable#CalendarView_selectedDateVerticalBar 67 * @attr ref android.R.styleable#CalendarView_weekDayTextAppearance 68 * @attr ref android.R.styleable#CalendarView_dateTextAppearance 69 */ 70 @Widget 71 public class CalendarView extends FrameLayout { 72 private static final String LOG_TAG = "CalendarView"; 73 74 private static final int MODE_HOLO = 0; 75 private static final int MODE_MATERIAL = 1; 76 77 @UnsupportedAppUsage 78 private final CalendarViewDelegate mDelegate; 79 80 /** 81 * The callback used to indicate the user changes the date. 82 */ 83 public interface OnDateChangeListener { 84 85 /** 86 * Called upon change of the selected day. 87 * 88 * @param view The view associated with this listener. 89 * @param year The year that was set. 90 * @param month The month that was set [0-11]. 91 * @param dayOfMonth The day of the month that was set. 92 */ onSelectedDayChange(@onNull CalendarView view, int year, int month, int dayOfMonth)93 void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth); 94 } 95 CalendarView(@onNull Context context)96 public CalendarView(@NonNull Context context) { 97 this(context, null); 98 } 99 CalendarView(@onNull Context context, @Nullable AttributeSet attrs)100 public CalendarView(@NonNull Context context, @Nullable AttributeSet attrs) { 101 this(context, attrs, R.attr.calendarViewStyle); 102 } 103 CalendarView(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr)104 public CalendarView(@NonNull Context context, @Nullable AttributeSet attrs, 105 @AttrRes int defStyleAttr) { 106 this(context, attrs, defStyleAttr, 0); 107 } 108 CalendarView(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes)109 public CalendarView(@NonNull Context context, @Nullable AttributeSet attrs, 110 @AttrRes int defStyleAttr, @StyleRes int defStyleRes) { 111 super(context, attrs, defStyleAttr, defStyleRes); 112 113 final TypedArray a = context.obtainStyledAttributes( 114 attrs, R.styleable.CalendarView, defStyleAttr, defStyleRes); 115 saveAttributeDataForStyleable(context, R.styleable.CalendarView, 116 attrs, a, defStyleAttr, defStyleRes); 117 final int mode = a.getInt(R.styleable.CalendarView_calendarViewMode, MODE_HOLO); 118 a.recycle(); 119 120 switch (mode) { 121 case MODE_HOLO: 122 mDelegate = new CalendarViewLegacyDelegate( 123 this, context, attrs, defStyleAttr, defStyleRes); 124 break; 125 case MODE_MATERIAL: 126 mDelegate = new CalendarViewMaterialDelegate( 127 this, context, attrs, defStyleAttr, defStyleRes); 128 break; 129 default: 130 throw new IllegalArgumentException("invalid calendarViewMode attribute"); 131 } 132 } 133 134 /** 135 * Sets the number of weeks to be shown. 136 * 137 * @param count The shown week count. 138 * 139 * @attr ref android.R.styleable#CalendarView_shownWeekCount 140 * @deprecated No longer used by Material-style CalendarView. 141 */ 142 @Deprecated setShownWeekCount(int count)143 public void setShownWeekCount(int count) { 144 mDelegate.setShownWeekCount(count); 145 } 146 147 /** 148 * Gets the number of weeks to be shown. 149 * 150 * @return The shown week count. 151 * 152 * @attr ref android.R.styleable#CalendarView_shownWeekCount 153 * @deprecated No longer used by Material-style CalendarView. 154 */ 155 @InspectableProperty 156 @Deprecated getShownWeekCount()157 public int getShownWeekCount() { 158 return mDelegate.getShownWeekCount(); 159 } 160 161 /** 162 * Sets the background color for the selected week. 163 * 164 * @param color The week background color. 165 * 166 * @attr ref android.R.styleable#CalendarView_selectedWeekBackgroundColor 167 * @deprecated No longer used by Material-style CalendarView. 168 */ 169 @Deprecated setSelectedWeekBackgroundColor(@olorInt int color)170 public void setSelectedWeekBackgroundColor(@ColorInt int color) { 171 mDelegate.setSelectedWeekBackgroundColor(color); 172 } 173 174 /** 175 * Gets the background color for the selected week. 176 * 177 * @return The week background color. 178 * 179 * @attr ref android.R.styleable#CalendarView_selectedWeekBackgroundColor 180 * @deprecated No longer used by Material-style CalendarView. 181 */ 182 @InspectableProperty 183 @ColorInt 184 @Deprecated getSelectedWeekBackgroundColor()185 public int getSelectedWeekBackgroundColor() { 186 return mDelegate.getSelectedWeekBackgroundColor(); 187 } 188 189 /** 190 * Sets the color for the dates of the focused month. 191 * 192 * @param color The focused month date color. 193 * 194 * @attr ref android.R.styleable#CalendarView_focusedMonthDateColor 195 * @deprecated No longer used by Material-style CalendarView. 196 */ 197 @Deprecated setFocusedMonthDateColor(@olorInt int color)198 public void setFocusedMonthDateColor(@ColorInt int color) { 199 mDelegate.setFocusedMonthDateColor(color); 200 } 201 202 /** 203 * Gets the color for the dates in the focused month. 204 * 205 * @return The focused month date color. 206 * 207 * @attr ref android.R.styleable#CalendarView_focusedMonthDateColor 208 * @deprecated No longer used by Material-style CalendarView. 209 */ 210 @InspectableProperty 211 @ColorInt 212 @Deprecated getFocusedMonthDateColor()213 public int getFocusedMonthDateColor() { 214 return mDelegate.getFocusedMonthDateColor(); 215 } 216 217 /** 218 * Sets the color for the dates of a not focused month. 219 * 220 * @param color A not focused month date color. 221 * 222 * @attr ref android.R.styleable#CalendarView_unfocusedMonthDateColor 223 * @deprecated No longer used by Material-style CalendarView. 224 */ 225 @Deprecated setUnfocusedMonthDateColor(@olorInt int color)226 public void setUnfocusedMonthDateColor(@ColorInt int color) { 227 mDelegate.setUnfocusedMonthDateColor(color); 228 } 229 230 /** 231 * Gets the color for the dates in a not focused month. 232 * 233 * @return A not focused month date color. 234 * 235 * @attr ref android.R.styleable#CalendarView_unfocusedMonthDateColor 236 * @deprecated No longer used by Material-style CalendarView. 237 */ 238 @InspectableProperty 239 @ColorInt 240 @Deprecated getUnfocusedMonthDateColor()241 public int getUnfocusedMonthDateColor() { 242 return mDelegate.getUnfocusedMonthDateColor(); 243 } 244 245 /** 246 * Sets the color for the week numbers. 247 * 248 * @param color The week number color. 249 * 250 * @attr ref android.R.styleable#CalendarView_weekNumberColor 251 * @deprecated No longer used by Material-style CalendarView. 252 */ 253 @Deprecated setWeekNumberColor(@olorInt int color)254 public void setWeekNumberColor(@ColorInt int color) { 255 mDelegate.setWeekNumberColor(color); 256 } 257 258 /** 259 * Gets the color for the week numbers. 260 * 261 * @return The week number color. 262 * 263 * @attr ref android.R.styleable#CalendarView_weekNumberColor 264 * @deprecated No longer used by Material-style CalendarView. 265 */ 266 @InspectableProperty 267 @ColorInt 268 @Deprecated getWeekNumberColor()269 public int getWeekNumberColor() { 270 return mDelegate.getWeekNumberColor(); 271 } 272 273 /** 274 * Sets the color for the separator line between weeks. 275 * 276 * @param color The week separator color. 277 * 278 * @attr ref android.R.styleable#CalendarView_weekSeparatorLineColor 279 * @deprecated No longer used by Material-style CalendarView. 280 */ 281 @Deprecated setWeekSeparatorLineColor(@olorInt int color)282 public void setWeekSeparatorLineColor(@ColorInt int color) { 283 mDelegate.setWeekSeparatorLineColor(color); 284 } 285 286 /** 287 * Gets the color for the separator line between weeks. 288 * 289 * @return The week separator color. 290 * 291 * @attr ref android.R.styleable#CalendarView_weekSeparatorLineColor 292 * @deprecated No longer used by Material-style CalendarView. 293 */ 294 @ColorInt 295 @Deprecated 296 @InspectableProperty getWeekSeparatorLineColor()297 public int getWeekSeparatorLineColor() { 298 return mDelegate.getWeekSeparatorLineColor(); 299 } 300 301 /** 302 * Sets the drawable for the vertical bar shown at the beginning and at 303 * the end of the selected date. 304 * 305 * @param resourceId The vertical bar drawable resource id. 306 * 307 * @attr ref android.R.styleable#CalendarView_selectedDateVerticalBar 308 * @deprecated No longer used by Material-style CalendarView. 309 */ 310 @Deprecated setSelectedDateVerticalBar(@rawableRes int resourceId)311 public void setSelectedDateVerticalBar(@DrawableRes int resourceId) { 312 mDelegate.setSelectedDateVerticalBar(resourceId); 313 } 314 315 /** 316 * Sets the drawable for the vertical bar shown at the beginning and at 317 * the end of the selected date. 318 * 319 * @param drawable The vertical bar drawable. 320 * 321 * @attr ref android.R.styleable#CalendarView_selectedDateVerticalBar 322 * @deprecated No longer used by Material-style CalendarView. 323 */ 324 @Deprecated setSelectedDateVerticalBar(Drawable drawable)325 public void setSelectedDateVerticalBar(Drawable drawable) { 326 mDelegate.setSelectedDateVerticalBar(drawable); 327 } 328 329 /** 330 * Gets the drawable for the vertical bar shown at the beginning and at 331 * the end of the selected date. 332 * 333 * @return The vertical bar drawable. 334 * @deprecated No longer used by Material-style CalendarView. 335 */ 336 @InspectableProperty 337 @Deprecated getSelectedDateVerticalBar()338 public Drawable getSelectedDateVerticalBar() { 339 return mDelegate.getSelectedDateVerticalBar(); 340 } 341 342 /** 343 * Sets the text appearance for the week day abbreviation of the calendar header. 344 * 345 * @param resourceId The text appearance resource id. 346 * 347 * @attr ref android.R.styleable#CalendarView_weekDayTextAppearance 348 */ setWeekDayTextAppearance(@tyleRes int resourceId)349 public void setWeekDayTextAppearance(@StyleRes int resourceId) { 350 mDelegate.setWeekDayTextAppearance(resourceId); 351 } 352 353 /** 354 * Gets the text appearance for the week day abbreviation of the calendar header. 355 * 356 * @return The text appearance resource id. 357 * 358 * @attr ref android.R.styleable#CalendarView_weekDayTextAppearance 359 */ 360 @InspectableProperty getWeekDayTextAppearance()361 public @StyleRes int getWeekDayTextAppearance() { 362 return mDelegate.getWeekDayTextAppearance(); 363 } 364 365 /** 366 * Sets the text appearance for the calendar dates. 367 * 368 * @param resourceId The text appearance resource id. 369 * 370 * @attr ref android.R.styleable#CalendarView_dateTextAppearance 371 */ setDateTextAppearance(@tyleRes int resourceId)372 public void setDateTextAppearance(@StyleRes int resourceId) { 373 mDelegate.setDateTextAppearance(resourceId); 374 } 375 376 /** 377 * Gets the text appearance for the calendar dates. 378 * 379 * @return The text appearance resource id. 380 * 381 * @attr ref android.R.styleable#CalendarView_dateTextAppearance 382 */ 383 @InspectableProperty getDateTextAppearance()384 public @StyleRes int getDateTextAppearance() { 385 return mDelegate.getDateTextAppearance(); 386 } 387 388 /** 389 * Gets the minimal date supported by this {@link CalendarView} in milliseconds 390 * since January 1, 1970 00:00:00 in {@link TimeZone#getDefault()} time 391 * zone. 392 * <p> 393 * Note: The default minimal date is 01/01/1900. 394 * <p> 395 * 396 * @return The minimal supported date. 397 * 398 * @attr ref android.R.styleable#CalendarView_minDate 399 */ 400 @InspectableProperty getMinDate()401 public long getMinDate() { 402 return mDelegate.getMinDate(); 403 } 404 405 /** 406 * Sets the minimal date supported by this {@link CalendarView} in milliseconds 407 * since January 1, 1970 00:00:00 in {@link TimeZone#getDefault()} time 408 * zone. 409 * 410 * @param minDate The minimal supported date. 411 * 412 * @attr ref android.R.styleable#CalendarView_minDate 413 */ setMinDate(long minDate)414 public void setMinDate(long minDate) { 415 mDelegate.setMinDate(minDate); 416 } 417 418 /** 419 * Gets the maximal date supported by this {@link CalendarView} in milliseconds 420 * since January 1, 1970 00:00:00 in {@link TimeZone#getDefault()} time 421 * zone. 422 * <p> 423 * Note: The default maximal date is 01/01/2100. 424 * <p> 425 * 426 * @return The maximal supported date. 427 * 428 * @attr ref android.R.styleable#CalendarView_maxDate 429 */ 430 @InspectableProperty getMaxDate()431 public long getMaxDate() { 432 return mDelegate.getMaxDate(); 433 } 434 435 /** 436 * Sets the maximal date supported by this {@link CalendarView} in milliseconds 437 * since January 1, 1970 00:00:00 in {@link TimeZone#getDefault()} time 438 * zone. 439 * 440 * @param maxDate The maximal supported date. 441 * 442 * @attr ref android.R.styleable#CalendarView_maxDate 443 */ setMaxDate(long maxDate)444 public void setMaxDate(long maxDate) { 445 mDelegate.setMaxDate(maxDate); 446 } 447 448 /** 449 * Sets whether to show the week number. 450 * 451 * @param showWeekNumber True to show the week number. 452 * @deprecated No longer used by Material-style CalendarView. 453 * 454 * @attr ref android.R.styleable#CalendarView_showWeekNumber 455 */ 456 @Deprecated setShowWeekNumber(boolean showWeekNumber)457 public void setShowWeekNumber(boolean showWeekNumber) { 458 mDelegate.setShowWeekNumber(showWeekNumber); 459 } 460 461 /** 462 * Gets whether to show the week number. 463 * 464 * @return True if showing the week number. 465 * @deprecated No longer used by Material-style CalendarView. 466 * 467 * @attr ref android.R.styleable#CalendarView_showWeekNumber 468 */ 469 @InspectableProperty 470 @Deprecated getShowWeekNumber()471 public boolean getShowWeekNumber() { 472 return mDelegate.getShowWeekNumber(); 473 } 474 475 /** 476 * Gets the first day of week. 477 * 478 * @return The first day of the week conforming to the {@link CalendarView} 479 * APIs. 480 * @see Calendar#MONDAY 481 * @see Calendar#TUESDAY 482 * @see Calendar#WEDNESDAY 483 * @see Calendar#THURSDAY 484 * @see Calendar#FRIDAY 485 * @see Calendar#SATURDAY 486 * @see Calendar#SUNDAY 487 * 488 * @attr ref android.R.styleable#CalendarView_firstDayOfWeek 489 */ 490 @InspectableProperty getFirstDayOfWeek()491 public int getFirstDayOfWeek() { 492 return mDelegate.getFirstDayOfWeek(); 493 } 494 495 /** 496 * Sets the first day of week. 497 * 498 * @param firstDayOfWeek The first day of the week conforming to the 499 * {@link CalendarView} APIs. 500 * @see Calendar#MONDAY 501 * @see Calendar#TUESDAY 502 * @see Calendar#WEDNESDAY 503 * @see Calendar#THURSDAY 504 * @see Calendar#FRIDAY 505 * @see Calendar#SATURDAY 506 * @see Calendar#SUNDAY 507 * 508 * @attr ref android.R.styleable#CalendarView_firstDayOfWeek 509 */ setFirstDayOfWeek(int firstDayOfWeek)510 public void setFirstDayOfWeek(int firstDayOfWeek) { 511 mDelegate.setFirstDayOfWeek(firstDayOfWeek); 512 } 513 514 /** 515 * Sets the listener to be notified upon selected date change. 516 * 517 * @param listener The listener to be notified. 518 */ setOnDateChangeListener(OnDateChangeListener listener)519 public void setOnDateChangeListener(OnDateChangeListener listener) { 520 mDelegate.setOnDateChangeListener(listener); 521 } 522 523 /** 524 * Gets the selected date in milliseconds since January 1, 1970 00:00:00 in 525 * {@link TimeZone#getDefault()} time zone. 526 * 527 * @return The selected date. 528 */ getDate()529 public long getDate() { 530 return mDelegate.getDate(); 531 } 532 533 /** 534 * Sets the selected date in milliseconds since January 1, 1970 00:00:00 in 535 * {@link TimeZone#getDefault()} time zone. 536 * 537 * @param date The selected date. 538 * 539 * @throws IllegalArgumentException of the provided date is before the 540 * minimal or after the maximal date. 541 * 542 * @see #setDate(long, boolean, boolean) 543 * @see #setMinDate(long) 544 * @see #setMaxDate(long) 545 */ setDate(long date)546 public void setDate(long date) { 547 mDelegate.setDate(date); 548 } 549 550 /** 551 * Sets the selected date in milliseconds since January 1, 1970 00:00:00 in 552 * {@link TimeZone#getDefault()} time zone. 553 * 554 * @param date The date. 555 * @param animate Whether to animate the scroll to the current date. 556 * @param center Whether to center the current date even if it is already visible. 557 * 558 * @throws IllegalArgumentException of the provided date is before the 559 * minimal or after the maximal date. 560 * 561 * @see #setMinDate(long) 562 * @see #setMaxDate(long) 563 */ setDate(long date, boolean animate, boolean center)564 public void setDate(long date, boolean animate, boolean center) { 565 mDelegate.setDate(date, animate, center); 566 } 567 568 /** 569 * Retrieves the screen bounds for the specific date in the coordinate system of this 570 * view. If the passed date is being currently displayed, this method returns true and 571 * the caller can query the fields of the passed {@link Rect} object. Otherwise the 572 * method returns false and does not touch the passed {@link Rect} object. 573 * 574 * @hide 575 */ 576 @TestApi getBoundsForDate(long date, Rect outBounds)577 public boolean getBoundsForDate(long date, Rect outBounds) { 578 return mDelegate.getBoundsForDate(date, outBounds); 579 } 580 581 @Override onConfigurationChanged(Configuration newConfig)582 protected void onConfigurationChanged(Configuration newConfig) { 583 super.onConfigurationChanged(newConfig); 584 mDelegate.onConfigurationChanged(newConfig); 585 } 586 587 @Override getAccessibilityClassName()588 public CharSequence getAccessibilityClassName() { 589 return CalendarView.class.getName(); 590 } 591 592 /** 593 * A delegate interface that defined the public API of the CalendarView. Allows different 594 * CalendarView implementations. This would need to be implemented by the CalendarView delegates 595 * for the real behavior. 596 */ 597 private interface CalendarViewDelegate { setShownWeekCount(int count)598 void setShownWeekCount(int count); getShownWeekCount()599 int getShownWeekCount(); 600 setSelectedWeekBackgroundColor(@olorInt int color)601 void setSelectedWeekBackgroundColor(@ColorInt int color); getSelectedWeekBackgroundColor()602 @ColorInt int getSelectedWeekBackgroundColor(); 603 setFocusedMonthDateColor(@olorInt int color)604 void setFocusedMonthDateColor(@ColorInt int color); getFocusedMonthDateColor()605 @ColorInt int getFocusedMonthDateColor(); 606 setUnfocusedMonthDateColor(@olorInt int color)607 void setUnfocusedMonthDateColor(@ColorInt int color); getUnfocusedMonthDateColor()608 @ColorInt int getUnfocusedMonthDateColor(); 609 setWeekNumberColor(@olorInt int color)610 void setWeekNumberColor(@ColorInt int color); getWeekNumberColor()611 @ColorInt int getWeekNumberColor(); 612 setWeekSeparatorLineColor(@olorInt int color)613 void setWeekSeparatorLineColor(@ColorInt int color); getWeekSeparatorLineColor()614 @ColorInt int getWeekSeparatorLineColor(); 615 setSelectedDateVerticalBar(@rawableRes int resourceId)616 void setSelectedDateVerticalBar(@DrawableRes int resourceId); setSelectedDateVerticalBar(Drawable drawable)617 void setSelectedDateVerticalBar(Drawable drawable); getSelectedDateVerticalBar()618 Drawable getSelectedDateVerticalBar(); 619 setWeekDayTextAppearance(@tyleRes int resourceId)620 void setWeekDayTextAppearance(@StyleRes int resourceId); getWeekDayTextAppearance()621 @StyleRes int getWeekDayTextAppearance(); 622 setDateTextAppearance(@tyleRes int resourceId)623 void setDateTextAppearance(@StyleRes int resourceId); getDateTextAppearance()624 @StyleRes int getDateTextAppearance(); 625 setMinDate(long minDate)626 void setMinDate(long minDate); getMinDate()627 long getMinDate(); 628 setMaxDate(long maxDate)629 void setMaxDate(long maxDate); getMaxDate()630 long getMaxDate(); 631 setShowWeekNumber(boolean showWeekNumber)632 void setShowWeekNumber(boolean showWeekNumber); getShowWeekNumber()633 boolean getShowWeekNumber(); 634 setFirstDayOfWeek(int firstDayOfWeek)635 void setFirstDayOfWeek(int firstDayOfWeek); getFirstDayOfWeek()636 int getFirstDayOfWeek(); 637 setDate(long date)638 void setDate(long date); setDate(long date, boolean animate, boolean center)639 void setDate(long date, boolean animate, boolean center); getDate()640 long getDate(); 641 getBoundsForDate(long date, Rect outBounds)642 boolean getBoundsForDate(long date, Rect outBounds); 643 setOnDateChangeListener(OnDateChangeListener listener)644 void setOnDateChangeListener(OnDateChangeListener listener); 645 onConfigurationChanged(Configuration newConfig)646 void onConfigurationChanged(Configuration newConfig); 647 } 648 649 /** 650 * An abstract class which can be used as a start for CalendarView implementations 651 */ 652 abstract static class AbstractCalendarViewDelegate implements CalendarViewDelegate { 653 /** The default minimal date. */ 654 protected static final String DEFAULT_MIN_DATE = "01/01/1900"; 655 656 /** The default maximal date. */ 657 protected static final String DEFAULT_MAX_DATE = "01/01/2100"; 658 659 protected CalendarView mDelegator; 660 protected Context mContext; 661 protected Locale mCurrentLocale; 662 AbstractCalendarViewDelegate(CalendarView delegator, Context context)663 AbstractCalendarViewDelegate(CalendarView delegator, Context context) { 664 mDelegator = delegator; 665 mContext = context; 666 667 // Initialization based on locale 668 setCurrentLocale(Locale.getDefault()); 669 } 670 setCurrentLocale(Locale locale)671 protected void setCurrentLocale(Locale locale) { 672 if (locale.equals(mCurrentLocale)) { 673 return; 674 } 675 mCurrentLocale = locale; 676 } 677 678 @Override setShownWeekCount(int count)679 public void setShownWeekCount(int count) { 680 // Deprecated. 681 } 682 683 @Override getShownWeekCount()684 public int getShownWeekCount() { 685 // Deprecated. 686 return 0; 687 } 688 689 @Override setSelectedWeekBackgroundColor(@olorInt int color)690 public void setSelectedWeekBackgroundColor(@ColorInt int color) { 691 // Deprecated. 692 } 693 694 @ColorInt 695 @Override getSelectedWeekBackgroundColor()696 public int getSelectedWeekBackgroundColor() { 697 return 0; 698 } 699 700 @Override setFocusedMonthDateColor(@olorInt int color)701 public void setFocusedMonthDateColor(@ColorInt int color) { 702 // Deprecated. 703 } 704 705 @ColorInt 706 @Override getFocusedMonthDateColor()707 public int getFocusedMonthDateColor() { 708 return 0; 709 } 710 711 @Override setUnfocusedMonthDateColor(@olorInt int color)712 public void setUnfocusedMonthDateColor(@ColorInt int color) { 713 // Deprecated. 714 } 715 716 @ColorInt 717 @Override getUnfocusedMonthDateColor()718 public int getUnfocusedMonthDateColor() { 719 return 0; 720 } 721 722 @Override setWeekNumberColor(@olorInt int color)723 public void setWeekNumberColor(@ColorInt int color) { 724 // Deprecated. 725 } 726 727 @ColorInt 728 @Override getWeekNumberColor()729 public int getWeekNumberColor() { 730 // Deprecated. 731 return 0; 732 } 733 734 @Override setWeekSeparatorLineColor(@olorInt int color)735 public void setWeekSeparatorLineColor(@ColorInt int color) { 736 // Deprecated. 737 } 738 739 @ColorInt 740 @Override getWeekSeparatorLineColor()741 public int getWeekSeparatorLineColor() { 742 // Deprecated. 743 return 0; 744 } 745 746 @Override setSelectedDateVerticalBar(@rawableRes int resId)747 public void setSelectedDateVerticalBar(@DrawableRes int resId) { 748 // Deprecated. 749 } 750 751 @Override setSelectedDateVerticalBar(Drawable drawable)752 public void setSelectedDateVerticalBar(Drawable drawable) { 753 // Deprecated. 754 } 755 756 @Override getSelectedDateVerticalBar()757 public Drawable getSelectedDateVerticalBar() { 758 // Deprecated. 759 return null; 760 } 761 762 @Override setShowWeekNumber(boolean showWeekNumber)763 public void setShowWeekNumber(boolean showWeekNumber) { 764 // Deprecated. 765 } 766 767 @Override getShowWeekNumber()768 public boolean getShowWeekNumber() { 769 // Deprecated. 770 return false; 771 } 772 773 @Override onConfigurationChanged(Configuration newConfig)774 public void onConfigurationChanged(Configuration newConfig) { 775 // Nothing to do here, configuration changes are already propagated 776 // by ViewGroup. 777 } 778 } 779 780 /** String for parsing dates. */ 781 private static final String DATE_FORMAT = "MM/dd/yyyy"; 782 783 /** Date format for parsing dates. */ 784 private static final DateFormat DATE_FORMATTER = new SimpleDateFormat(DATE_FORMAT); 785 786 /** 787 * Utility method for the date format used by CalendarView's min/max date. 788 * 789 * @hide Use only as directed. For internal use only. 790 */ parseDate(String date, Calendar outDate)791 public static boolean parseDate(String date, Calendar outDate) { 792 if (date == null || date.isEmpty()) { 793 return false; 794 } 795 796 try { 797 final Date parsedDate = DATE_FORMATTER.parse(date); 798 outDate.setTime(parsedDate); 799 return true; 800 } catch (ParseException e) { 801 Log.w(LOG_TAG, "Date: " + date + " not in format: " + DATE_FORMAT); 802 return false; 803 } 804 } 805 } 806