| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.IdRes; |
| 20 |
import android.annotation.LayoutRes; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.res.ColorStateList; |
| 25 |
import android.content.res.TypedArray; |
| 26 |
import android.graphics.Rect; |
| 27 |
import android.icu.util.Calendar; |
| 28 |
import android.util.SparseArray; |
| 29 |
import android.view.LayoutInflater; |
| 30 |
import android.view.View; |
| 31 |
import android.view.ViewGroup; |
| 32 |
import android.widget.SimpleMonthView.OnDayClickListener; |
| 33 |
|
| 34 |
import com.android.internal.widget.PagerAdapter; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class DayPickerPagerAdapter extends PagerAdapter { |
| 40 |
private static final int MONTHS_IN_YEAR = 12; |
| 41 |
|
| 42 |
private final Calendar mMinDate = Calendar.getInstance(); |
| 43 |
private final Calendar mMaxDate = Calendar.getInstance(); |
| 44 |
|
| 45 |
private final SparseArray<ViewHolder> mItems = new SparseArray<>(); |
| 46 |
|
| 47 |
private final LayoutInflater mInflater; |
| 48 |
private final int mLayoutResId; |
| 49 |
private final int mCalendarViewId; |
| 50 |
|
| 51 |
private Calendar mSelectedDay = null; |
| 52 |
|
| 53 |
private int mMonthTextAppearance; |
| 54 |
private int mDayOfWeekTextAppearance; |
| 55 |
private int mDayTextAppearance; |
| 56 |
|
| 57 |
private ColorStateList mCalendarTextColor; |
| 58 |
private ColorStateList mDaySelectorColor; |
| 59 |
private ColorStateList mDayHighlightColor; |
| 60 |
|
| 61 |
private OnDaySelectedListener mOnDaySelectedListener; |
| 62 |
|
| 63 |
private int mCount; |
| 64 |
private int mFirstDayOfWeek; |
| 65 |
|
| 66 |
public DayPickerPagerAdapter(@NonNull Context context, @LayoutRes int layoutResId, |
| 67 |
@IdRes int calendarViewId) { |
| 68 |
mInflater = LayoutInflater.from(context); |
| 69 |
mLayoutResId = layoutResId; |
| 70 |
mCalendarViewId = calendarViewId; |
| 71 |
|
| 72 |
final TypedArray ta = context.obtainStyledAttributes(new int[] { |
| 73 |
com.android.internal.R.attr.colorControlHighlight}); |
| 74 |
mDayHighlightColor = ta.getColorStateList(0); |
| 75 |
ta.recycle(); |
| 76 |
} |
| 77 |
|
| 78 |
public void setRange(@NonNull Calendar min, @NonNull Calendar max) { |
| 79 |
mMinDate.setTimeInMillis(min.getTimeInMillis()); |
| 80 |
mMaxDate.setTimeInMillis(max.getTimeInMillis()); |
| 81 |
|
| 82 |
final int diffYear = mMaxDate.get(Calendar.YEAR) - mMinDate.get(Calendar.YEAR); |
| 83 |
final int diffMonth = mMaxDate.get(Calendar.MONTH) - mMinDate.get(Calendar.MONTH); |
| 84 |
mCount = diffMonth + MONTHS_IN_YEAR * diffYear + 1; |
| 85 |
|
| 86 |
|
| 87 |
notifyDataSetChanged(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public void setFirstDayOfWeek(int weekStart) { |
| 97 |
mFirstDayOfWeek = weekStart; |
| 98 |
|
| 99 |
|
| 100 |
final int count = mItems.size(); |
| 101 |
for (int i = 0; i < count; i++) { |
| 102 |
final SimpleMonthView monthView = mItems.valueAt(i).calendar; |
| 103 |
monthView.setFirstDayOfWeek(weekStart); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public int getFirstDayOfWeek() { |
| 108 |
return mFirstDayOfWeek; |
| 109 |
} |
| 110 |
|
| 111 |
public boolean getBoundsForDate(Calendar day, Rect outBounds) { |
| 112 |
final int position = getPositionForDay(day); |
| 113 |
final ViewHolder monthView = mItems.get(position, null); |
| 114 |
if (monthView == null) { |
| 115 |
return false; |
| 116 |
} else { |
| 117 |
final int dayOfMonth = day.get(Calendar.DAY_OF_MONTH); |
| 118 |
return monthView.calendar.getBoundsForDay(dayOfMonth, outBounds); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public void setSelectedDay(@Nullable Calendar day) { |
| 128 |
final int oldPosition = getPositionForDay(mSelectedDay); |
| 129 |
final int newPosition = getPositionForDay(day); |
| 130 |
|
| 131 |
|
| 132 |
if (oldPosition != newPosition && oldPosition >= 0) { |
| 133 |
final ViewHolder oldMonthView = mItems.get(oldPosition, null); |
| 134 |
if (oldMonthView != null) { |
| 135 |
oldMonthView.calendar.setSelectedDay(-1); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
if (newPosition >= 0) { |
| 141 |
final ViewHolder newMonthView = mItems.get(newPosition, null); |
| 142 |
if (newMonthView != null) { |
| 143 |
final int dayOfMonth = day.get(Calendar.DAY_OF_MONTH); |
| 144 |
newMonthView.calendar.setSelectedDay(dayOfMonth); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
mSelectedDay = day; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public void setOnDaySelectedListener(OnDaySelectedListener listener) { |
| 157 |
mOnDaySelectedListener = listener; |
| 158 |
} |
| 159 |
|
| 160 |
void setCalendarTextColor(ColorStateList calendarTextColor) { |
| 161 |
mCalendarTextColor = calendarTextColor; |
| 162 |
notifyDataSetChanged(); |
| 163 |
} |
| 164 |
|
| 165 |
void setDaySelectorColor(ColorStateList selectorColor) { |
| 166 |
mDaySelectorColor = selectorColor; |
| 167 |
notifyDataSetChanged(); |
| 168 |
} |
| 169 |
|
| 170 |
void setMonthTextAppearance(int resId) { |
| 171 |
mMonthTextAppearance = resId; |
| 172 |
notifyDataSetChanged(); |
| 173 |
} |
| 174 |
|
| 175 |
void setDayOfWeekTextAppearance(int resId) { |
| 176 |
mDayOfWeekTextAppearance = resId; |
| 177 |
notifyDataSetChanged(); |
| 178 |
} |
| 179 |
|
| 180 |
int getDayOfWeekTextAppearance() { |
| 181 |
return mDayOfWeekTextAppearance; |
| 182 |
} |
| 183 |
|
| 184 |
void setDayTextAppearance(int resId) { |
| 185 |
mDayTextAppearance = resId; |
| 186 |
notifyDataSetChanged(); |
| 187 |
} |
| 188 |
|
| 189 |
int getDayTextAppearance() { |
| 190 |
return mDayTextAppearance; |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public int getCount() { |
| 195 |
return mCount; |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
public boolean isViewFromObject(View view, Object object) { |
| 200 |
final ViewHolder holder = (ViewHolder) object; |
| 201 |
return view == holder.container; |
| 202 |
} |
| 203 |
|
| 204 |
private int getMonthForPosition(int position) { |
| 205 |
return (position + mMinDate.get(Calendar.MONTH)) % MONTHS_IN_YEAR; |
| 206 |
} |
| 207 |
|
| 208 |
private int getYearForPosition(int position) { |
| 209 |
final int yearOffset = (position + mMinDate.get(Calendar.MONTH)) / MONTHS_IN_YEAR; |
| 210 |
return yearOffset + mMinDate.get(Calendar.YEAR); |
| 211 |
} |
| 212 |
|
| 213 |
private int getPositionForDay(@Nullable Calendar day) { |
| 214 |
if (day == null) { |
| 215 |
return -1; |
| 216 |
} |
| 217 |
|
| 218 |
final int yearOffset = day.get(Calendar.YEAR) - mMinDate.get(Calendar.YEAR); |
| 219 |
final int monthOffset = day.get(Calendar.MONTH) - mMinDate.get(Calendar.MONTH); |
| 220 |
final int position = yearOffset * MONTHS_IN_YEAR + monthOffset; |
| 221 |
return position; |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public Object instantiateItem(ViewGroup container, int position) { |
| 226 |
final View itemView = mInflater.inflate(mLayoutResId, container, false); |
| 227 |
|
| 228 |
final SimpleMonthView v = itemView.findViewById(mCalendarViewId); |
| 229 |
v.setOnDayClickListener(mOnDayClickListener); |
| 230 |
v.setMonthTextAppearance(mMonthTextAppearance); |
| 231 |
v.setDayOfWeekTextAppearance(mDayOfWeekTextAppearance); |
| 232 |
v.setDayTextAppearance(mDayTextAppearance); |
| 233 |
|
| 234 |
if (mDaySelectorColor != null) { |
| 235 |
v.setDaySelectorColor(mDaySelectorColor); |
| 236 |
} |
| 237 |
|
| 238 |
if (mDayHighlightColor != null) { |
| 239 |
v.setDayHighlightColor(mDayHighlightColor); |
| 240 |
} |
| 241 |
|
| 242 |
if (mCalendarTextColor != null) { |
| 243 |
v.setMonthTextColor(mCalendarTextColor); |
| 244 |
v.setDayOfWeekTextColor(mCalendarTextColor); |
| 245 |
v.setDayTextColor(mCalendarTextColor); |
| 246 |
} |
| 247 |
|
| 248 |
final int month = getMonthForPosition(position); |
| 249 |
final int year = getYearForPosition(position); |
| 250 |
|
| 251 |
final int selectedDay; |
| 252 |
if (mSelectedDay != null && mSelectedDay.get(Calendar.MONTH) == month && mSelectedDay.get( |
| 253 |
Calendar.YEAR) == year) { |
| 254 |
selectedDay = mSelectedDay.get(Calendar.DAY_OF_MONTH); |
| 255 |
} else { |
| 256 |
selectedDay = -1; |
| 257 |
} |
| 258 |
|
| 259 |
final int enabledDayRangeStart; |
| 260 |
if (mMinDate.get(Calendar.MONTH) == month && mMinDate.get(Calendar.YEAR) == year) { |
| 261 |
enabledDayRangeStart = mMinDate.get(Calendar.DAY_OF_MONTH); |
| 262 |
} else { |
| 263 |
enabledDayRangeStart = 1; |
| 264 |
} |
| 265 |
|
| 266 |
final int enabledDayRangeEnd; |
| 267 |
if (mMaxDate.get(Calendar.MONTH) == month && mMaxDate.get(Calendar.YEAR) == year) { |
| 268 |
enabledDayRangeEnd = mMaxDate.get(Calendar.DAY_OF_MONTH); |
| 269 |
} else { |
| 270 |
enabledDayRangeEnd = 31; |
| 271 |
} |
| 272 |
|
| 273 |
v.setMonthParams(selectedDay, month, year, mFirstDayOfWeek, |
| 274 |
enabledDayRangeStart, enabledDayRangeEnd); |
| 275 |
|
| 276 |
final ViewHolder holder = new ViewHolder(position, itemView, v); |
| 277 |
mItems.put(position, holder); |
| 278 |
|
| 279 |
container.addView(itemView); |
| 280 |
|
| 281 |
return holder; |
| 282 |
} |
| 283 |
|
| 284 |
@Override |
| 285 |
public void destroyItem(ViewGroup container, int position, Object object) { |
| 286 |
final ViewHolder holder = (ViewHolder) object; |
| 287 |
container.removeView(holder.container); |
| 288 |
|
| 289 |
mItems.remove(position); |
| 290 |
} |
| 291 |
|
| 292 |
@Override |
| 293 |
public int getItemPosition(Object object) { |
| 294 |
final ViewHolder holder = (ViewHolder) object; |
| 295 |
return holder.position; |
| 296 |
} |
| 297 |
|
| 298 |
@Override |
| 299 |
public CharSequence getPageTitle(int position) { |
| 300 |
final SimpleMonthView v = mItems.get(position).calendar; |
| 301 |
if (v != null) { |
| 302 |
return v.getMonthYearLabel(); |
| 303 |
} |
| 304 |
return null; |
| 305 |
} |
| 306 |
|
| 307 |
SimpleMonthView getView(Object object) { |
| 308 |
if (object == null) { |
| 309 |
return null; |
| 310 |
} |
| 311 |
final ViewHolder holder = (ViewHolder) object; |
| 312 |
return holder.calendar; |
| 313 |
} |
| 314 |
|
| 315 |
private final OnDayClickListener mOnDayClickListener = new OnDayClickListener() { |
| 316 |
@Override |
| 317 |
public void onDayClick(SimpleMonthView view, Calendar day) { |
| 318 |
if (day != null) { |
| 319 |
setSelectedDay(day); |
| 320 |
|
| 321 |
if (mOnDaySelectedListener != null) { |
| 322 |
mOnDaySelectedListener.onDaySelected(DayPickerPagerAdapter.this, day); |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
}; |
| 327 |
|
| 328 |
private static class ViewHolder { |
| 329 |
public final int position; |
| 330 |
public final View container; |
| 331 |
public final SimpleMonthView calendar; |
| 332 |
|
| 333 |
public ViewHolder(int position, View container, SimpleMonthView calendar) { |
| 334 |
this.position = position; |
| 335 |
this.container = container; |
| 336 |
this.calendar = calendar; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
public interface OnDaySelectedListener { |
| 341 |
public void onDaySelected(DayPickerPagerAdapter view, Calendar day); |
| 342 |
} |
| 343 |
} |