1 /* 2 * Copyright (C) 2014 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.StyleRes; 20 import android.content.Context; 21 import android.graphics.Rect; 22 import android.icu.util.Calendar; 23 import android.util.AttributeSet; 24 import android.widget.DayPickerView.OnDaySelectedListener; 25 26 class CalendarViewMaterialDelegate extends CalendarView.AbstractCalendarViewDelegate { 27 private final DayPickerView mDayPickerView; 28 29 private CalendarView.OnDateChangeListener mOnDateChangeListener; 30 CalendarViewMaterialDelegate(CalendarView delegator, Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)31 public CalendarViewMaterialDelegate(CalendarView delegator, Context context, AttributeSet attrs, 32 int defStyleAttr, int defStyleRes) { 33 super(delegator, context); 34 35 mDayPickerView = new DayPickerView(context, attrs, defStyleAttr, defStyleRes); 36 mDayPickerView.setOnDaySelectedListener(mOnDaySelectedListener); 37 38 delegator.addView(mDayPickerView); 39 } 40 41 @Override setWeekDayTextAppearance(@tyleRes int resId)42 public void setWeekDayTextAppearance(@StyleRes int resId) { 43 mDayPickerView.setDayOfWeekTextAppearance(resId); 44 } 45 46 @StyleRes 47 @Override getWeekDayTextAppearance()48 public int getWeekDayTextAppearance() { 49 return mDayPickerView.getDayOfWeekTextAppearance(); 50 } 51 52 @Override setDateTextAppearance(@tyleRes int resId)53 public void setDateTextAppearance(@StyleRes int resId) { 54 mDayPickerView.setDayTextAppearance(resId); 55 } 56 57 @StyleRes 58 @Override getDateTextAppearance()59 public int getDateTextAppearance() { 60 return mDayPickerView.getDayTextAppearance(); 61 } 62 63 @Override setMinDate(long minDate)64 public void setMinDate(long minDate) { 65 mDayPickerView.setMinDate(minDate); 66 } 67 68 @Override getMinDate()69 public long getMinDate() { 70 return mDayPickerView.getMinDate(); 71 } 72 73 @Override setMaxDate(long maxDate)74 public void setMaxDate(long maxDate) { 75 mDayPickerView.setMaxDate(maxDate); 76 } 77 78 @Override getMaxDate()79 public long getMaxDate() { 80 return mDayPickerView.getMaxDate(); 81 } 82 83 @Override setFirstDayOfWeek(int firstDayOfWeek)84 public void setFirstDayOfWeek(int firstDayOfWeek) { 85 mDayPickerView.setFirstDayOfWeek(firstDayOfWeek); 86 } 87 88 @Override getFirstDayOfWeek()89 public int getFirstDayOfWeek() { 90 return mDayPickerView.getFirstDayOfWeek(); 91 } 92 93 @Override setDate(long date)94 public void setDate(long date) { 95 mDayPickerView.setDate(date, true); 96 } 97 98 @Override setDate(long date, boolean animate, boolean center)99 public void setDate(long date, boolean animate, boolean center) { 100 mDayPickerView.setDate(date, animate); 101 } 102 103 @Override getDate()104 public long getDate() { 105 return mDayPickerView.getDate(); 106 } 107 108 @Override setOnDateChangeListener(CalendarView.OnDateChangeListener listener)109 public void setOnDateChangeListener(CalendarView.OnDateChangeListener listener) { 110 mOnDateChangeListener = listener; 111 } 112 113 @Override getBoundsForDate(long date, Rect outBounds)114 public boolean getBoundsForDate(long date, Rect outBounds) { 115 boolean result = mDayPickerView.getBoundsForDate(date, outBounds); 116 if (result) { 117 // Found the date in the current picker. Now need to offset vertically to return correct 118 // bounds in the coordinate system of the entire layout 119 final int[] dayPickerPositionOnScreen = new int[2]; 120 final int[] delegatorPositionOnScreen = new int[2]; 121 mDayPickerView.getLocationOnScreen(dayPickerPositionOnScreen); 122 mDelegator.getLocationOnScreen(delegatorPositionOnScreen); 123 final int extraVerticalOffset = 124 dayPickerPositionOnScreen[1] - delegatorPositionOnScreen[1]; 125 outBounds.top += extraVerticalOffset; 126 outBounds.bottom += extraVerticalOffset; 127 return true; 128 } 129 return false; 130 } 131 132 private final OnDaySelectedListener mOnDaySelectedListener = new OnDaySelectedListener() { 133 @Override 134 public void onDaySelected(DayPickerView view, Calendar day) { 135 if (mOnDateChangeListener != null) { 136 final int year = day.get(Calendar.YEAR); 137 final int month = day.get(Calendar.MONTH); 138 final int dayOfMonth = day.get(Calendar.DAY_OF_MONTH); 139 mOnDateChangeListener.onSelectedDayChange(mDelegator, year, month, dayOfMonth); 140 } 141 } 142 }; 143 } 144