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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
|
/*
* Copyright (C) 2010 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.R;
import android.app.Service;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.text.format.Time;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import java.security.InvalidParameterException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import libcore.icu.LocaleData;
/**
* Displays a day picker in the form of a calendar. The calendar
* is represented as a list where each row depicts a week. Each week is
* composed of items that are selectable days.
*/
public class DayPicker extends FrameLayout {
/**
* The number of milliseconds in a day.
*
* @hide
*/
protected static final long MILLIS_IN_DAY = 86400000L;
/**
* The number of day in a week.
*
* @hide
*/
protected static final int DAYS_PER_WEEK = 7;
/**
* The number of milliseconds in a week.
*
* @hide
*/
protected static final long MILLIS_IN_WEEK = DAYS_PER_WEEK * MILLIS_IN_DAY;
/**
* Affects when the month selection will change while scrolling up
*
* @hide
*/
protected static final int SCROLL_HYST_WEEKS = 2;
/**
* How long the GoTo fling animation should last.
*
* @hide
*/
protected static final int GOTO_SCROLL_DURATION = 1000;
/**
* The duration of the adjustment upon a user scroll in milliseconds.
*
* @hide
*/
protected static final int ADJUSTMENT_SCROLL_DURATION = 500;
/**
* How long to wait after receiving an onScrollStateChanged notification
* before acting on it.
*
* @hide
*/
protected static final int SCROLL_CHANGE_DELAY = 40;
/**
* The scale used to compensate for different screen density.
*
* @hide
*/
protected static float sScale;
/**
* The top offset of the weeks list.
*
* @hide
*/
protected static int mListTopOffset = 2;
/**
* The visible height of a week view.
*
* @hide
*/
protected int mWeekMinVisibleHeight = 12;
/**
* The visible height of a week view.
*
* @hide
*/
protected int mBottomBuffer = 20;
/**
* The number of shown weeks.
*
* @hide
*/
protected int mShownWeekCount = 6;
/**
* Flag whether to show the week number.
*
* @hide
*/
protected boolean mShowWeekNumber = true;
/**
* The number of day per week to be shown
*
* @hide
*/
protected int mDaysPerWeek = 7;
/**
* The friction of the week list while flinging.
*
* @hide
*/
protected float mFriction = .05f;
/**
* Scale for adjusting velocity of the week list while flinging.
*
* @hide
*/
protected float mVelocityScale = 0.333f;
/**
* The adapter for the weeks list.
*
* @hide
*/
protected WeeksAdapter mAdapter;
/**
* The weeks list.
*
* @hide
*/
protected ListView mListView;
/**
* The name of the month to display.
*
* @hide
*/
protected TextView mMonthName;
/**
* The header with week day names.
*
* @hide
*/
protected ViewGroup mDayNamesHeader;
/**
* Cached labels for the week names header.
*
* @hide
*/
protected String[] mDayLabels;
/**
* Temporary instance to avoid multiple instantiations.
*
* @hide
*/
protected Calendar mTempCalendar = Calendar.getInstance();
/**
* The first day of the week based on the current locale.
*
* @hide
*/
protected int mFirstDayOfWeek = LocaleData.get(Locale.getDefault()).firstDayOfWeek;
/**
* The first day of the focused month.
*
* @hide
*/
protected Calendar mFirstDayOfMonth = Calendar.getInstance();
/**
* Which month should be displayed/highlighted [0-11]
*
* @hide
*/
protected int mCurrentMonthDisplayed;
/**
* Used for tracking during a scroll.
*
* @hide
*/
protected long mPreviousScrollPosition;
/**
* Used for tracking which direction the view is scrolling.
*
* @hide
*/
protected boolean mIsScrollingUp = false;
/**
* The previous scroll state of the weeks ListView.
*
* @hide
*/
protected int mPreviousScrollState = OnScrollListener.SCROLL_STATE_IDLE;
/**
* The current scroll state of the weeks ListView.
*
* @hide
*/
protected int mCurrentScrollState = OnScrollListener.SCROLL_STATE_IDLE;
/**
* Listener for changes in the selected day.
*
* @hide
*/
protected OnSelectedDayChangeListener mOnChangeListener;
/**
* Command for adjusting the position after a scroll/fling.
*
* @hide
*/
protected ScrollStateRunnable mScrollStateChangedRunnable = new ScrollStateRunnable();
/**
* The start date of the range supported by this picker.
*
* @hide
*/
protected Calendar mStartRangeDate = Calendar.getInstance();
/**
* The end date of the range supported by this picker.
*
* @hide
*/
protected Calendar mEndRangeDate = Calendar.getInstance();
/**
* String for formatting the month name in the title text view.
*
* @hide
*/
protected String mMonthNameFormatSrting = "MMMM, yyyy";
/**
* The callback used to indicate the user changes the date.
*/
public interface OnSelectedDayChangeListener {
/**
* Called upon change of the selected day.
*
* @param view The view associated with this listener.
* @param year The year that was set.
* @param month The month that was set [0-11].
* @param dayOfMonth The day of the month that was set.
*/
public void onSelectedDayChange(DayPicker view, int year, int month, int dayOfMonth);
}
public DayPicker(Context context) {
this(context, null);
}
public DayPicker(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DayPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, 0);
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
View content = layoutInflater.inflate(R.layout.day_picker, null, false);
addView(content);
mListView = (ListView) findViewById(R.id.list);
mDayNamesHeader = (ViewGroup) content.findViewById(com.android.internal.R.id.day_names);
mMonthName = (TextView) content.findViewById(com.android.internal.R.id.month_name);
// Adjust sizes for screen density
if (sScale == 0) {
sScale = mContext.getResources().getDisplayMetrics().density;
if (sScale != 1) {
mWeekMinVisibleHeight *= sScale;
mBottomBuffer *= sScale;
mListTopOffset *= sScale;
}
}
// set default range
mStartRangeDate.clear();
mStartRangeDate.set(1900, 0, 1);
mEndRangeDate.clear();
mEndRangeDate.set(2100, 0, 1);
setUpHeader();
updateHeader();
setUpListView();
setUpAdapter();
// go to today now
mTempCalendar.setTimeInMillis(System.currentTimeMillis());
goTo(mTempCalendar, false, true, true);
invalidate();
}
/**
* Sets the range supported by this day picker. This is the picker will not
* support dates before <code>startRangeDate</code> and <code>endRangeDate
* </code>.
*
* @param startRangeDate The start date.
* @param endRangeDate The end date.
*/
public void setRange(Calendar startRangeDate, Calendar endRangeDate) {
boolean doSetupAdapter = false;
if (mStartRangeDate.get(Calendar.DAY_OF_YEAR) != startRangeDate.get(Calendar.DAY_OF_YEAR)
|| mStartRangeDate.get(Calendar.YEAR) != startRangeDate.get(Calendar.YEAR)) {
mStartRangeDate = startRangeDate;
doSetupAdapter = true;
}
if (mEndRangeDate.get(Calendar.DAY_OF_YEAR) != endRangeDate.get(Calendar.DAY_OF_YEAR)
|| mEndRangeDate.get(Calendar.YEAR) != endRangeDate.get(Calendar.YEAR)) {
mEndRangeDate = endRangeDate;
doSetupAdapter = true;
}
if (doSetupAdapter) {
setUpAdapter();
}
}
/**
* Sets the listener to be notified upon day selection changes.
*
* @param listener The listener to be called back.
*/
public void setOnDateChangeListener(OnSelectedDayChangeListener listener) {
mOnChangeListener = listener;
}
/**
* Gets the selected day.
*
* @return The selected day.
*/
public Calendar getSelectedDay() {
return mAdapter.mSelectedDay;
}
/**
* Sets the selected day. This is equivalent to a call to
* {@link #goTo(Calendar, boolean, boolean, boolean)} with
* the arguments <code>selectedDay</code>, <code>false</code>,
* <code>true</code>, <code>false</code> respectively.
*
* @param selectedDay The selected day.
*/
public void setSelectedDay(Calendar selectedDay) {
goTo(selectedDay, false, true, false);
}
/**
* Creates a new adapter if necessary and sets up its parameters. Override
* this method to provide a custom adapter.
*
* @hide
*/
protected void setUpAdapter() {
if (mAdapter == null) {
mAdapter = new WeeksAdapter(getContext());
mAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
if (mOnChangeListener != null) {
Calendar selectedDay = mAdapter.getSelectedDay();
mOnChangeListener.onSelectedDayChange(DayPicker.this,
selectedDay.get(Calendar.YEAR),
selectedDay.get(Calendar.MONTH),
selectedDay.get(Calendar.DAY_OF_MONTH));
}
}
});
mListView.setAdapter(mAdapter);
}
// refresh the view with the new parameters
mAdapter.notifyDataSetChanged();
}
/**
* Sets up the strings to be used by the header. Override this method to use
* different strings or modify the view params.
*
* @hide
*/
protected void setUpHeader() {
mDayLabels = new String[mDaysPerWeek];
for (int i = mFirstDayOfWeek, count = mFirstDayOfWeek + mDaysPerWeek; i < count; i++) {
int calendarDay = (i < mDaysPerWeek) ? i : 1; // Calendar.MONDAY is
// 1
mDayLabels[i - mFirstDayOfWeek] = DateUtils.getDayOfWeekString(calendarDay,
DateUtils.LENGTH_SHORTEST);
}
}
/**
* Sets all the required fields for the list view. Override this method to
* set a different list view behavior.
*
* @hide
*/
protected void setUpListView() {
// Configure the listview
mListView.setDivider(null);
mListView.setItemsCanFocus(true);
mListView.setVerticalScrollBarEnabled(false);
mListView.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
DayPicker.this.onScrollStateChanged(view, scrollState);
}
public void onScroll(
AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
DayPicker.this.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
});
// Make the scrolling behavior nicer
mListView.setFriction(mFriction);
mListView.setVelocityScale(mVelocityScale);
}
/**
* Fixes the day names header to provide correct spacing and updates the
* label text. Override this to set up a custom header.
*
* @hide
*/
protected void updateHeader() {
TextView label = (TextView) mDayNamesHeader.getChildAt(0);
if (mShowWeekNumber) {
label.setVisibility(View.VISIBLE);
} else {
label.setVisibility(View.GONE);
}
for (int i = 1, count = mDayNamesHeader.getChildCount(); i < count; i++) {
label = (TextView) mDayNamesHeader.getChildAt(i);
if (i < mDaysPerWeek + 1) {
label.setText(mDayLabels[i - 1]);
label.setVisibility(View.VISIBLE);
} else {
label.setVisibility(View.GONE);
}
}
mDayNamesHeader.invalidate();
}
/**
* This moves to the specified time in the view. If the time is not already
* in range it will move the list so that the first of the month containing
* the time is at the top of the view. If the new time is already in view
* the list will not be scrolled unless forceScroll is true. This time may
* optionally be highlighted as selected as well.
*
* @param year The year to move to.
* @param month The month to move to <strong>starting from zero<strong>.
* @param dayOfMonth The month day to move to.
* @param animate Whether to scroll to the given time or just redraw at the
* new location.
* @param setSelected Whether to set the given time as selected
* @param forceScroll Whether to recenter even if the time is already
* visible.
*/
public void goTo(int year, int month, int dayOfMonth, boolean animate, boolean setSelected,
boolean forceScroll) {
mTempCalendar.clear();
mTempCalendar.set(year, month, dayOfMonth);
goTo(mTempCalendar, animate, setSelected, forceScroll);
}
/**
* This moves to the specified time in the view. If the time is not already
* in range it will move the list so that the first of the month containing
* the time is at the top of the view. If the new time is already in view
* the list will not be scrolled unless forceScroll is true. This time may
* optionally be highlighted as selected as well.
*
* @param date The time to move to.
* @param animate Whether to scroll to the given time or just redraw at the
* new location.
* @param setSelected Whether to set the given time as selected.
* @param forceScroll Whether to recenter even if the time is already
* visible.
*/
public void goTo(Calendar date, boolean animate, boolean setSelected, boolean forceScroll) {
long timeInMillis = date.getTimeInMillis();
if (timeInMillis < mStartRangeDate.getTimeInMillis()
|| timeInMillis > mEndRangeDate.getTimeInMillis()) {
throw new IllegalArgumentException("Time not between " + mStartRangeDate.getTime()
+ " and " + mEndRangeDate.getTime());
}
// Find the first and last entirely visible weeks
int firstFullyVisiblePosition = mListView.getFirstVisiblePosition();
View firstChild = mListView.getChildAt(0);
if (firstChild != null && firstChild.getTop() < 0) {
firstFullyVisiblePosition++;
}
int lastFullyVisiblePosition = firstFullyVisiblePosition + mShownWeekCount - 1;
if (firstChild != null && firstChild.getTop() > mBottomBuffer) {
lastFullyVisiblePosition--;
}
if (setSelected) {
mAdapter.setSelectedDay(date);
}
// Get the week we're going to
int position = getWeeksDelta(date);
// Check if the selected day is now outside of our visible range
// and if so scroll to the month that contains it
if (position < firstFullyVisiblePosition || position > lastFullyVisiblePosition
|| forceScroll) {
mFirstDayOfMonth.setTimeInMillis(date.getTimeInMillis());
mFirstDayOfMonth.setTimeZone(date.getTimeZone());
mFirstDayOfMonth.set(Calendar.DAY_OF_MONTH, 1);
setMonthDisplayed(mFirstDayOfMonth);
position = getWeeksDelta(mFirstDayOfMonth);
mPreviousScrollState = OnScrollListener.SCROLL_STATE_FLING;
if (animate) {
mListView.smoothScrollToPositionFromTop(position, mListTopOffset,
GOTO_SCROLL_DURATION);
} else {
mListView.setSelectionFromTop(position, mListTopOffset);
// Perform any after scroll operations that are needed
onScrollStateChanged(mListView, OnScrollListener.SCROLL_STATE_IDLE);
}
} else if (setSelected) {
// Otherwise just set the selection
setMonthDisplayed(date);
}
}
/**
* Called when a <code>view</code> transitions to a new <code>scrollState
* </code>.
*
* @hide
*/
protected void onScrollStateChanged(AbsListView view, int scrollState) {
mScrollStateChangedRunnable.doScrollStateChange(view, scrollState);
}
/**
* Updates the title and selected month if the <code>view</code> has moved to a new
* month.
*
* @hide
*/
protected void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
WeekView child = (WeekView) view.getChildAt(0);
if (child == null) {
return;
}
// Figure out where we are
int offset = child.getBottom() < mWeekMinVisibleHeight ? 1 : 0;
long currScroll = view.getFirstVisiblePosition() * child.getHeight() - child.getBottom();
// If we have moved since our last call update the direction
if (currScroll < mPreviousScrollPosition) {
mIsScrollingUp = true;
} else if (currScroll > mPreviousScrollPosition) {
mIsScrollingUp = false;
} else {
return;
}
// Use some hysteresis for checking which month to highlight. This
// causes the month to transition when two full weeks of a month are
// visible when scrolling up, and when the first day in a month reaches
// the top of the screen when scrolling down.
if (mIsScrollingUp) {
child = (WeekView) view.getChildAt(SCROLL_HYST_WEEKS + offset);
} else if (offset != 0) {
child = (WeekView) view.getChildAt(offset);
}
// Find out which month we're moving into
int month;
if (mIsScrollingUp) {
month = child.getMonthOfFirstWeekDay();
} else {
month = child.getMonthOfLastWeekDay();
}
// And how it relates to our current highlighted month
int monthDiff;
if (mCurrentMonthDisplayed == 11 && month == 0) {
monthDiff = 1;
} else if (mCurrentMonthDisplayed == 0 && month == 11) {
monthDiff = -1;
} else {
monthDiff = month - mCurrentMonthDisplayed;
}
// Only switch months if we're scrolling away from the currently
// selected month
if ((!mIsScrollingUp && monthDiff > 0) || (mIsScrollingUp && monthDiff < 0)) {
Calendar firstDay = child.getFirstDay();
if (mIsScrollingUp) {
firstDay.add(Calendar.DAY_OF_MONTH, -DAYS_PER_WEEK);
} else {
firstDay.add(Calendar.DAY_OF_MONTH, DAYS_PER_WEEK);
}
setMonthDisplayed(firstDay);
}
mPreviousScrollPosition = currScroll;
mPreviousScrollState = mCurrentScrollState;
}
/**
* Sets the month displayed at the top of this view based on time. Override
* to add custom events when the title is changed.
*
* @param calendar A day in the new focus month.
*
* @hide
*/
protected void setMonthDisplayed(Calendar calendar) {
mMonthName.setText(DateFormat.format(mMonthNameFormatSrting, calendar));
mMonthName.invalidate();
mCurrentMonthDisplayed = calendar.get(Calendar.MONTH);
mAdapter.setFocusMonth(mCurrentMonthDisplayed);
// TODO Send Accessibility Event
}
/**
* @return Returns the number of weeks between the current week day of the
* <code>fromDate</code> and the first day of week of
* <code>toDate</code>.
*
* @hide
*/
protected int getWeeksDelta(Calendar toDate) {
if (toDate.before(mStartRangeDate)) {
throw new IllegalArgumentException("fromDate: " + mStartRangeDate.getTime()
+ " does not precede toDate: " + toDate.getTime());
}
int fromDateDayOfWeek = mStartRangeDate.get(Calendar.DAY_OF_WEEK);
long diff = (fromDateDayOfWeek - toDate.getFirstDayOfWeek()) * MILLIS_IN_DAY;
if (diff < 0) {
diff = diff + MILLIS_IN_WEEK;
}
long refDay = mStartRangeDate.getTimeInMillis() - diff;
return (int) ((toDate.getTimeInMillis() - refDay) / MILLIS_IN_WEEK);
}
/**
* Command responsible for acting upon scroll state changes.
*
* @hide
*/
protected class ScrollStateRunnable implements Runnable {
private AbsListView mView;
private int mNewState;
/**
* Sets up the runnable with a short delay in case the scroll state
* immediately changes again.
*
* @param view The list view that changed state
* @param scrollState The new state it changed to
*/
public void doScrollStateChange(AbsListView view, int scrollState) {
removeCallbacks(this);
mView = view;
mNewState = scrollState;
removeCallbacks(this);
postDelayed(this, SCROLL_CHANGE_DELAY);
}
public void run() {
mCurrentScrollState = mNewState;
// Fix the position after a scroll or a fling ends
if (mNewState == OnScrollListener.SCROLL_STATE_IDLE
&& mPreviousScrollState != OnScrollListener.SCROLL_STATE_IDLE) {
mPreviousScrollState = mNewState;
View child = mView.getChildAt(0);
if (child == null) {
// The view is no longer visible, just return
return;
}
int dist = child.getBottom() - mListTopOffset;
if (dist > mListTopOffset) {
if (mIsScrollingUp) {
mView.smoothScrollBy(dist - child.getHeight(), ADJUSTMENT_SCROLL_DURATION);
} else {
mView.smoothScrollBy(dist, ADJUSTMENT_SCROLL_DURATION);
}
}
} else {
mPreviousScrollState = mNewState;
}
}
}
/**
* <p>
* This is a specialized adapter for creating a list of weeks with
* selectable days. It can be configured to display the week number, start
* the week on a given day, show a reduced number of days, or display an
* arbitrary number of weeks at a time.
* </p>
*
* @hide
*/
public class WeeksAdapter extends BaseAdapter implements OnTouchListener {
/**
* The default maximum year supported by the Date Time Picker.
*/
public static final int DEFAULT_MAX_CALENDAR_YEAR = 2100;
/**
* The default minimum year supported by the Date Time Picker.
*/
public static final int DEFAULT_MIN_CALENDAR_YEAR = 1900;
/**
* The number of weeks to display at a time.
*/
public static final String WEEK_PARAMS_NUM_WEEKS = "num_weeks";
/**
* Which month should be in focus currently.
*/
public static final String WEEK_PARAMS_FOCUS_MONTH = "focus_month";
/**
* Whether the week number should be shown. Non-zero to show them.
*/
public static final String WEEK_PARAMS_SHOW_WEEK = "week_numbers";
/**
* Which day the week should start on. {@link Time#SUNDAY} through
* {@link Time#SATURDAY}.
*/
public static final String WEEK_PARAMS_WEEK_START = "week_start";
/**
* The year of the highlighted day.
*/
public static final String WEEK_PARAMS_YEAR = "selected_year";
/**
* The month of the highlighted day.
*/
public static final String WEEK_PARAMS_MONTH = "selected_month";
/**
* The year of the highlighted day.
*/
public static final String WEEK_PARAMS_DAY_OF_MONTH = "selected_day_of_month";
/**
* The start date of the supported interval.
*/
public static final String WEEK_PARAMS_START_DATE_RANGE_MILLIS = "start_date_gange_millis";
/**
* The end date of the supported interval.
*/
public static final String WEEK_PARAMS_END_DATE_RANGE_MILLIS = "end_date_gange_millis";
/**
* How many days of the week to display [1-7].
*/
public static final String WEEK_PARAMS_DAYS_PER_WEEK = "days_per_week";
protected int WEEK_7_OVERHANG_HEIGHT = 7;
protected int mSelectedWeek;
protected GestureDetector mGestureDetector;
protected int mFocusMonth = 0;
private final Calendar mSelectedDay = Calendar.getInstance();
private int mTotalWeekCount = -1;
public WeeksAdapter(Context context) {
mContext = context;
if (sScale == 0) {
sScale = context.getResources().getDisplayMetrics().density;
if (sScale != 1) {
WEEK_7_OVERHANG_HEIGHT *= sScale;
}
}
init();
}
/**
* Set up the gesture detector and selected time
*/
protected void init() {
mGestureDetector = new GestureDetector(mContext, new CalendarGestureListener());
mSelectedWeek = getWeeksDelta(mSelectedDay);
mTotalWeekCount = getWeeksDelta(mEndRangeDate);
}
/**
* Updates the selected day and related parameters.
*
* @param selectedDay The time to highlight
*/
public void setSelectedDay(Calendar selectedDay) {
if (selectedDay.get(Calendar.DAY_OF_YEAR) == mSelectedDay.get(Calendar.DAY_OF_YEAR)
&& selectedDay.get(Calendar.YEAR) == mSelectedDay.get(Calendar.YEAR)) {
return;
}
mSelectedDay.setTimeInMillis(selectedDay.getTimeInMillis());
mSelectedDay.setTimeZone(selectedDay.getTimeZone());
mSelectedWeek = getWeeksDelta(mSelectedDay);
mFocusMonth = mSelectedDay.get(Calendar.MONTH);
notifyDataSetChanged();
invalidate(); // Test
}
/**
* @return The selected day of month.
*/
public Calendar getSelectedDay() {
return mSelectedDay;
}
/**
* updates any config options that may have changed and refreshes the
* view
*/
public void refresh() {
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTotalWeekCount;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("unchecked")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
WeekView v;
HashMap<String, Object> drawingParams = null;
if (convertView != null) {
v = (WeekView) convertView;
// We store the drawing parameters in the view so it can be
// recycled
drawingParams = (HashMap<String, Object>) v.getTag();
} else {
v = getNewView();
// Set up the new view
android.widget.AbsListView.LayoutParams params =
new android.widget.AbsListView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
v.setLayoutParams(params);
v.setClickable(true);
v.setOnTouchListener(this);
drawingParams = new HashMap<String, Object>();
}
// pass in all the view parameters
putDrawingParementer(drawingParams, WeekView.VIEW_PARAMS_SHOW_WK_NUM,
mShowWeekNumber ? 1 : 0);
putDrawingParementer(drawingParams, WeekView.VIEW_PARAMS_WEEK_START, mFirstDayOfWeek);
putDrawingParementer(drawingParams, WeekView.VIEW_PARAMS_NUM_DAYS, mDaysPerWeek);
putDrawingParementer(drawingParams, WeekView.VIEW_PARAMS_WEEK, position);
putDrawingParementer(drawingParams, WeekView.VIEW_PARAMS_FOCUS_MONTH, mFocusMonth);
putDrawingParementer(drawingParams, WeekView.VIEW_PARAMS_SELECTED_DAY,
(mSelectedWeek == position) ? mSelectedDay.get(Calendar.DAY_OF_WEEK) : -1);
v.setWeekParams(drawingParams);
return v;
}
/**
* Puts the given <code>value</code> for the drawing
* <code>parameter</code> in the <code>drawingParams</code>.
*/
private void putDrawingParementer(HashMap<String, Object> drawingParams, String parameter,
int value) {
int[] valueArray = (int[]) drawingParams.get(parameter);
if (valueArray == null) {
valueArray = new int[1];
drawingParams.put(parameter, valueArray);
}
valueArray[0] = value;
}
/**
* Creates a new WeekView and returns it. Override this to customize the
* view creation.
*
* @return A new WeekView
*/
protected WeekView getNewView() {
return new WeekView(mContext);
}
/**
* Changes which month is in focus and updates the view.
*
* @param month The month to show as in focus [0-11]
*/
public void setFocusMonth(int month) {
if (mFocusMonth == month) {
return;
}
mFocusMonth = month;
notifyDataSetChanged();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
WeekView weekView = (WeekView) v;
weekView.getDayFromLocation(event.getX(), mTempCalendar);
if (mTempCalendar.get(Calendar.YEAR) != 0) {
onDayTapped(mTempCalendar);
}
return true;
}
return false;
}
/**
* Maintains the same hour/min/sec but moves the day to the tapped day.
*
* @param day The day that was tapped
*/
protected void onDayTapped(Calendar day) {
setSelectedDay(day);
}
/**
* This is here so we can identify single tap events and set the
* selected day correctly
*/
protected class CalendarGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
}
}
/**
* <p>
* This is a dynamic view for drawing a single week. It can be configured to
* display the week number, start the week on a given day, or show a reduced
* number of days. It is intended for use as a single view within a
* ListView. See {@link WeeksAdapter} for usage.
* </p>
*
* @hide
*/
public class WeekView extends View {
/*
* These params can be passed into the view to control how it appears.
* {@link #VIEW_PARAMS_WEEK} is the only required field, though the
* default values are unlikely to fit most layouts correctly.
*/
/**
* This sets the height of this week in pixels
*/
public static final String VIEW_PARAMS_HEIGHT = "height";
/**
* This specifies the position (or weeks since the epoch) of this week.
*/
public static final String VIEW_PARAMS_WEEK = "week";
/**
* This sets one of the days in this view as selected
* {@link Time#SUNDAY} through {@link Time#SATURDAY}.
*/
public static final String VIEW_PARAMS_SELECTED_DAY = "selected_day";
/**
* Which day the week should start on. {@link Time#SUNDAY} through
* {@link Time#SATURDAY}.
*/
public static final String VIEW_PARAMS_WEEK_START = "week_start";
/**
* How many days to display at a time. Days will be displayed starting
* with {@link #mFirstDay}.
*/
public static final String VIEW_PARAMS_NUM_DAYS = "num_days";
/**
* Which month is currently in focus, as defined by {@link Time#month}
* [0-11].
*/
public static final String VIEW_PARAMS_FOCUS_MONTH = "focus_month";
/**
* If this month should display week numbers. false if 0, true
* otherwise.
*/
public static final String VIEW_PARAMS_SHOW_WK_NUM = "show_wk_num";
protected int mDefaultHeight = 32;
protected int mMinHeight = 10;
protected static final int DEFAULT_SELECTED_DAY = -1;
protected static final int DEFAULT_WEEK_START = Calendar.SUNDAY;
protected static final int DEFAULT_NUM_DAYS = 7;
protected static final int DEFAULT_SHOW_WK_NUM = 0;
protected static final int DEFAULT_FOCUS_MONTH = -1;
protected static final int DAY_SEPARATOR_WIDTH = 1;
protected int mNumberTextSize = 14;
// affects the padding on the sides of this view
protected int mPadding = 0;
protected final Rect mTempRect = new Rect();
protected final Paint mDrawPaint = new Paint();
protected Paint mMonthNumDrawPaint = new Paint();
protected Drawable mSelectedDayLine;
protected final int mSelectionBackgroundColor;
protected final int mFocusedMonthDateColor;
protected final int mOtherMonthDateColor;
protected final int mGridLinesColor;
protected final int mWeekNumberColor;
// Cache the number strings so we don't have to recompute them each time
protected String[] mDayNumbers;
// Quick lookup for checking which days are in the focus month
protected boolean[] mFocusDay;
// The first day displayed by this item
protected Calendar mFirstDay;
// The month of the first day in this week
protected int mMonthOfFirstWeekDay = -1;
// The month of the last day in this week
protected int mLastWeekDayMonth = -1;
// The position of this week, equivalent to weeks since the week of Jan
// 1st, 1900
protected int mWeek = -1;
// Quick reference to the width of this view, matches parent
protected int mWidth;
// The height this view should draw at in pixels, set by height param
protected int mHeight = mDefaultHeight;
// Whether the week number should be shown
protected boolean mShowWeekNum = false;
// If this view contains the selected day
protected boolean mHasSelectedDay = false;
// Which day is selected [0-6] or -1 if no day is selected
protected int mSelectedDay = DEFAULT_SELECTED_DAY;
// How many days to display
protected int mNumDays = DEFAULT_NUM_DAYS;
// The number of days + a spot for week number if it is displayed
protected int mNumCells = mNumDays;
// The left edge of the selected day
protected int mSelectedLeft = -1;
// The right edge of the selected day
protected int mSelectedRight = -1;
public WeekView(Context context) {
super(context);
TypedValue outTypedValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.dayPickerWeekViewStyle, outTypedValue, true);
TypedArray attributesArray = context.obtainStyledAttributes(outTypedValue.resourceId,
R.styleable.DayPickerWeekView);
mSelectionBackgroundColor = attributesArray.getColor(
R.styleable.DayPickerWeekView_selectionBackgroundColor, 0);
mFocusedMonthDateColor = attributesArray.getColor(
R.styleable.DayPickerWeekView_focusedMonthDateColor, 0);
mOtherMonthDateColor = attributesArray.getColor(
R.styleable.DayPickerWeekView_otherMonthDateColor, 0);
mGridLinesColor = attributesArray.getColor(
R.styleable.DayPickerWeekView_gridLinesColor, 0);
mWeekNumberColor = attributesArray.getColor(
R.styleable.DayPickerWeekView_weekNumberColor, 0);
mSelectedDayLine = attributesArray
.getDrawable(R.styleable.DayPickerWeekView_selectedDayLine);
attributesArray.recycle();
if (sScale == 0) {
sScale = context.getResources().getDisplayMetrics().density;
if (sScale != 1) {
mDefaultHeight *= sScale;
mMinHeight *= sScale;
mNumberTextSize *= sScale;
}
}
// Sets up any standard paints that will be used
setPaintProperties();
}
/**
* Sets all the parameters for displaying this week. The only required
* parameter is the week number. Other parameters have a default value
* and will only update if a new value is included, except for focus
* month, which will always default to no focus month if no value is
* passed in. See {@link #VIEW_PARAMS_HEIGHT} for more info on
* parameters.
*
* @param params A map of the new parameters, see
* {@link #VIEW_PARAMS_HEIGHT}
*/
public void setWeekParams(HashMap<String, Object> params) {
if (!params.containsKey(VIEW_PARAMS_WEEK)) {
throw new InvalidParameterException(
"You must specify the week number for this view");
}
setTag(params);
// We keep the current value for any params not present
if (params.containsKey(VIEW_PARAMS_HEIGHT)) {
mHeight = ((int[]) params.get(VIEW_PARAMS_HEIGHT))[0];
if (mHeight < mMinHeight) {
mHeight = mMinHeight;
}
}
if (params.containsKey(VIEW_PARAMS_SELECTED_DAY)) {
mSelectedDay = ((int[]) params.get(VIEW_PARAMS_SELECTED_DAY))[0];
}
mHasSelectedDay = mSelectedDay != -1;
if (params.containsKey(VIEW_PARAMS_NUM_DAYS)) {
mNumDays = ((int[]) params.get(VIEW_PARAMS_NUM_DAYS))[0];
}
if (params.containsKey(VIEW_PARAMS_SHOW_WK_NUM)) {
if (((int[]) params.get(VIEW_PARAMS_SHOW_WK_NUM))[0] != 0) {
mNumCells = mNumDays + 1;
mShowWeekNum = true;
} else {
mShowWeekNum = false;
}
} else {
mNumCells = mShowWeekNum ? mNumDays + 1 : mNumDays;
}
mWeek = ((int[]) params.get(VIEW_PARAMS_WEEK))[0];
mTempCalendar.clear();
mTempCalendar.set(1900, 0, 1);
mTempCalendar.add(Calendar.WEEK_OF_YEAR, mWeek);
if (params.containsKey(VIEW_PARAMS_WEEK_START)) {
mTempCalendar.setFirstDayOfWeek(((int[]) params.get(VIEW_PARAMS_WEEK_START))[0]);
} else {
mTempCalendar.setFirstDayOfWeek(DEFAULT_WEEK_START);
}
// Allocate space for caching the day numbers and focus values
mDayNumbers = new String[mNumCells];
mFocusDay = new boolean[mNumCells];
// If we're showing the week number calculate it based on Monday
int i = 0;
if (mShowWeekNum) {
mDayNumbers[0] = Integer.toString(mTempCalendar.get(Calendar.WEEK_OF_YEAR));
i++;
}
// Now adjust our starting day based on the start day of the week
int diff = mTempCalendar.getFirstDayOfWeek() - mTempCalendar.get(Calendar.DAY_OF_WEEK);
mTempCalendar.add(Calendar.DAY_OF_MONTH, diff);
mFirstDay = (Calendar) mTempCalendar.clone();
mMonthOfFirstWeekDay = mTempCalendar.get(Calendar.MONTH);
int focusMonth = params.containsKey(VIEW_PARAMS_FOCUS_MONTH) ? ((int[]) params
.get(VIEW_PARAMS_FOCUS_MONTH))[0] : DEFAULT_FOCUS_MONTH;
for (; i < mNumCells; i++) {
mFocusDay[i] = (mTempCalendar.get(Calendar.MONTH) == focusMonth);
mDayNumbers[i] = Integer.toString(mTempCalendar.get(Calendar.DAY_OF_MONTH));
mTempCalendar.add(Calendar.DAY_OF_MONTH, 1);
}
// We do one extra add at the end of the loop, if that pushed us to
// new month undo it
if (mTempCalendar.get(Calendar.DAY_OF_MONTH) == 1) {
mTempCalendar.add(Calendar.DAY_OF_MONTH, -1);
}
mLastWeekDayMonth = mTempCalendar.get(Calendar.MONTH);
updateSelectionPositions();
}
/**
* Sets up the text and style properties for painting. Override this if
* you want to use a different paint.
*/
protected void setPaintProperties() {
mDrawPaint.setFakeBoldText(false);
mDrawPaint.setAntiAlias(true);
mDrawPaint.setTextSize(mNumberTextSize);
mDrawPaint.setStyle(Style.FILL);
mMonthNumDrawPaint.setFakeBoldText(true);
mMonthNumDrawPaint.setAntiAlias(true);
mMonthNumDrawPaint.setTextSize(mNumberTextSize);
mMonthNumDrawPaint.setColor(mFocusedMonthDateColor);
mMonthNumDrawPaint.setStyle(Style.FILL);
mMonthNumDrawPaint.setTextAlign(Align.CENTER);
}
/**
* Returns the month of the first day in this week.
*
* @return The month the first day of this view is in.
*/
public int getMonthOfFirstWeekDay() {
return mMonthOfFirstWeekDay;
}
/**
* Returns the month of the last day in this week
*
* @return The month the last day of this view is in
*/
public int getMonthOfLastWeekDay() {
return mLastWeekDayMonth;
}
/**
* Returns the first day in this view.
*
* @return The first day in the view.
*/
public Calendar getFirstDay() {
return mFirstDay;
}
/**
* Returns the number of days this view will display.
*/
public int getNumDays() {
return mNumDays;
}
/**
* Calculates the day that the given x position is in, accounting for
* week number. Returns a Time referencing that day or null if
*
* @param x The x position of the touch eventy
*/
public void getDayFromLocation(float x, Calendar outCalendar) {
int dayStart = mShowWeekNum ? (mWidth - mPadding * 2) / mNumCells + mPadding : mPadding;
if (x < dayStart || x > mWidth - mPadding) {
outCalendar.set(0, 0, 0, 0, 0, 0);
return;
}
// Selection is (x - start) / (pixels/day) == (x -s) * day / pixels
int dayPosition = (int) ((x - dayStart) * mNumDays / (mWidth - dayStart - mPadding));
outCalendar.setTimeZone(mFirstDay.getTimeZone());
outCalendar.setTimeInMillis(mFirstDay.getTimeInMillis());
outCalendar.add(Calendar.DAY_OF_MONTH, dayPosition);
}
@Override
protected void onDraw(Canvas canvas) {
drawBackground(canvas);
drawWeekNums(canvas);
drawDaySeparators(canvas);
}
/**
* This draws the selection highlight if a day is selected in this week.
* Override this method if you wish to have a different background
* drawn.
*
* @param canvas The canvas to draw on
*/
protected void drawBackground(Canvas canvas) {
if (!mHasSelectedDay) {
return;
}
mDrawPaint.setColor(mSelectionBackgroundColor);
mTempRect.top = DAY_SEPARATOR_WIDTH;
mTempRect.bottom = mHeight;
mTempRect.left = mShowWeekNum ? mPadding + (mWidth - mPadding * 2) / mNumCells
: mPadding;
mTempRect.right = mSelectedLeft - 2;
canvas.drawRect(mTempRect, mDrawPaint);
mTempRect.left = mSelectedRight + 3;
mTempRect.right = mWidth - mPadding;
canvas.drawRect(mTempRect, mDrawPaint);
}
/**
* Draws the week and month day numbers for this week. Override this
* method if you need different placement.
*
* @param canvas The canvas to draw on
*/
protected void drawWeekNums(Canvas canvas) {
float textHeight = mDrawPaint.getTextSize();
int y = (int) ((mHeight + textHeight) / 2) - DAY_SEPARATOR_WIDTH;
int nDays = mNumCells;
mDrawPaint.setTextAlign(Align.CENTER);
int i = 0;
int divisor = 2 * nDays;
if (mShowWeekNum) {
mDrawPaint.setColor(mWeekNumberColor);
int x = (mWidth - mPadding * 2) / divisor + mPadding;
canvas.drawText(mDayNumbers[0], x, y, mDrawPaint);
i++;
}
for (; i < nDays; i++) {
mMonthNumDrawPaint.setColor(mFocusDay[i] ? mFocusedMonthDateColor
: mOtherMonthDateColor);
int x = (2 * i + 1) * (mWidth - mPadding * 2) / divisor + mPadding;
canvas.drawText(mDayNumbers[i], x, y, mMonthNumDrawPaint);
}
}
/**
* Draws a horizontal line for separating the weeks. Override this
* method if you want custom separators.
*
* @param canvas The canvas to draw on
*/
protected void drawDaySeparators(Canvas canvas) {
mDrawPaint.setColor(mGridLinesColor);
mDrawPaint.setStrokeWidth(DAY_SEPARATOR_WIDTH);
float x = mShowWeekNum ? mPadding + (mWidth - mPadding * 2) / mNumCells : mPadding;
canvas.drawLine(x, 0, mWidth - mPadding, 0, mDrawPaint);
if (mHasSelectedDay) {
mSelectedDayLine.setBounds(mSelectedLeft - 2, DAY_SEPARATOR_WIDTH,
mSelectedLeft + 4, mHeight + 1);
mSelectedDayLine.draw(canvas);
mSelectedDayLine.setBounds(mSelectedRight - 3, DAY_SEPARATOR_WIDTH,
mSelectedRight + 3, mHeight + 1);
mSelectedDayLine.draw(canvas);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mWidth = w;
updateSelectionPositions();
}
/**
* This calculates the positions for the selected day lines.
*/
protected void updateSelectionPositions() {
if (mHasSelectedDay) {
int selectedPosition = mSelectedDay - mTempCalendar.getFirstDayOfWeek();
if (selectedPosition < 0) {
selectedPosition += 7;
}
if (mShowWeekNum) {
selectedPosition++;
}
mSelectedLeft = selectedPosition * (mWidth - mPadding * 2) / mNumCells + mPadding;
mSelectedRight = (selectedPosition + 1) * (mWidth - mPadding * 2) / mNumCells
+ mPadding;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mHeight);
}
}
}
|