forked from alamkanak/Android-Week-View
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathWeekView.java
executable file
·2873 lines (2480 loc) · 111 KB
/
WeekView.java
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
package com.alamkanak.weekview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.text.*;
import android.text.format.DateFormat;
import android.text.style.StyleSpan;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.*;
import android.widget.OverScroller;
import java.text.SimpleDateFormat;
import java.util.*;
import static com.alamkanak.weekview.WeekViewUtil.*;
/**
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
* Website: http://alamkanak.github.io/
*/
public class WeekView extends View {
private enum Direction {
NONE, LEFT, RIGHT, VERTICAL
}
@Deprecated
public static final int LENGTH_SHORT = 1;
@Deprecated
public static final int LENGTH_LONG = 2;
private final Context mContext;
private Calendar mHomeDate;
private Calendar mMinDate;
private Calendar mMaxDate;
private Paint mTimeTextPaint;
private float mTimeTextWidth;
private float mTimeTextHeight;
private Paint mHeaderTextPaint;
private float mHeaderTextHeight;
private float mHeaderHeight;
private GestureDetectorCompat mGestureDetector;
private OverScroller mScroller;
private PointF mCurrentOrigin = new PointF(0f, 0f);
private Direction mCurrentScrollDirection = Direction.NONE;
private Paint mHeaderBackgroundPaint;
private float mWidthPerDay;
private Paint mDayBackgroundPaint;
private Paint mHourSeparatorPaint;
private float mHeaderMarginBottom;
private Paint mTodayBackgroundPaint;
private Paint mFutureBackgroundPaint;
private Paint mPastBackgroundPaint;
private Paint mFutureWeekendBackgroundPaint;
private Paint mPastWeekendBackgroundPaint;
private Paint mNowLinePaint;
private Paint mTodayHeaderTextPaint;
private Paint mEventBackgroundPaint;
private Paint mNewEventBackgroundPaint;
private float mHeaderColumnWidth;
private List<EventRect> mEventRects;
private List<WeekViewEvent> mEvents;
private TextPaint mEventTextPaint;
private TextPaint mNewEventTextPaint;
private Paint mHeaderColumnBackgroundPaint;
private int mFetchedPeriod = -1; // the middle period the calendar has fetched.
private boolean mRefreshEvents = false;
private Direction mCurrentFlingDirection = Direction.NONE;
private ScaleGestureDetector mScaleDetector;
private boolean mIsZooming;
private Calendar mFirstVisibleDay;
private Calendar mLastVisibleDay;
private int mMinimumFlingVelocity = 0;
private int mScaledTouchSlop = 0;
private EventRect mNewEventRect;
private TextColorPicker textColorPicker;
// Attributes and their default values.
private int mHourHeight = 50;
private int mNewHourHeight = -1;
private int mMinHourHeight = 0; //no minimum specified (will be dynamic, based on screen)
private int mEffectiveMinHourHeight = mMinHourHeight; //compensates for the fact that you can't keep zooming out.
private int mMaxHourHeight = 250;
private int mColumnGap = 10;
private int mFirstDayOfWeek = Calendar.MONDAY;
private int mTextSize = 12;
private int mHeaderColumnPadding = 10;
private int mHeaderColumnTextColor = Color.BLACK;
private int mNumberOfVisibleDays = 3;
private int mHeaderRowPadding = 10;
private int mHeaderRowBackgroundColor = Color.WHITE;
private int mDayBackgroundColor = Color.rgb(245, 245, 245);
private int mPastBackgroundColor = Color.rgb(227, 227, 227);
private int mFutureBackgroundColor = Color.rgb(245, 245, 245);
private int mPastWeekendBackgroundColor = 0;
private int mFutureWeekendBackgroundColor = 0;
private int mNowLineColor = Color.rgb(102, 102, 102);
private int mNowLineThickness = 5;
private int mHourSeparatorColor = Color.rgb(230, 230, 230);
private int mTodayBackgroundColor = Color.rgb(239, 247, 254);
private int mHourSeparatorHeight = 2;
private int mTodayHeaderTextColor = Color.rgb(39, 137, 228);
private int mEventTextSize = 12;
private int mEventTextColor = Color.BLACK;
private int mEventPadding = 8;
private int mHeaderColumnBackgroundColor = Color.WHITE;
private int mDefaultEventColor;
private int mNewEventColor;
private String mNewEventIdentifier = "-100";
private Drawable mNewEventIconDrawable;
private int mNewEventLengthInMinutes = 60;
private int mNewEventTimeResolutionInMinutes = 15;
private boolean mShowFirstDayOfWeekFirst = false;
private boolean mIsFirstDraw = true;
private boolean mAreDimensionsInvalid = true;
@Deprecated
private int mDayNameLength = LENGTH_LONG;
private int mOverlappingEventGap = 0;
private int mEventMarginVertical = 0;
private float mXScrollingSpeed = 1f;
private Calendar mScrollToDay = null;
private double mScrollToHour = -1;
private int mEventCornerRadius = 0;
private boolean mShowDistinctWeekendColor = false;
private boolean mShowNowLine = false;
private boolean mShowDistinctPastFutureColor = false;
private boolean mHorizontalFlingEnabled = true;
private boolean mVerticalFlingEnabled = true;
private int mAllDayEventHeight = 100;
private float mZoomFocusPoint = 0;
private boolean mZoomFocusPointEnabled = true;
private int mScrollDuration = 250;
private int mTimeColumnResolution = 60;
private Typeface mTypeface = Typeface.DEFAULT_BOLD;
private int mMinTime = 0;
private int mMaxTime = 24;
private boolean mAutoLimitTime = false;
private boolean mEnableDropListener = false;
private int mMinOverlappingMinutes = 0;
// Listeners.
private EventClickListener mEventClickListener;
private EventLongPressListener mEventLongPressListener;
private WeekViewLoader mWeekViewLoader;
private EmptyViewClickListener mEmptyViewClickListener;
private EmptyViewLongPressListener mEmptyViewLongPressListener;
private DateTimeInterpreter mDateTimeInterpreter;
private ScrollListener mScrollListener;
private AddEventClickListener mAddEventClickListener;
private DropListener mDropListener;
private ZoomEndListener mZoomEndListener;
private final GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
stopScrolling();
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e){
goToNearestOrigin();
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// Check if view is zoomed.
if (mIsZooming)
return true;
switch (mCurrentScrollDirection) {
case NONE: {
// Allow scrolling only in one direction.
if (Math.abs(distanceX) > Math.abs(distanceY)) {
if (distanceX > 0) {
mCurrentScrollDirection = Direction.LEFT;
} else {
mCurrentScrollDirection = Direction.RIGHT;
}
} else {
mCurrentScrollDirection = Direction.VERTICAL;
}
break;
}
case LEFT: {
// Change direction if there was enough change.
if (Math.abs(distanceX) > Math.abs(distanceY) && (distanceX < -mScaledTouchSlop)) {
mCurrentScrollDirection = Direction.RIGHT;
}
break;
}
case RIGHT: {
// Change direction if there was enough change.
if (Math.abs(distanceX) > Math.abs(distanceY) && (distanceX > mScaledTouchSlop)) {
mCurrentScrollDirection = Direction.LEFT;
}
break;
}
default:
break;
}
// Calculate the new origin after scroll.
switch (mCurrentScrollDirection) {
case LEFT:
case RIGHT:
float minX = getXMinLimit();
float maxX = getXMaxLimit();
if ((mCurrentOrigin.x - (distanceX * mXScrollingSpeed)) > maxX) {
mCurrentOrigin.x = maxX;
} else if ((mCurrentOrigin.x - (distanceX * mXScrollingSpeed)) < minX) {
mCurrentOrigin.x = minX;
} else {
mCurrentOrigin.x -= distanceX * mXScrollingSpeed;
}
ViewCompat.postInvalidateOnAnimation(WeekView.this);
break;
case VERTICAL:
float minY = getYMinLimit();
float maxY = getYMaxLimit();
if ((mCurrentOrigin.y - (distanceY)) > maxY) {
mCurrentOrigin.y = maxY;
} else if ((mCurrentOrigin.y - (distanceY)) < minY) {
mCurrentOrigin.y = minY;
} else {
mCurrentOrigin.y -= distanceY;
}
ViewCompat.postInvalidateOnAnimation(WeekView.this);
break;
default:
break;
}
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (mIsZooming)
return true;
if ((mCurrentFlingDirection == Direction.LEFT && !mHorizontalFlingEnabled) ||
(mCurrentFlingDirection == Direction.RIGHT && !mHorizontalFlingEnabled) ||
(mCurrentFlingDirection == Direction.VERTICAL && !mVerticalFlingEnabled)) {
return true;
}
mScroller.forceFinished(true);
mCurrentFlingDirection = mCurrentScrollDirection;
switch (mCurrentFlingDirection) {
case LEFT:
case RIGHT:
mScroller.fling((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, (int) (velocityX * mXScrollingSpeed), 0, (int) getXMinLimit(), (int) getXMaxLimit(), (int) getYMinLimit(), (int) getYMaxLimit());
break;
case VERTICAL:
mScroller.fling((int) mCurrentOrigin.x, (int) mCurrentOrigin.y, 0, (int) velocityY, (int) getXMinLimit(), (int) getXMaxLimit(), (int) getYMinLimit(), (int) getYMaxLimit());
break;
default:
break;
}
ViewCompat.postInvalidateOnAnimation(WeekView.this);
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
goToNearestOrigin();
// If the tap was on an event then trigger the callback.
if (mEventRects != null && mEventClickListener != null) {
List<EventRect> reversedEventRects = mEventRects;
Collections.reverse(reversedEventRects);
for (EventRect eventRect : reversedEventRects) {
if (!mNewEventIdentifier.equals(eventRect.event.getIdentifier()) && eventRect.rectF != null && e.getX() > eventRect.rectF.left && e.getX() < eventRect.rectF.right && e.getY() > eventRect.rectF.top && e.getY() < eventRect.rectF.bottom) {
mEventClickListener.onEventClick(eventRect.originalEvent, eventRect.rectF);
playSoundEffect(SoundEffectConstants.CLICK);
return super.onSingleTapConfirmed(e);
}
}
}
float xOffset = getXStartPixel();
float x = e.getX() - xOffset;
float y = e.getY() - mCurrentOrigin.y;
// If the tap was on add new Event space, then trigger the callback
if (mAddEventClickListener != null && mNewEventRect != null && mNewEventRect.rectF != null &&
mNewEventRect.rectF.contains(x, y)) {
mAddEventClickListener.onAddEventClicked(mNewEventRect.event.getStartTime(), mNewEventRect.event.getEndTime());
return super.onSingleTapConfirmed(e);
}
// If the tap was on an empty space, then trigger the callback.
if ((mEmptyViewClickListener != null || mAddEventClickListener != null) && e.getX() > mHeaderColumnWidth && e.getY() > (mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom)) {
Calendar selectedTime = getTimeFromPoint(e.getX(), e.getY());
if (selectedTime != null) {
List<WeekViewEvent> tempEvents = new ArrayList<>(mEvents);
if (mNewEventRect != null) {
tempEvents.remove(mNewEventRect.event);
mNewEventRect = null;
}
playSoundEffect(SoundEffectConstants.CLICK);
if (mEmptyViewClickListener != null)
mEmptyViewClickListener.onEmptyViewClicked((Calendar) selectedTime.clone());
if (mAddEventClickListener != null) {
//round selectedTime to resolution
selectedTime.add(Calendar.MINUTE, -(mNewEventLengthInMinutes / 2));
//Fix selected time if before the minimum hour
if (selectedTime.get(Calendar.HOUR_OF_DAY) < mMinTime) {
selectedTime.set(Calendar.HOUR_OF_DAY, mMinTime);
selectedTime.set(Calendar.MINUTE, 0);
}
int unroundedMinutes = selectedTime.get(Calendar.MINUTE);
int mod = unroundedMinutes % mNewEventTimeResolutionInMinutes;
selectedTime.add(Calendar.MINUTE, mod < Math.ceil(mNewEventTimeResolutionInMinutes / 2) ? -mod : (mNewEventTimeResolutionInMinutes - mod));
Calendar endTime = (Calendar) selectedTime.clone();
//Minus one to ensure it is the same day and not midnight (next day)
int maxMinutes = (mMaxTime - selectedTime.get(Calendar.HOUR_OF_DAY)) * 60 - selectedTime.get(Calendar.MINUTE) - 1;
endTime.add(Calendar.MINUTE, Math.min(maxMinutes, mNewEventLengthInMinutes));
//If clicked at end of the day, fix selected startTime
if (maxMinutes < mNewEventLengthInMinutes) {
selectedTime.add(Calendar.MINUTE, maxMinutes - mNewEventLengthInMinutes);
}
WeekViewEvent newEvent = new WeekViewEvent(mNewEventIdentifier, "", null, selectedTime, endTime);
float top = mHourHeight * getPassedMinutesInDay(selectedTime) / 60 + getEventsTop();
float bottom = mHourHeight * getPassedMinutesInDay(endTime) / 60 + getEventsTop();
// Calculate left and right.
float left = mWidthPerDay * WeekViewUtil.daysBetween(getFirstVisibleDay(), selectedTime);
float right = left + mWidthPerDay;
// Add the new event if its bounds are valid
if (left < right &&
left < getWidth() &&
top < getHeight() &&
right > mHeaderColumnWidth &&
bottom > 0
) {
RectF dayRectF = new RectF(left, top, right, bottom - mCurrentOrigin.y);
newEvent.setColor(mNewEventColor);
mNewEventRect = new EventRect(newEvent, newEvent, dayRectF);
tempEvents.add(newEvent);
WeekView.this.clearEvents();
cacheAndSortEvents(tempEvents);
computePositionOfEvents(mEventRects);
invalidate();
}
}
}
}
return super.onSingleTapConfirmed(e);
}
@Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
goToNearestOrigin();
if (mEventLongPressListener != null && mEventRects != null) {
List<EventRect> reversedEventRects = mEventRects;
Collections.reverse(reversedEventRects);
for (EventRect event : reversedEventRects) {
if (event.rectF != null && e.getX() > event.rectF.left && e.getX() < event.rectF.right && e.getY() > event.rectF.top && e.getY() < event.rectF.bottom) {
mEventLongPressListener.onEventLongPress(event.originalEvent, event.rectF);
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
return;
}
}
}
// If the tap was on in an empty space, then trigger the callback.
if (mEmptyViewLongPressListener != null && e.getX() > mHeaderColumnWidth && e.getY() > (mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom)) {
Calendar selectedTime = getTimeFromPoint(e.getX(), e.getY());
if (selectedTime != null) {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
mEmptyViewLongPressListener.onEmptyViewLongPress(selectedTime);
}
}
}
};
public WeekView(Context context) {
this(context, null);
}
public WeekView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// Hold references.
mContext = context;
// Get the attribute values (if any).
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.WeekView, 0, 0);
try {
mFirstDayOfWeek = a.getInteger(R.styleable.WeekView_firstDayOfWeek, mFirstDayOfWeek);
mHourHeight = a.getDimensionPixelSize(R.styleable.WeekView_hourHeight, mHourHeight);
mMinHourHeight = a.getDimensionPixelSize(R.styleable.WeekView_minHourHeight, mMinHourHeight);
mEffectiveMinHourHeight = mMinHourHeight;
mMaxHourHeight = a.getDimensionPixelSize(R.styleable.WeekView_maxHourHeight, mMaxHourHeight);
mTextSize = a.getDimensionPixelSize(R.styleable.WeekView_textSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mTextSize, context.getResources().getDisplayMetrics()));
mHeaderColumnPadding = a.getDimensionPixelSize(R.styleable.WeekView_headerColumnPadding, mHeaderColumnPadding);
mColumnGap = a.getDimensionPixelSize(R.styleable.WeekView_columnGap, mColumnGap);
mHeaderColumnTextColor = a.getColor(R.styleable.WeekView_headerColumnTextColor, mHeaderColumnTextColor);
mNumberOfVisibleDays = a.getInteger(R.styleable.WeekView_noOfVisibleDays, mNumberOfVisibleDays);
mShowFirstDayOfWeekFirst = a.getBoolean(R.styleable.WeekView_showFirstDayOfWeekFirst, mShowFirstDayOfWeekFirst);
mHeaderRowPadding = a.getDimensionPixelSize(R.styleable.WeekView_headerRowPadding, mHeaderRowPadding);
mHeaderRowBackgroundColor = a.getColor(R.styleable.WeekView_headerRowBackgroundColor, mHeaderRowBackgroundColor);
mDayBackgroundColor = a.getColor(R.styleable.WeekView_dayBackgroundColor, mDayBackgroundColor);
mFutureBackgroundColor = a.getColor(R.styleable.WeekView_futureBackgroundColor, mFutureBackgroundColor);
mPastBackgroundColor = a.getColor(R.styleable.WeekView_pastBackgroundColor, mPastBackgroundColor);
mFutureWeekendBackgroundColor = a.getColor(R.styleable.WeekView_futureWeekendBackgroundColor, mFutureBackgroundColor); // If not set, use the same color as in the week
mPastWeekendBackgroundColor = a.getColor(R.styleable.WeekView_pastWeekendBackgroundColor, mPastBackgroundColor);
mNowLineColor = a.getColor(R.styleable.WeekView_nowLineColor, mNowLineColor);
mNowLineThickness = a.getDimensionPixelSize(R.styleable.WeekView_nowLineThickness, mNowLineThickness);
mHourSeparatorColor = a.getColor(R.styleable.WeekView_hourSeparatorColor, mHourSeparatorColor);
mTodayBackgroundColor = a.getColor(R.styleable.WeekView_todayBackgroundColor, mTodayBackgroundColor);
mHourSeparatorHeight = a.getDimensionPixelSize(R.styleable.WeekView_hourSeparatorHeight, mHourSeparatorHeight);
mTodayHeaderTextColor = a.getColor(R.styleable.WeekView_todayHeaderTextColor, mTodayHeaderTextColor);
mEventTextSize = a.getDimensionPixelSize(R.styleable.WeekView_eventTextSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mEventTextSize, context.getResources().getDisplayMetrics()));
mEventTextColor = a.getColor(R.styleable.WeekView_eventTextColor, mEventTextColor);
mNewEventColor = a.getColor(R.styleable.WeekView_newEventColor, mNewEventColor);
mNewEventIconDrawable = a.getDrawable(R.styleable.WeekView_newEventIconResource);
// For backward compatibility : Set "mNewEventIdentifier" if the attribute is "WeekView_newEventId" of type int
setNewEventId(a.getInt(R.styleable.WeekView_newEventId, Integer.parseInt(mNewEventIdentifier)));
mNewEventIdentifier = (a.getString(R.styleable.WeekView_newEventIdentifier) != null) ? a.getString(R.styleable.WeekView_newEventIdentifier) : mNewEventIdentifier;
mNewEventLengthInMinutes = a.getInt(R.styleable.WeekView_newEventLengthInMinutes, mNewEventLengthInMinutes);
mNewEventTimeResolutionInMinutes = a.getInt(R.styleable.WeekView_newEventTimeResolutionInMinutes, mNewEventTimeResolutionInMinutes);
mEventPadding = a.getDimensionPixelSize(R.styleable.WeekView_eventPadding, mEventPadding);
mHeaderColumnBackgroundColor = a.getColor(R.styleable.WeekView_headerColumnBackground, mHeaderColumnBackgroundColor);
mDayNameLength = a.getInteger(R.styleable.WeekView_dayNameLength, mDayNameLength);
mOverlappingEventGap = a.getDimensionPixelSize(R.styleable.WeekView_overlappingEventGap, mOverlappingEventGap);
mEventMarginVertical = a.getDimensionPixelSize(R.styleable.WeekView_eventMarginVertical, mEventMarginVertical);
mXScrollingSpeed = a.getFloat(R.styleable.WeekView_xScrollingSpeed, mXScrollingSpeed);
mEventCornerRadius = a.getDimensionPixelSize(R.styleable.WeekView_eventCornerRadius, mEventCornerRadius);
mShowDistinctPastFutureColor = a.getBoolean(R.styleable.WeekView_showDistinctPastFutureColor, mShowDistinctPastFutureColor);
mShowDistinctWeekendColor = a.getBoolean(R.styleable.WeekView_showDistinctWeekendColor, mShowDistinctWeekendColor);
mShowNowLine = a.getBoolean(R.styleable.WeekView_showNowLine, mShowNowLine);
mHorizontalFlingEnabled = a.getBoolean(R.styleable.WeekView_horizontalFlingEnabled, mHorizontalFlingEnabled);
mVerticalFlingEnabled = a.getBoolean(R.styleable.WeekView_verticalFlingEnabled, mVerticalFlingEnabled);
mAllDayEventHeight = a.getDimensionPixelSize(R.styleable.WeekView_allDayEventHeight, mAllDayEventHeight);
mZoomFocusPoint = a.getFraction(R.styleable.WeekView_zoomFocusPoint, 1, 1, mZoomFocusPoint);
mZoomFocusPointEnabled = a.getBoolean(R.styleable.WeekView_zoomFocusPointEnabled, mZoomFocusPointEnabled);
mScrollDuration = a.getInt(R.styleable.WeekView_scrollDuration, mScrollDuration);
mTimeColumnResolution = a.getInt(R.styleable.WeekView_timeColumnResolution, mTimeColumnResolution);
mAutoLimitTime = a.getBoolean(R.styleable.WeekView_autoLimitTime, mAutoLimitTime);
mMinTime = a.getInt(R.styleable.WeekView_minTime, mMinTime);
mMaxTime = a.getInt(R.styleable.WeekView_maxTime, mMaxTime);
if (a.getBoolean(R.styleable.WeekView_dropListenerEnabled, false))
this.enableDropListener();
mMinOverlappingMinutes = a.getInt(R.styleable.WeekView_minOverlappingMinutes, 0);
} finally {
a.recycle();
}
init();
if (isInEditMode()) {
goToHour(Math.max(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) - 2, 0));
}
}
private void init() {
resetHomeDate();
// Scrolling initialization.
mGestureDetector = new GestureDetectorCompat(mContext, mGestureListener);
mScroller = new OverScroller(mContext, new FastOutLinearInInterpolator());
mMinimumFlingVelocity = ViewConfiguration.get(mContext).getScaledMinimumFlingVelocity();
mScaledTouchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();
// Measure settings for time column.
mTimeTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTimeTextPaint.setTextAlign(Paint.Align.RIGHT);
mTimeTextPaint.setTextSize(mTextSize);
mTimeTextPaint.setColor(mHeaderColumnTextColor);
Rect rect = new Rect();
final String exampleTime = (mTimeColumnResolution % 60 != 0) ? "00:00 PM" : "00 PM";
mTimeTextPaint.getTextBounds(exampleTime, 0, exampleTime.length(), rect);
mTimeTextWidth = mTimeTextPaint.measureText(exampleTime);
mTimeTextHeight = rect.height();
mHeaderMarginBottom = mTimeTextHeight / 2;
initTextTimeWidth();
// Measure settings for header row.
mHeaderTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mHeaderTextPaint.setColor(mHeaderColumnTextColor);
mHeaderTextPaint.setTextAlign(Paint.Align.CENTER);
mHeaderTextPaint.setTextSize(mTextSize);
mHeaderTextPaint.getTextBounds(exampleTime, 0, exampleTime.length(), rect);
mHeaderTextHeight = rect.height();
mHeaderTextPaint.setTypeface(mTypeface);
// Prepare header background paint.
mHeaderBackgroundPaint = new Paint();
mHeaderBackgroundPaint.setColor(mHeaderRowBackgroundColor);
// Prepare day background color paint.
mDayBackgroundPaint = new Paint();
mDayBackgroundPaint.setColor(mDayBackgroundColor);
mFutureBackgroundPaint = new Paint();
mFutureBackgroundPaint.setColor(mFutureBackgroundColor);
mPastBackgroundPaint = new Paint();
mPastBackgroundPaint.setColor(mPastBackgroundColor);
mFutureWeekendBackgroundPaint = new Paint();
mFutureWeekendBackgroundPaint.setColor(mFutureWeekendBackgroundColor);
mPastWeekendBackgroundPaint = new Paint();
mPastWeekendBackgroundPaint.setColor(mPastWeekendBackgroundColor);
// Prepare hour separator color paint.
mHourSeparatorPaint = new Paint();
mHourSeparatorPaint.setStyle(Paint.Style.STROKE);
mHourSeparatorPaint.setStrokeWidth(mHourSeparatorHeight);
mHourSeparatorPaint.setColor(mHourSeparatorColor);
// Prepare the "now" line color paint
mNowLinePaint = new Paint();
mNowLinePaint.setStrokeWidth(mNowLineThickness);
mNowLinePaint.setColor(mNowLineColor);
// Prepare today background color paint.
mTodayBackgroundPaint = new Paint();
mTodayBackgroundPaint.setColor(mTodayBackgroundColor);
// Prepare today header text color paint.
mTodayHeaderTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTodayHeaderTextPaint.setTextAlign(Paint.Align.CENTER);
mTodayHeaderTextPaint.setTextSize(mTextSize);
mTodayHeaderTextPaint.setTypeface(mTypeface);
mTodayHeaderTextPaint.setColor(mTodayHeaderTextColor);
// Prepare event background color.
mEventBackgroundPaint = new Paint();
mEventBackgroundPaint.setColor(Color.rgb(174, 208, 238));
// Prepare empty event background color.
mNewEventBackgroundPaint = new Paint();
mNewEventBackgroundPaint.setColor(Color.rgb(60, 147, 217));
// Prepare header column background color.
mHeaderColumnBackgroundPaint = new Paint();
mHeaderColumnBackgroundPaint.setColor(mHeaderColumnBackgroundColor);
// Prepare event text size and color.
mEventTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
mEventTextPaint.setStyle(Paint.Style.FILL);
mEventTextPaint.setColor(mEventTextColor);
mEventTextPaint.setTextSize(mEventTextSize);
// Set default event color.
mDefaultEventColor = Color.parseColor("#9fc6e7");
// Set default empty event color.
mNewEventColor = Color.parseColor("#3c93d9");
mScaleDetector = new ScaleGestureDetector(mContext, new WeekViewGestureListener());
}
private void resetHomeDate() {
Calendar newHomeDate = today();
if (mMinDate != null && newHomeDate.before(mMinDate)) {
newHomeDate = (Calendar) mMinDate.clone();
}
if (mMaxDate != null && newHomeDate.after(mMaxDate)) {
newHomeDate = (Calendar) mMaxDate.clone();
}
if (mMaxDate != null) {
Calendar date = (Calendar) mMaxDate.clone();
date.add(Calendar.DATE, 1 - getRealNumberOfVisibleDays());
while (date.before(mMinDate)) {
date.add(Calendar.DATE, 1);
}
if (newHomeDate.after(date)) {
newHomeDate = date;
}
}
mHomeDate = newHomeDate;
}
private float getXOriginForDate(Calendar date) {
return -daysBetween(mHomeDate, date) * (mWidthPerDay + mColumnGap);
}
private int getNumberOfPeriods() {
return (int) ((mMaxTime - mMinTime) * (60.0 / mTimeColumnResolution));
}
private float getYMinLimit() {
return -(mHourHeight * (mMaxTime - mMinTime)
+ mHeaderHeight
+ mHeaderRowPadding * 2
+ mHeaderMarginBottom
+ mTimeTextHeight / 2
- getHeight());
}
private float getYMaxLimit() {
return 0;
}
private float getXMinLimit() {
if (mMaxDate == null) {
return Integer.MIN_VALUE;
} else {
Calendar date = (Calendar) mMaxDate.clone();
date.add(Calendar.DATE, 1 - getRealNumberOfVisibleDays());
while (date.before(mMinDate)) {
date.add(Calendar.DATE, 1);
}
return getXOriginForDate(date);
}
}
private float getXMaxLimit() {
if (mMinDate == null) {
return Integer.MAX_VALUE;
} else {
return getXOriginForDate(mMinDate);
}
}
// fix rotation changes
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mAreDimensionsInvalid = true;
}
/**
* Initialize time column width. Calculate value with all possible hours (supposed widest text).
*/
private void initTextTimeWidth() {
mTimeTextWidth = 0;
for (int i = 0; i < getNumberOfPeriods(); i++) {
// Measure time string and get max width.
String time = getDateTimeInterpreter().interpretTime(i, (i % 2) * 30);
if (time == null)
throw new IllegalStateException("A DateTimeInterpreter must not return null time");
mTimeTextWidth = Math.max(mTimeTextWidth, mTimeTextPaint.measureText(time));
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Draw the header row.
drawHeaderRowAndEvents(canvas);
// Draw the time column and all the axes/separators.
drawTimeColumnAndAxes(canvas);
}
private void calculateHeaderHeight() {
//Make sure the header is the right size (depends on AllDay events)
boolean containsAllDayEvent = false;
if (mEventRects != null && mEventRects.size() > 0) {
for (int dayNumber = 0;
dayNumber < getRealNumberOfVisibleDays();
dayNumber++) {
Calendar day = (Calendar) getFirstVisibleDay().clone();
day.add(Calendar.DATE, dayNumber);
for (int i = 0; i < mEventRects.size(); i++) {
if (isSameDay(mEventRects.get(i).event.getStartTime(), day) && mEventRects.get(i).event.isAllDay()) {
containsAllDayEvent = true;
break;
}
}
if (containsAllDayEvent) {
break;
}
}
}
if (containsAllDayEvent) {
mHeaderHeight = mHeaderTextHeight + (mAllDayEventHeight + mHeaderMarginBottom);
} else {
mHeaderHeight = mHeaderTextHeight;
}
}
private void drawTimeColumnAndAxes(Canvas canvas) {
// Draw the background color for the header column.
canvas.drawRect(0, mHeaderHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight(), mHeaderColumnBackgroundPaint);
// Clip to paint in left column only.
canvas.save();
canvas.clipRect(0, mHeaderHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight());
canvas.restore();
for (int i = 0; i < getNumberOfPeriods(); i++) {
// If we are showing half hours (eg. 5:30am), space the times out by half the hour height
// and need to provide 30 minutes on each odd period, otherwise, minutes is always 0.
float timeSpacing;
int minutes;
int hour;
float timesPerHour = (float) 60.0 / mTimeColumnResolution;
timeSpacing = mHourHeight / timesPerHour;
hour = mMinTime + i / (int) (timesPerHour);
minutes = i % ((int) timesPerHour) * (60 / (int) timesPerHour);
// Calculate the top of the rectangle where the time text will go
float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + timeSpacing * i + mHeaderMarginBottom;
// Get the time to be displayed, as a String.
String time = getDateTimeInterpreter().interpretTime(hour, minutes);
// Draw the text if its y position is not outside of the visible area. The pivot point of the text is the point at the bottom-right corner.
if (time == null)
throw new IllegalStateException("A DateTimeInterpreter must not return null time");
if (top < getHeight())
canvas.drawText(time, mTimeTextWidth + mHeaderColumnPadding, top + mTimeTextHeight, mTimeTextPaint);
}
}
private void drawHeaderRowAndEvents(Canvas canvas) {
// Calculate the available width for each day.
mHeaderColumnWidth = mTimeTextWidth + mHeaderColumnPadding * 2;
mWidthPerDay = getWidth() - mHeaderColumnWidth - mColumnGap * (getRealNumberOfVisibleDays() - 1);
mWidthPerDay = mWidthPerDay / getRealNumberOfVisibleDays();
calculateHeaderHeight(); //Make sure the header is the right size (depends on AllDay events)
Calendar today = today();
if (mAreDimensionsInvalid) {
mEffectiveMinHourHeight = Math.max(mMinHourHeight, (int) ((getHeight() - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom) / (mMaxTime - mMinTime)));
mAreDimensionsInvalid = false;
if (mScrollToDay != null)
goToDate(mScrollToDay);
mAreDimensionsInvalid = false;
if (mScrollToHour >= 0)
goToHour(mScrollToHour);
mScrollToDay = null;
mScrollToHour = -1;
mAreDimensionsInvalid = false;
}
if (mIsFirstDraw) {
mIsFirstDraw = false;
// If the week view is being drawn for the first time, then consider the first day of the week.
if (getRealNumberOfVisibleDays() >= 7 && mHomeDate.get(Calendar.DAY_OF_WEEK) != mFirstDayOfWeek && mShowFirstDayOfWeekFirst) {
int difference = (mHomeDate.get(Calendar.DAY_OF_WEEK) - mFirstDayOfWeek);
mCurrentOrigin.x += (mWidthPerDay + mColumnGap) * difference;
}
setLimitTime(mMinTime, mMaxTime);
}
// Calculate the new height due to the zooming.
if (mNewHourHeight > 0) {
if (mNewHourHeight < mEffectiveMinHourHeight)
mNewHourHeight = mEffectiveMinHourHeight;
else if (mNewHourHeight > mMaxHourHeight)
mNewHourHeight = mMaxHourHeight;
mHourHeight = mNewHourHeight;
mNewHourHeight = -1;
}
// If the new mCurrentOrigin.y is invalid, make it valid.
if (mCurrentOrigin.y < getHeight() - mHourHeight * (mMaxTime - mMinTime) - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight / 2)
mCurrentOrigin.y = getHeight() - mHourHeight * (mMaxTime - mMinTime) - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight / 2;
// Don't put an "else if" because it will trigger a glitch when completely zoomed out and
// scrolling vertically.
if (mCurrentOrigin.y > 0) {
mCurrentOrigin.y = 0;
}
int leftDaysWithGaps = getLeftDaysWithGaps();
// Consider scroll offset.
float startFromPixel = getXStartPixel();
float startPixel = startFromPixel;
// Prepare to iterate for each day.
Calendar day = (Calendar) today.clone();
day.add(Calendar.HOUR_OF_DAY, 6);
// Prepare to iterate for each hour to draw the hour lines.
int lineCount = (int) ((getHeight() - mHeaderHeight - mHeaderRowPadding * 2 -
mHeaderMarginBottom) / mHourHeight) + 1;
lineCount = (lineCount) * (getRealNumberOfVisibleDays() + 1);
float[] hourLines = new float[lineCount * 4];
// Clear the cache for event rectangles.
if (mEventRects != null) {
for (EventRect eventRect : mEventRects) {
eventRect.rectF = null;
}
}
// Clip to paint events only.
canvas.save();
canvas.clipRect(mHeaderColumnWidth, mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight / 2, getWidth(), getHeight());
// Iterate through each day.
Calendar oldFirstVisibleDay = mFirstVisibleDay;
mFirstVisibleDay = (Calendar) mHomeDate.clone();
mFirstVisibleDay.add(Calendar.DATE, -(Math.round(mCurrentOrigin.x / (mWidthPerDay + mColumnGap))));
if (!mFirstVisibleDay.equals(oldFirstVisibleDay) && mScrollListener != null) {
mScrollListener.onFirstVisibleDayChanged(mFirstVisibleDay, oldFirstVisibleDay);
}
if (mAutoLimitTime) {
List<Calendar> days = new ArrayList<>();
for (int dayNumber = leftDaysWithGaps + 1;
dayNumber <= leftDaysWithGaps + getRealNumberOfVisibleDays();
dayNumber++) {
day = (Calendar) mHomeDate.clone();
day.add(Calendar.DATE, dayNumber - 1);
days.add(day);
}
limitEventTime(days);
}
for (int dayNumber = leftDaysWithGaps + 1;
dayNumber <= leftDaysWithGaps + getRealNumberOfVisibleDays() + 1;
dayNumber++) {
// Check if the day is today.
day = (Calendar) mHomeDate.clone();
mLastVisibleDay = (Calendar) day.clone();
day.add(Calendar.DATE, dayNumber - 1);
mLastVisibleDay.add(Calendar.DATE, dayNumber - 2);
boolean isToday = isSameDay(day, today);
// Don't draw days which are outside requested range
if (!dateIsValid(day)) {
continue;
}
// Get more events if necessary. We want to store the events 3 months beforehand. Get
// events only when it is the first iteration of the loop.
if (!isInEditMode() && (mEventRects == null || mRefreshEvents ||
(dayNumber == leftDaysWithGaps + 1 && mFetchedPeriod != (int) mWeekViewLoader.toWeekViewPeriodIndex(day) &&
Math.abs(mFetchedPeriod - mWeekViewLoader.toWeekViewPeriodIndex(day)) > 0.5))) {
getMoreEvents(day);
mRefreshEvents = false;
}
// Draw background color for each day.
float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel);
if (mWidthPerDay + startPixel - start > 0) {
if (mShowDistinctPastFutureColor) {
boolean isWeekend = day.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || day.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
Paint pastPaint = isWeekend && mShowDistinctWeekendColor ? mPastWeekendBackgroundPaint : mPastBackgroundPaint;
Paint futurePaint = isWeekend && mShowDistinctWeekendColor ? mFutureWeekendBackgroundPaint : mFutureBackgroundPaint;
float startY = mHeaderHeight + mHeaderRowPadding * 2 + mTimeTextHeight / 2 + mHeaderMarginBottom + mCurrentOrigin.y;
if (isToday) {
Calendar now = Calendar.getInstance();
float beforeNow = (now.get(Calendar.HOUR_OF_DAY) - mMinTime + now.get(Calendar.MINUTE) / 60.0f) * mHourHeight;
canvas.drawRect(start, startY, startPixel + mWidthPerDay, startY + beforeNow, pastPaint);
canvas.drawRect(start, startY + beforeNow, startPixel + mWidthPerDay, getHeight(), futurePaint);
} else if (day.before(today)) {
canvas.drawRect(start, startY, startPixel + mWidthPerDay, getHeight(), pastPaint);
} else {
canvas.drawRect(start, startY, startPixel + mWidthPerDay, getHeight(), futurePaint);
}
} else {
canvas.drawRect(start, mHeaderHeight + mHeaderRowPadding * 2 + mTimeTextHeight / 2 + mHeaderMarginBottom, startPixel + mWidthPerDay, getHeight(), isToday ? mTodayBackgroundPaint : mDayBackgroundPaint);
}
}
// Prepare the separator lines for hours.
int i = 0;
for (int hourNumber = mMinTime; hourNumber < mMaxTime; hourNumber++) {
float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * (hourNumber - mMinTime) + mTimeTextHeight / 2 + mHeaderMarginBottom;
if (top > mHeaderHeight + mHeaderRowPadding * 2 + mTimeTextHeight / 2 + mHeaderMarginBottom - mHourSeparatorHeight && top < getHeight() && startPixel + mWidthPerDay - start > 0) {
hourLines[i * 4] = start;
hourLines[i * 4 + 1] = top;
hourLines[i * 4 + 2] = startPixel + mWidthPerDay;
hourLines[i * 4 + 3] = top;
i++;
}
}
// Draw the lines for hours.
canvas.drawLines(hourLines, mHourSeparatorPaint);
// Draw the events.
drawEvents(day, startPixel, canvas);
// Draw the line at the current time.
if (mShowNowLine && isToday) {
float startY = mHeaderHeight + mHeaderRowPadding * 2 + mTimeTextHeight / 2 + mHeaderMarginBottom + mCurrentOrigin.y;
Calendar now = Calendar.getInstance();
float beforeNow = (now.get(Calendar.HOUR_OF_DAY) - mMinTime + now.get(Calendar.MINUTE) / 60.0f) * mHourHeight;
float top = startY + beforeNow;
canvas.drawLine(start, top, startPixel + mWidthPerDay, top, mNowLinePaint);
}
// In the next iteration, start from the next day.
startPixel += mWidthPerDay + mColumnGap;
}
canvas.restore();
// Hide everything in the first cell (top left corner).
canvas.save();
canvas.clipRect(0, 0, mTimeTextWidth + mHeaderColumnPadding * 2, mHeaderHeight + mHeaderRowPadding * 2);
canvas.drawRect(0, 0, mTimeTextWidth + mHeaderColumnPadding * 2, mHeaderHeight + mHeaderRowPadding * 2, mHeaderBackgroundPaint);
canvas.restore();
// Clip to paint header row only.
canvas.save();
canvas.clipRect(mHeaderColumnWidth, 0, getWidth(), mHeaderHeight + mHeaderRowPadding * 2);
// Draw the header background.
canvas.drawRect(0, 0, getWidth(), mHeaderHeight + mHeaderRowPadding * 2, mHeaderBackgroundPaint);
canvas.restore();
// Draw the header row texts.
startPixel = startFromPixel;
for (int dayNumber = leftDaysWithGaps + 1; dayNumber <= leftDaysWithGaps + getRealNumberOfVisibleDays() + 1; dayNumber++) {
// Check if the day is today.
day = (Calendar) mHomeDate.clone();
day.add(Calendar.DATE, dayNumber - 1);
boolean isToday = isSameDay(day, today);
// Don't draw days which are outside requested range
if (!dateIsValid(day))
continue;
// Draw the day labels.
String dayLabel = getDateTimeInterpreter().interpretDate(day);
if (dayLabel == null)
throw new IllegalStateException("A DateTimeInterpreter must not return null date");
canvas.drawText(dayLabel, startPixel + mWidthPerDay / 2, mHeaderTextHeight + mHeaderRowPadding, isToday ? mTodayHeaderTextPaint : mHeaderTextPaint);
drawAllDayEvents(day, startPixel, canvas);
startPixel += mWidthPerDay + mColumnGap;
}
}
/**
* Get the time and date where the user clicked on.
*
* @param x The x position of the touch event.
* @param y The y position of the touch event.
* @return The time and date at the clicked position.
*/
private Calendar getTimeFromPoint(float x, float y) {
int leftDaysWithGaps = getLeftDaysWithGaps();
float startPixel = getXStartPixel();
for (int dayNumber = leftDaysWithGaps + 1;
dayNumber <= leftDaysWithGaps + getRealNumberOfVisibleDays() + 1;
dayNumber++) {
float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel);
if (mWidthPerDay + startPixel - start > 0 && x > start && x < startPixel + mWidthPerDay) {
Calendar day = (Calendar) mHomeDate.clone();
day.add(Calendar.DATE, dayNumber - 1);
float pixelsFromZero = y - mCurrentOrigin.y - mHeaderHeight
- mHeaderRowPadding * 2 - mTimeTextHeight / 2 - mHeaderMarginBottom;
int hour = (int) (pixelsFromZero / mHourHeight);
int minute = (int) (60 * (pixelsFromZero - hour * mHourHeight) / mHourHeight);
day.add(Calendar.HOUR_OF_DAY, hour + mMinTime);
day.set(Calendar.MINUTE, minute);
return day;
}