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
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.widget;
import com.android.internal.widget.PagerAdapter;
import android.annotation.IdRes;
import android.annotation.LayoutRes;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleMonthView.OnDayClickListener;
import java.util.Calendar;
/**
* An adapter for a list of {@link android.widget.SimpleMonthView} items.
*/
class DayPickerPagerAdapter extends PagerAdapter {
private static final int MONTHS_IN_YEAR = 12;
private final Calendar mMinDate = Calendar.getInstance();
private final Calendar mMaxDate = Calendar.getInstance();
private final SparseArray<ViewHolder> mItems = new SparseArray<>();
private final LayoutInflater mInflater;
private final int mLayoutResId;
private final int mCalendarViewId;
private Calendar mSelectedDay = null;
private int mMonthTextAppearance;
private int mDayOfWeekTextAppearance;
private int mDayTextAppearance;
private ColorStateList mCalendarTextColor;
private ColorStateList mDaySelectorColor;
private ColorStateList mDayHighlightColor;
private OnDaySelectedListener mOnDaySelectedListener;
private int mCount;
private int mFirstDayOfWeek;
public DayPickerPagerAdapter(@NonNull Context context, @LayoutRes int layoutResId,
@IdRes int calendarViewId) {
mInflater = LayoutInflater.from(context);
mLayoutResId = layoutResId;
mCalendarViewId = calendarViewId;
final TypedArray ta = context.obtainStyledAttributes(new int[] {
com.android.internal.R.attr.colorControlHighlight});
mDayHighlightColor = ta.getColorStateList(0);
ta.recycle();
}
public void setRange(@NonNull Calendar min, @NonNull Calendar max) {
mMinDate.setTimeInMillis(min.getTimeInMillis());
mMaxDate.setTimeInMillis(max.getTimeInMillis());
final int diffYear = mMaxDate.get(Calendar.YEAR) - mMinDate.get(Calendar.YEAR);
final int diffMonth = mMaxDate.get(Calendar.MONTH) - mMinDate.get(Calendar.MONTH);
mCount = diffMonth + MONTHS_IN_YEAR * diffYear + 1;
// Positions are now invalid, clear everything and start over.
notifyDataSetChanged();
}
/**
* Sets the first day of the week.
*
* @param weekStart which day the week should start on, valid values are
* {@link Calendar#SUNDAY} through {@link Calendar#SATURDAY}
*/
public void setFirstDayOfWeek(int weekStart) {
mFirstDayOfWeek = weekStart;
// Update displayed views.
final int count = mItems.size();
for (int i = 0; i < count; i++) {
final SimpleMonthView monthView = mItems.valueAt(i).calendar;
monthView.setFirstDayOfWeek(weekStart);
}
}
public int getFirstDayOfWeek() {
return mFirstDayOfWeek;
}
/**
* Sets the selected day.
*
* @param day the selected day
*/
public void setSelectedDay(@Nullable Calendar day) {
final int oldPosition = getPositionForDay(mSelectedDay);
final int newPosition = getPositionForDay(day);
// Clear the old position if necessary.
if (oldPosition != newPosition && oldPosition >= 0) {
final ViewHolder oldMonthView = mItems.get(oldPosition, null);
if (oldMonthView != null) {
oldMonthView.calendar.setSelectedDay(-1);
}
}
// Set the new position.
if (newPosition >= 0) {
final ViewHolder newMonthView = mItems.get(newPosition, null);
if (newMonthView != null) {
final int dayOfMonth = day.get(Calendar.DAY_OF_MONTH);
newMonthView.calendar.setSelectedDay(dayOfMonth);
}
}
mSelectedDay = day;
}
/**
* Sets the listener to call when the user selects a day.
*
* @param listener The listener to call.
*/
public void setOnDaySelectedListener(OnDaySelectedListener listener) {
mOnDaySelectedListener = listener;
}
void setCalendarTextColor(ColorStateList calendarTextColor) {
mCalendarTextColor = calendarTextColor;
}
void setDaySelectorColor(ColorStateList selectorColor) {
mDaySelectorColor = selectorColor;
}
void setMonthTextAppearance(int resId) {
mMonthTextAppearance = resId;
}
void setDayOfWeekTextAppearance(int resId) {
mDayOfWeekTextAppearance = resId;
}
int getDayOfWeekTextAppearance() {
return mDayOfWeekTextAppearance;
}
void setDayTextAppearance(int resId) {
mDayTextAppearance = resId;
}
int getDayTextAppearance() {
return mDayTextAppearance;
}
@Override
public int getCount() {
return mCount;
}
@Override
public boolean isViewFromObject(View view, Object object) {
final ViewHolder holder = (ViewHolder) object;
return view == holder.container;
}
private int getMonthForPosition(int position) {
return (position + mMinDate.get(Calendar.MONTH)) % MONTHS_IN_YEAR;
}
private int getYearForPosition(int position) {
final int yearOffset = (position + mMinDate.get(Calendar.MONTH)) / MONTHS_IN_YEAR;
return yearOffset + mMinDate.get(Calendar.YEAR);
}
private int getPositionForDay(@Nullable Calendar day) {
if (day == null) {
return -1;
}
final int yearOffset = day.get(Calendar.YEAR) - mMinDate.get(Calendar.YEAR);
final int monthOffset = day.get(Calendar.MONTH) - mMinDate.get(Calendar.MONTH);
final int position = yearOffset * MONTHS_IN_YEAR + monthOffset;
return position;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
final View itemView = mInflater.inflate(mLayoutResId, container, false);
final SimpleMonthView v = (SimpleMonthView) itemView.findViewById(mCalendarViewId);
v.setOnDayClickListener(mOnDayClickListener);
v.setMonthTextAppearance(mMonthTextAppearance);
v.setDayOfWeekTextAppearance(mDayOfWeekTextAppearance);
v.setDayTextAppearance(mDayTextAppearance);
if (mDaySelectorColor != null) {
v.setDaySelectorColor(mDaySelectorColor);
}
if (mDayHighlightColor != null) {
v.setDayHighlightColor(mDayHighlightColor);
}
if (mCalendarTextColor != null) {
v.setMonthTextColor(mCalendarTextColor);
v.setDayOfWeekTextColor(mCalendarTextColor);
v.setDayTextColor(mCalendarTextColor);
}
final int month = getMonthForPosition(position);
final int year = getYearForPosition(position);
final int selectedDay;
if (mSelectedDay != null && mSelectedDay.get(Calendar.MONTH) == month) {
selectedDay = mSelectedDay.get(Calendar.DAY_OF_MONTH);
} else {
selectedDay = -1;
}
final int enabledDayRangeStart;
if (mMinDate.get(Calendar.MONTH) == month && mMinDate.get(Calendar.YEAR) == year) {
enabledDayRangeStart = mMinDate.get(Calendar.DAY_OF_MONTH);
} else {
enabledDayRangeStart = 1;
}
final int enabledDayRangeEnd;
if (mMaxDate.get(Calendar.MONTH) == month && mMaxDate.get(Calendar.YEAR) == year) {
enabledDayRangeEnd = mMaxDate.get(Calendar.DAY_OF_MONTH);
} else {
enabledDayRangeEnd = 31;
}
v.setMonthParams(selectedDay, month, year, mFirstDayOfWeek,
enabledDayRangeStart, enabledDayRangeEnd);
final ViewHolder holder = new ViewHolder(position, itemView, v);
mItems.put(position, holder);
container.addView(itemView);
return holder;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
final ViewHolder holder = (ViewHolder) object;
container.removeView(holder.container);
mItems.remove(position);
}
@Override
public int getItemPosition(Object object) {
final ViewHolder holder = (ViewHolder) object;
return holder.position;
}
@Override
public CharSequence getPageTitle(int position) {
final SimpleMonthView v = mItems.get(position).calendar;
if (v != null) {
return v.getTitle();
}
return null;
}
private final OnDayClickListener mOnDayClickListener = new OnDayClickListener() {
@Override
public void onDayClick(SimpleMonthView view, Calendar day) {
if (day != null) {
setSelectedDay(day);
if (mOnDaySelectedListener != null) {
mOnDaySelectedListener.onDaySelected(DayPickerPagerAdapter.this, day);
}
}
}
};
private static class ViewHolder {
public final int position;
public final View container;
public final SimpleMonthView calendar;
public ViewHolder(int position, View container, SimpleMonthView calendar) {
this.position = position;
this.container = container;
this.calendar = calendar;
}
}
public interface OnDaySelectedListener {
public void onDaySelected(DayPickerPagerAdapter view, Calendar day);
}
}
|