6
6
import android .graphics .drawable .BitmapDrawable ;
7
7
import android .graphics .drawable .Drawable ;
8
8
import android .os .Build ;
9
+
10
+ import androidx .annotation .NonNull ;
9
11
import androidx .annotation .Nullable ;
10
12
import androidx .annotation .RequiresApi ;
11
13
import androidx .core .view .GestureDetectorCompat ;
@@ -61,7 +63,7 @@ private enum AutoScrollDirection {
61
63
private float mHeaderHeight ;
62
64
private GestureDetectorCompat mGestureDetector ;
63
65
private OverScroller mScroller ;
64
- private PointF mCurrentOrigin = new PointF (0f , 0f );
66
+ private final PointF mCurrentOrigin = new PointF (0f , 0f );
65
67
private Direction mCurrentScrollDirection = Direction .NONE ;
66
68
private Paint mHeaderBackgroundPaint ;
67
69
private float mWidthPerDay ;
@@ -98,15 +100,17 @@ private enum AutoScrollDirection {
98
100
private EventRect mNewEventRect ;
99
101
private boolean creatingNewEvent = false ;
100
102
private boolean movingNewEvent = false ;
103
+ private MotionEvent mNewEventLastTouch ;
101
104
private NewEventScrollDirection mCurrentNewEventScrollDirection = NewEventScrollDirection .NONE ;
102
105
private float mNewEventDragOffset = 0 ;
103
- private Handler autoScrollHandler = new Handler (Looper .getMainLooper ());
106
+ private final Handler autoScrollHandler = new Handler (Looper .getMainLooper ());
104
107
private boolean isAutoScrolling = false ;
105
108
private Runnable autoScrollRunnable ;
106
- private int mAutoScrollInterval = 1200 ;
107
- private int mAutoScrollDuration = 300 ;
108
- private int mAutoScrollLeftThreshold = 150 ;
109
- private int mAutoScrollRightThreshold = 150 ;
109
+ private final int mNewEventVerticalScrollDuration = 100 ;
110
+ private final int mAutoScrollInterval = 1200 ;
111
+ private final int mAutoScrollDuration = 300 ;
112
+ private final int mAutoScrollLeftThreshold = 150 ;
113
+ private final int mAutoScrollRightThreshold = 150 ;
110
114
111
115
// Attributes and their default values.
112
116
private int mHourHeight = 50 ;
@@ -171,8 +175,7 @@ private enum AutoScrollDirection {
171
175
private boolean mAutoLimitTime = false ;
172
176
private boolean mEnableDropListener = false ;
173
177
private int mMinOverlappingMinutes = 0 ;
174
- private MotionEvent mNewEventLastTouch ;
175
- private int mNewEventVerticalScrollDuration = 100 ;
178
+
176
179
177
180
// Listeners.
178
181
private EventClickListener mEventClickListener ;
@@ -189,16 +192,15 @@ private enum AutoScrollDirection {
189
192
private final GestureDetector .SimpleOnGestureListener mGestureListener = new GestureDetector .SimpleOnGestureListener () {
190
193
191
194
@ Override
192
- public boolean onDown (MotionEvent e ) {
195
+ public boolean onDown (@ NonNull MotionEvent e ) {
193
196
if (!wasOnNewEventRect (e )) {
194
197
stopScrolling ();
195
198
}
196
199
return true ;
197
200
}
198
201
199
202
@ Override
200
- public boolean onSingleTapUp (MotionEvent e ) {
201
- Log .d ("QuivrWeekView" , "singleTapUp" );
203
+ public boolean onSingleTapUp (@ NonNull MotionEvent e ) {
202
204
goToNearestOrigin ();
203
205
boolean wasEmptyClick = true ;
204
206
boolean doRemoveNewEvent = false ;
@@ -237,18 +239,19 @@ public boolean onSingleTapUp(MotionEvent e) {
237
239
238
240
239
241
@ Override
240
- public boolean onScroll (MotionEvent e1 , MotionEvent e2 , float distanceX , float distanceY ) {
241
- if ((movingNewEvent ) || (creatingNewEvent && wasOnNewEventRect (e1 ))) {
242
+ public boolean onScroll (MotionEvent e1 , @ NonNull MotionEvent e2 , float distanceX , float distanceY ) {
243
+ if ((movingNewEvent ) || (creatingNewEvent && e1 != null && wasOnNewEventRect (e1 ))) {
242
244
return true ;
243
245
}
244
246
// Check if view is zoomed.
245
247
if (mIsZooming )
246
248
return true ;
247
249
250
+ boolean isHorizontal = Math .abs (distanceX ) > Math .abs (distanceY );
248
251
switch (mCurrentScrollDirection ) {
249
252
case NONE : {
250
253
// Allow scrolling only in one direction.
251
- if (Math . abs ( distanceX ) > Math . abs ( distanceY ) ) {
254
+ if (isHorizontal ) {
252
255
if (distanceX > 0 ) {
253
256
mCurrentScrollDirection = Direction .LEFT ;
254
257
} else {
@@ -261,14 +264,14 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
261
264
}
262
265
case LEFT : {
263
266
// Change direction if there was enough change.
264
- if (Math . abs ( distanceX ) > Math . abs ( distanceY ) && (distanceX < -mScaledTouchSlop )) {
267
+ if (isHorizontal && (distanceX < -mScaledTouchSlop )) {
265
268
mCurrentScrollDirection = Direction .RIGHT ;
266
269
}
267
270
break ;
268
271
}
269
272
case RIGHT : {
270
273
// Change direction if there was enough change.
271
- if (Math . abs ( distanceX ) > Math . abs ( distanceY ) && (distanceX > mScaledTouchSlop )) {
274
+ if (isHorizontal && (distanceX > mScaledTouchSlop )) {
272
275
mCurrentScrollDirection = Direction .LEFT ;
273
276
}
274
277
break ;
@@ -311,7 +314,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
311
314
}
312
315
313
316
@ Override
314
- public boolean onFling (MotionEvent e1 , MotionEvent e2 , float velocityX , float velocityY ) {
317
+ public boolean onFling (MotionEvent e1 , @ NonNull MotionEvent e2 , float velocityX , float velocityY ) {
315
318
if (mIsZooming || movingNewEvent )
316
319
return true ;
317
320
@@ -342,7 +345,7 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
342
345
343
346
344
347
@ Override
345
- public void onLongPress (MotionEvent e ) {
348
+ public void onLongPress (@ NonNull MotionEvent e ) {
346
349
super .onLongPress (e );
347
350
goToNearestOrigin ();
348
351
@@ -649,7 +652,7 @@ private void initTextTimeWidth() {
649
652
}
650
653
651
654
@ Override
652
- protected void onDraw (Canvas canvas ) {
655
+ protected void onDraw (@ NonNull Canvas canvas ) {
653
656
super .onDraw (canvas );
654
657
655
658
// Draw the header row.
@@ -662,7 +665,7 @@ protected void onDraw(Canvas canvas) {
662
665
private void calculateHeaderHeight () {
663
666
//Make sure the header is the right size (depends on AllDay events)
664
667
boolean containsAllDayEvent = false ;
665
- if (mEventRects != null && mEventRects .size () > 0 ) {
668
+ if (mEventRects != null && ! mEventRects .isEmpty () ) {
666
669
for (int dayNumber = 0 ;
667
670
dayNumber < getRealNumberOfVisibleDays ();
668
671
dayNumber ++) {
@@ -854,7 +857,7 @@ else if (mNewHourHeight > mMaxHourHeight)
854
857
}
855
858
856
859
// Draw background color for each day.
857
- float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel );
860
+ float start = (Math . max ( startPixel , mHeaderColumnWidth ) );
858
861
if (mWidthPerDay + startPixel - start > 0 ) {
859
862
if (mShowDistinctPastFutureColor ) {
860
863
boolean isWeekend = day .get (Calendar .DAY_OF_WEEK ) == Calendar .SATURDAY || day .get (Calendar .DAY_OF_WEEK ) == Calendar .SUNDAY ;
@@ -960,7 +963,7 @@ private Calendar getTimeFromPoint(float x, float y) {
960
963
for (int dayNumber = leftDaysWithGaps + 1 ;
961
964
dayNumber <= leftDaysWithGaps + getRealNumberOfVisibleDays () + 1 ;
962
965
dayNumber ++) {
963
- float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel );
966
+ float start = (Math . max ( startPixel , mHeaderColumnWidth ) );
964
967
if (mWidthPerDay + startPixel - start > 0 && x > start && x < startPixel + mWidthPerDay ) {
965
968
Calendar day = (Calendar ) mHomeDate .clone ();
966
969
day .add (Calendar .DATE , dayNumber - 1 );
@@ -982,7 +985,7 @@ private Calendar getTimeFromPoint(float x, float y) {
982
985
* find smallest of start time & latest of end time
983
986
*/
984
987
private void limitEventTime (List <Calendar > dates ) {
985
- if (mEventRects != null && mEventRects .size () > 0 ) {
988
+ if (mEventRects != null && ! mEventRects .isEmpty () ) {
986
989
Calendar startTime = null ;
987
990
Calendar endTime = null ;
988
991
@@ -1036,7 +1039,7 @@ private float getXStartPixel() {
1036
1039
* @param canvas The canvas to draw upon.
1037
1040
*/
1038
1041
private void drawEvents (Calendar date , float startFromPixel , Canvas canvas ) {
1039
- if (mEventRects != null && mEventRects .size () > 0 ) {
1042
+ if (mEventRects != null && ! mEventRects .isEmpty () ) {
1040
1043
for (int i = 0 ; i < mEventRects .size (); i ++) {
1041
1044
if (isSameDay (mEventRects .get (i ).event .getStartTime (), date ) && !mEventRects .get (i ).event .isAllDay ()) {
1042
1045
float top = mHourHeight * mEventRects .get (i ).top / 60 + getEventsTop ();
@@ -1063,7 +1066,7 @@ top < getHeight() &&
1063
1066
canvas .drawRoundRect (mEventRects .get (i ).rectF , 30 , 30 , mEventBackgroundPaint );
1064
1067
float topToUse = top ;
1065
1068
if (mEventRects .get (i ).event .getStartTime ().get (Calendar .HOUR_OF_DAY ) < mMinTime )
1066
- topToUse = mHourHeight * getPassedMinutesInDay (mMinTime , 0 ) / 60 + getEventsTop ();
1069
+ topToUse = mHourHeight * ( getPassedMinutesInDay (mMinTime , 0 ) / 60 ) + getEventsTop ();
1067
1070
1068
1071
if (!mNewEventIdentifier .equals (mEventRects .get (i ).event .getIdentifier ()))
1069
1072
drawEventTitle (mEventRects .get (i ).event , mEventRects .get (i ).rectF , canvas , topToUse , left );
@@ -1085,7 +1088,7 @@ top < getHeight() &&
1085
1088
* @param canvas The canvas to draw upon.
1086
1089
*/
1087
1090
private void drawAllDayEvents (Calendar date , float startFromPixel , Canvas canvas ) {
1088
- if (mEventRects != null && mEventRects .size () > 0 ) {
1091
+ if (mEventRects != null && ! mEventRects .isEmpty () ) {
1089
1092
for (int i = 0 ; i < mEventRects .size (); i ++) {
1090
1093
if (isSameDay (mEventRects .get (i ).event .getStartTime (), date ) && mEventRects .get (i ).event .isAllDay ()) {
1091
1094
@@ -1204,7 +1207,7 @@ private void drawEmptyImage(WeekViewEvent event, RectF rect, Canvas canvas, floa
1204
1207
* stored in "originalEvent". But the event that corresponds to rectangle the rectangle
1205
1208
* instance will be stored in "event".
1206
1209
*/
1207
- private class EventRect {
1210
+ private static class EventRect {
1208
1211
public WeekViewEvent event ;
1209
1212
public WeekViewEvent originalEvent ;
1210
1213
public RectF rectF ;
@@ -1280,7 +1283,7 @@ private void getMoreEvents(Calendar day) {
1280
1283
mEventRects = new ArrayList <>();
1281
1284
1282
1285
// Iterate through each day with events to calculate the position of the events.
1283
- while (tempEvents .size () > 0 ) {
1286
+ while (! tempEvents .isEmpty () ) {
1284
1287
ArrayList <EventRect > eventRects = new ArrayList <>(tempEvents .size ());
1285
1288
1286
1289
// Get first event for a day.
@@ -1341,16 +1344,16 @@ private void cacheAndSortEvents(List<? extends WeekViewEvent> events) {
1341
1344
* @param eventRects The events to be sorted.
1342
1345
*/
1343
1346
private void sortEventRects (List <EventRect > eventRects ) {
1344
- Collections .sort (eventRects , new Comparator <EventRect >() {
1347
+ eventRects .sort (new Comparator <EventRect >() {
1345
1348
@ Override
1346
1349
public int compare (EventRect left , EventRect right ) {
1347
1350
long start1 = left .event .getStartTime ().getTimeInMillis ();
1348
1351
long start2 = right .event .getStartTime ().getTimeInMillis ();
1349
- int comparator = start1 > start2 ? 1 : (start1 < start2 ? - 1 : 0 );
1352
+ int comparator = Long . compare (start1 , start2 );
1350
1353
if (comparator == 0 ) {
1351
1354
long end1 = left .event .getEndTime ().getTimeInMillis ();
1352
1355
long end2 = right .event .getEndTime ().getTimeInMillis ();
1353
- comparator = end1 > end2 ? 1 : (end1 < end2 ? - 1 : 0 );
1356
+ comparator = Long . compare (end1 , end2 );
1354
1357
}
1355
1358
return comparator ;
1356
1359
}
@@ -1405,7 +1408,7 @@ private void expandEventsToMaxWidth(List<EventRect> collisionGroup) {
1405
1408
for (EventRect eventRect : collisionGroup ) {
1406
1409
boolean isPlaced = false ;
1407
1410
for (List <EventRect > column : columns ) {
1408
- if (column .size () == 0 ) {
1411
+ if (column .isEmpty () ) {
1409
1412
column .add (eventRect );
1410
1413
isPlaced = true ;
1411
1414
} else if (!isEventsCollide (eventRect .event , column .get (column .size () - 1 ).event )) {
@@ -1462,7 +1465,7 @@ private boolean isEventsCollide(WeekViewEvent event1, WeekViewEvent event2) {
1462
1465
long start2 = event2 .getStartTime ().getTimeInMillis ();
1463
1466
long end2 = event2 .getEndTime ().getTimeInMillis ();
1464
1467
1465
- long minOverlappingMillis = mMinOverlappingMinutes * 60 * 1000 ;
1468
+ long minOverlappingMillis = ( long ) mMinOverlappingMinutes * 60 * 1000 ;
1466
1469
1467
1470
return !((start1 + minOverlappingMillis >= end2 ) || (end1 <= start2 + minOverlappingMillis ));
1468
1471
}
@@ -2352,15 +2355,15 @@ public void setZoomFocusPointEnabled(boolean zoomFocusPointEnabled) {
2352
2355
mZoomFocusPointEnabled = zoomFocusPointEnabled ;
2353
2356
}
2354
2357
2355
- /*
2358
+ /**
2356
2359
* Is focus point enabled
2357
2360
* @return fixed focus point enabled?
2358
2361
*/
2359
2362
public boolean isZoomFocusPointEnabled () {
2360
2363
return mZoomFocusPointEnabled ;
2361
2364
}
2362
2365
2363
- /*
2366
+ /**
2364
2367
* Get focus point
2365
2368
* 0 = top of view, 1 = bottom of view
2366
2369
* The focused point (multiplier of the view height) where the week view is zoomed around.
@@ -2467,17 +2470,13 @@ public void setNewEventIconDrawable(Drawable newEventIconDrawable) {
2467
2470
public void enableDropListener () {
2468
2471
this .mEnableDropListener = true ;
2469
2472
//set drag and drop listener, required Honeycomb+ Api level
2470
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .HONEYCOMB ) {
2471
- setOnDragListener (new DragListener ());
2472
- }
2473
+ setOnDragListener (new DragListener ());
2473
2474
}
2474
2475
2475
2476
public void disableDropListener () {
2476
2477
this .mEnableDropListener = false ;
2477
2478
//set drag and drop listener, required Honeycomb+ Api level
2478
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .HONEYCOMB ) {
2479
- setOnDragListener (null );
2480
- }
2479
+ setOnDragListener (null );
2481
2480
}
2482
2481
2483
2482
public boolean isDropListenerEnabled () {
@@ -2500,7 +2499,6 @@ public int getMinOverlappingMinutes() {
2500
2499
2501
2500
@ Override
2502
2501
public boolean onTouchEvent (MotionEvent event ) {
2503
- // TODO: fix single tap up
2504
2502
mScaleDetector .onTouchEvent (event );
2505
2503
boolean val = mGestureDetector .onTouchEvent (event );
2506
2504
if (event .getAction () == MotionEvent .ACTION_DOWN && creatingNewEvent && wasOnNewEventRect (event )) {
@@ -2779,8 +2777,6 @@ public boolean dateIsValid(Calendar day) {
2779
2777
}
2780
2778
2781
2779
private void updateNewEvent (MotionEvent e ) {
2782
- Log .d ("QuivrWeekView" , String .format ("updateNewEvent: %s%n" , e .toString ()));
2783
-
2784
2780
float x = e .getX ();
2785
2781
2786
2782
// Ugly fix for when the user is dragging the event over the small left time column
@@ -2868,7 +2864,6 @@ private void removeNewEvent() {
2868
2864
}
2869
2865
2870
2866
private void startNewEventAdding (MotionEvent e ) {
2871
- Log .d ("QuivrWeekView" , "start new event adding!" );
2872
2867
playSoundEffect (SoundEffectConstants .CLICK );
2873
2868
2874
2869
Calendar selectedTime = getTimeFromPoint (e .getX (), e .getY ());
@@ -2971,15 +2966,15 @@ private class WeekViewGestureListener implements ScaleGestureDetector.OnScaleGes
2971
2966
float mFocusedPointY ;
2972
2967
2973
2968
@ Override
2974
- public void onScaleEnd (ScaleGestureDetector detector ) {
2969
+ public void onScaleEnd (@ NonNull ScaleGestureDetector detector ) {
2975
2970
mIsZooming = false ;
2976
2971
if (mZoomEndListener != null ) {
2977
2972
mZoomEndListener .onZoomEnd (mHourHeight );
2978
2973
}
2979
2974
}
2980
2975
2981
2976
@ Override
2982
- public boolean onScaleBegin (ScaleGestureDetector detector ) {
2977
+ public boolean onScaleBegin (@ NonNull ScaleGestureDetector detector ) {
2983
2978
mIsZooming = true ;
2984
2979
goToNearestOrigin ();
2985
2980
@@ -3014,19 +3009,16 @@ public boolean onScale(ScaleGestureDetector detector) {
3014
3009
3015
3010
}
3016
3011
3017
- @ RequiresApi (api = Build .VERSION_CODES .HONEYCOMB )
3018
3012
private class DragListener implements View .OnDragListener {
3019
3013
@ Override
3020
3014
public boolean onDrag (View v , DragEvent e ) {
3021
- switch (e .getAction ()) {
3022
- case DragEvent .ACTION_DROP :
3023
- if (e .getX () > mHeaderColumnWidth && e .getY () > (mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom )) {
3024
- Calendar selectedTime = getTimeFromPoint (e .getX (), e .getY ());
3025
- if (selectedTime != null ) {
3026
- mDropListener .onDrop (v , selectedTime );
3027
- }
3015
+ if (e .getAction () == DragEvent .ACTION_DROP ) {
3016
+ if (e .getX () > mHeaderColumnWidth && e .getY () > (mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom )) {
3017
+ Calendar selectedTime = getTimeFromPoint (e .getX (), e .getY ());
3018
+ if (selectedTime != null ) {
3019
+ mDropListener .onDrop (v , selectedTime );
3028
3020
}
3029
- break ;
3021
+ }
3030
3022
}
3031
3023
return true ;
3032
3024
}
0 commit comments