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 com.android.internal.R; 20 21 import android.content.Context; 22 import android.content.res.Configuration; 23 import android.content.res.TypedArray; 24 import android.graphics.drawable.Drawable; 25 import android.text.TextUtils; 26 import android.util.AttributeSet; 27 import android.util.MathUtils; 28 29 import java.util.Calendar; 30 import java.util.Locale; 31 32 import libcore.icu.LocaleData; 33 34 class CalendarViewMaterialDelegate extends CalendarView.AbstractCalendarViewDelegate { 35 private final DayPickerView mDayPickerView; 36 37 private CalendarView.OnDateChangeListener mOnDateChangeListener; 38 CalendarViewMaterialDelegate(CalendarView delegator, Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)39 public CalendarViewMaterialDelegate(CalendarView delegator, Context context, AttributeSet attrs, 40 int defStyleAttr, int defStyleRes) { 41 super(delegator, context); 42 43 final TypedArray a = context.obtainStyledAttributes(attrs, 44 R.styleable.CalendarView, defStyleAttr, defStyleRes); 45 final int firstDayOfWeek = a.getInt(R.styleable.CalendarView_firstDayOfWeek, 46 LocaleData.get(Locale.getDefault()).firstDayOfWeek); 47 48 final long minDate = parseDateToMillis(a.getString( 49 R.styleable.CalendarView_minDate), DEFAULT_MIN_DATE); 50 final long maxDate = parseDateToMillis(a.getString( 51 R.styleable.CalendarView_maxDate), DEFAULT_MAX_DATE); 52 if (maxDate < minDate) { 53 throw new IllegalArgumentException("max date cannot be before min date"); 54 } 55 56 final long setDate = MathUtils.constrain(System.currentTimeMillis(), minDate, maxDate); 57 final int dateTextAppearanceResId = a.getResourceId( 58 R.styleable.CalendarView_dateTextAppearance, 59 R.style.TextAppearance_DeviceDefault_Small); 60 61 a.recycle(); 62 63 mDayPickerView = new DayPickerView(context); 64 mDayPickerView.setFirstDayOfWeek(firstDayOfWeek); 65 mDayPickerView.setCalendarTextAppearance(dateTextAppearanceResId); 66 mDayPickerView.setMinDate(minDate); 67 mDayPickerView.setMaxDate(maxDate); 68 mDayPickerView.setDate(setDate, false, true); 69 mDayPickerView.setOnDaySelectedListener(mOnDaySelectedListener); 70 71 delegator.addView(mDayPickerView); 72 } 73 parseDateToMillis(String dateStr, String defaultDateStr)74 private long parseDateToMillis(String dateStr, String defaultDateStr) { 75 final Calendar tempCalendar = Calendar.getInstance(); 76 if (TextUtils.isEmpty(dateStr) || !parseDate(dateStr, tempCalendar)) { 77 parseDate(defaultDateStr, tempCalendar); 78 } 79 return tempCalendar.getTimeInMillis(); 80 } 81 82 @Override setShownWeekCount(int count)83 public void setShownWeekCount(int count) { 84 // Deprecated. 85 } 86 87 @Override getShownWeekCount()88 public int getShownWeekCount() { 89 // Deprecated. 90 return 0; 91 } 92 93 @Override setSelectedWeekBackgroundColor(int color)94 public void setSelectedWeekBackgroundColor(int color) { 95 // TODO: Should use a ColorStateList. Deprecate? 96 } 97 98 @Override getSelectedWeekBackgroundColor()99 public int getSelectedWeekBackgroundColor() { 100 return 0; 101 } 102 103 @Override setFocusedMonthDateColor(int color)104 public void setFocusedMonthDateColor(int color) { 105 // TODO: Should use a ColorStateList. Deprecate? 106 } 107 108 @Override getFocusedMonthDateColor()109 public int getFocusedMonthDateColor() { 110 return 0; 111 } 112 113 @Override setUnfocusedMonthDateColor(int color)114 public void setUnfocusedMonthDateColor(int color) { 115 // TODO: Should use a ColorStateList. Deprecate? 116 } 117 118 @Override getUnfocusedMonthDateColor()119 public int getUnfocusedMonthDateColor() { 120 return 0; 121 } 122 123 @Override setWeekDayTextAppearance(int resourceId)124 public void setWeekDayTextAppearance(int resourceId) { 125 126 } 127 128 @Override getWeekDayTextAppearance()129 public int getWeekDayTextAppearance() { 130 return 0; 131 } 132 133 @Override setDateTextAppearance(int resourceId)134 public void setDateTextAppearance(int resourceId) { 135 136 } 137 138 @Override getDateTextAppearance()139 public int getDateTextAppearance() { 140 return 0; 141 } 142 143 @Override setWeekNumberColor(int color)144 public void setWeekNumberColor(int color) { 145 // Deprecated. 146 } 147 148 @Override getWeekNumberColor()149 public int getWeekNumberColor() { 150 // Deprecated. 151 return 0; 152 } 153 154 @Override setWeekSeparatorLineColor(int color)155 public void setWeekSeparatorLineColor(int color) { 156 // Deprecated. 157 } 158 159 @Override getWeekSeparatorLineColor()160 public int getWeekSeparatorLineColor() { 161 // Deprecated. 162 return 0; 163 } 164 165 @Override setSelectedDateVerticalBar(int resourceId)166 public void setSelectedDateVerticalBar(int resourceId) { 167 // Deprecated. 168 } 169 170 @Override setSelectedDateVerticalBar(Drawable drawable)171 public void setSelectedDateVerticalBar(Drawable drawable) { 172 // Deprecated. 173 } 174 175 @Override getSelectedDateVerticalBar()176 public Drawable getSelectedDateVerticalBar() { 177 // Deprecated. 178 return null; 179 } 180 181 @Override setMinDate(long minDate)182 public void setMinDate(long minDate) { 183 mDayPickerView.setMinDate(minDate); 184 } 185 186 @Override getMinDate()187 public long getMinDate() { 188 return mDayPickerView.getMinDate(); 189 } 190 191 @Override setMaxDate(long maxDate)192 public void setMaxDate(long maxDate) { 193 mDayPickerView.setMaxDate(maxDate); 194 } 195 196 @Override getMaxDate()197 public long getMaxDate() { 198 return mDayPickerView.getMaxDate(); 199 } 200 201 @Override setShowWeekNumber(boolean showWeekNumber)202 public void setShowWeekNumber(boolean showWeekNumber) { 203 // Deprecated. 204 } 205 206 @Override getShowWeekNumber()207 public boolean getShowWeekNumber() { 208 // Deprecated. 209 return false; 210 } 211 212 @Override setFirstDayOfWeek(int firstDayOfWeek)213 public void setFirstDayOfWeek(int firstDayOfWeek) { 214 mDayPickerView.setFirstDayOfWeek(firstDayOfWeek); 215 } 216 217 @Override getFirstDayOfWeek()218 public int getFirstDayOfWeek() { 219 return mDayPickerView.getFirstDayOfWeek(); 220 } 221 222 @Override setDate(long date)223 public void setDate(long date) { 224 mDayPickerView.setDate(date, true, false); 225 } 226 227 @Override setDate(long date, boolean animate, boolean center)228 public void setDate(long date, boolean animate, boolean center) { 229 mDayPickerView.setDate(date, animate, center); 230 } 231 232 @Override getDate()233 public long getDate() { 234 return mDayPickerView.getDate(); 235 } 236 237 @Override setOnDateChangeListener(CalendarView.OnDateChangeListener listener)238 public void setOnDateChangeListener(CalendarView.OnDateChangeListener listener) { 239 mOnDateChangeListener = listener; 240 } 241 242 @Override onConfigurationChanged(Configuration newConfig)243 public void onConfigurationChanged(Configuration newConfig) { 244 // Nothing to do here, configuration changes are already propagated 245 // by ViewGroup. 246 } 247 248 private final DayPickerView.OnDaySelectedListener mOnDaySelectedListener = 249 new DayPickerView.OnDaySelectedListener() { 250 @Override 251 public void onDaySelected(DayPickerView view, Calendar day) { 252 if (mOnDateChangeListener != null) { 253 final int year = day.get(Calendar.YEAR); 254 final int month = day.get(Calendar.MONTH); 255 final int dayOfMonth = day.get(Calendar.DAY_OF_MONTH); 256 mOnDateChangeListener.onSelectedDayChange(mDelegator, year, month, dayOfMonth); 257 } 258 } 259 }; 260 } 261