9
9
import android .graphics .Rect ;
10
10
import android .graphics .RectF ;
11
11
import android .graphics .Typeface ;
12
+ import android .support .annotation .NonNull ;
12
13
import android .support .v4 .view .GestureDetectorCompat ;
13
14
import android .support .v4 .view .ViewCompat ;
14
15
import android .text .Layout ;
25
26
import android .widget .OverScroller ;
26
27
import android .widget .Scroller ;
27
28
29
+ import java .text .SimpleDateFormat ;
28
30
import java .util .ArrayList ;
29
31
import java .util .Calendar ;
30
32
import java .util .Collections ;
37
39
*/
38
40
public class WeekView extends View {
39
41
42
+ @ Deprecated
40
43
public static final int LENGTH_SHORT = 1 ;
44
+ @ Deprecated
41
45
public static final int LENGTH_LONG = 2 ;
42
46
private final Context mContext ;
43
47
private Calendar mToday ;
@@ -91,7 +95,7 @@ public class WeekView extends View {
91
95
private int mHeaderColumnBackgroundColor = Color .WHITE ;
92
96
private int mDefaultEventColor ;
93
97
private boolean mIsFirstDraw = true ;
94
- private int mDayNameLength = LENGTH_LONG ;
98
+ @ Deprecated private int mDayNameLength = LENGTH_LONG ;
95
99
private int mOverlappingEventGap = 0 ;
96
100
private int mEventMarginVertical = 0 ;
97
101
private float mXScrollingSpeed = 1f ;
@@ -103,6 +107,7 @@ public class WeekView extends View {
103
107
private EventLongPressListener mEventLongPressListener ;
104
108
private MonthChangeListener mMonthChangeListener ;
105
109
private TimeClickListener mTimeClickListener ;
110
+ private DateTimeInterpreter mDateTimeInterpreter ;
106
111
107
112
private final GestureDetector .SimpleOnGestureListener mGestureListener = new GestureDetector .SimpleOnGestureListener () {
108
113
@@ -193,7 +198,6 @@ public void onLongPress(MotionEvent e) {
193
198
}
194
199
};
195
200
196
-
197
201
private enum Direction {
198
202
NONE , HORIZONTAL , VERTICAL
199
203
}
@@ -352,7 +356,10 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
352
356
float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + mHourHeight * i + mHeaderMarginBottom ;
353
357
354
358
// 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.
355
- if (top < getHeight ()) canvas .drawText (getTimeString (i ), mTimeTextWidth + mHeaderColumnPadding , top + mTimeTextHeight , mTimeTextPaint );
359
+ String time = getDateTimeInterpreter ().interpretTime (i );
360
+ if (time == null )
361
+ throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
362
+ if (top < getHeight ()) canvas .drawText (time , mTimeTextWidth + mHeaderColumnPadding , top + mTimeTextHeight , mTimeTextPaint );
356
363
}
357
364
}
358
365
@@ -456,7 +463,9 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
456
463
boolean sameDay = isSameDay (day , mToday );
457
464
458
465
// Draw the day labels.
459
- String dayLabel = String .format ("%s %d/%02d" , getDayName (day ), day .get (Calendar .MONTH ) + 1 , day .get (Calendar .DAY_OF_MONTH ));
466
+ String dayLabel = getDateTimeInterpreter ().interpretDate (day );
467
+ if (dayLabel == null )
468
+ throw new IllegalStateException ("A DateTimeInterpreter must not return null date" );
460
469
canvas .drawText (dayLabel , startPixel + mWidthPerDay / 2 , mHeaderTextHeight + mHeaderRowPadding , sameDay ? mTodayHeaderTextPaint : mHeaderTextPaint );
461
470
startPixel += mWidthPerDay + mColumnGap ;
462
471
}
@@ -917,12 +926,54 @@ public void setEventLongPressListener(EventLongPressListener eventLongPressListe
917
926
this .mEventLongPressListener = eventLongPressListener ;
918
927
}
919
928
929
+ public void setHourClickListener (TimeClickListener mTimeClickListener ){
930
+ this .mTimeClickListener = mTimeClickListener ;
931
+ }
932
+
920
933
public TimeClickListener getHourClickListener (){
921
934
return mTimeClickListener ;
922
935
}
923
936
924
- public void setHourClickListener (TimeClickListener mTimeClickListener ){
925
- this .mTimeClickListener = mTimeClickListener ;
937
+ /**
938
+ * Get the interpreter which provides the text to show in the header column and the header row.
939
+ * @return The date, time interpreter.
940
+ */
941
+ public @ NonNull DateTimeInterpreter getDateTimeInterpreter () {
942
+ if (mDateTimeInterpreter == null ) {
943
+ mDateTimeInterpreter = new DateTimeInterpreter () {
944
+ @ Override
945
+ public String interpretDate (Calendar date ) {
946
+ SimpleDateFormat sdf ;
947
+ sdf = mDayNameLength == LENGTH_SHORT ? new SimpleDateFormat ("EEEEE" ) : new SimpleDateFormat ("EEE" );
948
+ try {
949
+ String dayName = sdf .format (date .getTime ()).toUpperCase ();
950
+ return String .format ("%s %d/%02d" , dayName , date .get (Calendar .MONTH ) + 1 , date .get (Calendar .DAY_OF_MONTH ));
951
+ }catch (Exception e ){
952
+ e .printStackTrace ();
953
+ return "" ;
954
+ }
955
+ }
956
+
957
+ @ Override
958
+ public String interpretTime (int hour ) {
959
+ String amPm ;
960
+ if (hour >= 0 && hour < 12 ) amPm = "AM" ;
961
+ else amPm = "PM" ;
962
+ if (hour == 0 ) hour = 12 ;
963
+ if (hour > 12 ) hour -= 12 ;
964
+ return String .format ("%02d %s" , hour , amPm );
965
+ }
966
+ };
967
+ }
968
+ return mDateTimeInterpreter ;
969
+ }
970
+
971
+ /**
972
+ * Set the interpreter which provides the text to show in the header column and the header row.
973
+ * @param dateTimeInterpreter The date, time interpreter.
974
+ */
975
+ public void setDateTimeInterpreter (DateTimeInterpreter dateTimeInterpreter ){
976
+ this .mDateTimeInterpreter = dateTimeInterpreter ;
926
977
}
927
978
928
979
@@ -1123,16 +1174,26 @@ public void setDefaultEventColor(int defaultEventColor) {
1123
1174
invalidate ();
1124
1175
}
1125
1176
1177
+ /**
1178
+ * <b>Note:</b> Use {@link #setDateTimeInterpreter(DateTimeInterpreter)} and
1179
+ * {@link #getDateTimeInterpreter()} instead.
1180
+ * @return
1181
+ */
1182
+ @ Deprecated
1126
1183
public int getDayNameLength () {
1127
1184
return mDayNameLength ;
1128
1185
}
1129
1186
1130
1187
/**
1131
1188
* Set the length of the day name displayed in the header row. Example of short day names is
1132
1189
* 'M' for 'Monday' and example of long day names is 'Mon' for 'Monday'.
1190
+ * <p>
1191
+ * <b>Note:</b> Use {@link #setDateTimeInterpreter(DateTimeInterpreter)} instead.
1192
+ * </p>
1133
1193
* @param length Supported values are {@link com.alamkanak.weekview.WeekView#LENGTH_SHORT} and
1134
1194
* {@link com.alamkanak.weekview.WeekView#LENGTH_LONG}.
1135
1195
*/
1196
+ @ Deprecated
1136
1197
public void setDayNameLength (int length ) {
1137
1198
if (length != LENGTH_LONG && length != LENGTH_SHORT ) {
1138
1199
throw new IllegalArgumentException ("length parameter must be either LENGTH_LONG or LENGTH_SHORT" );
@@ -1357,22 +1418,6 @@ private boolean containsValue(int[] list, int value) {
1357
1418
return false ;
1358
1419
}
1359
1420
1360
-
1361
- /**
1362
- * Converts an int (0-23) to time string (e.g. 12 PM).
1363
- * @param hour The time. Limit: 0-23.
1364
- * @return The string representation of the time.
1365
- */
1366
- private String getTimeString (int hour ) {
1367
- String amPm ;
1368
- if (hour >= 0 && hour < 12 ) amPm = "AM" ;
1369
- else amPm = "PM" ;
1370
- if (hour == 0 ) hour = 12 ;
1371
- if (hour > 12 ) hour -= 12 ;
1372
- return String .format ("%02d %s" , hour , amPm );
1373
- }
1374
-
1375
-
1376
1421
/**
1377
1422
* Checks if two times are on the same day.
1378
1423
* @param dayOne The first day.
@@ -1383,21 +1428,4 @@ private boolean isSameDay(Calendar dayOne, Calendar dayTwo) {
1383
1428
return dayOne .get (Calendar .YEAR ) == dayTwo .get (Calendar .YEAR ) && dayOne .get (Calendar .DAY_OF_YEAR ) == dayTwo .get (Calendar .DAY_OF_YEAR );
1384
1429
}
1385
1430
1386
-
1387
- /**
1388
- * Get the day name of a given date.
1389
- * @param date The date.
1390
- * @return The first the characters of the day name.
1391
- */
1392
- private String getDayName (Calendar date ) {
1393
- int dayOfWeek = date .get (Calendar .DAY_OF_WEEK );
1394
- if (Calendar .MONDAY == dayOfWeek ) return (mDayNameLength == LENGTH_SHORT ? "M" : "MON" );
1395
- else if (Calendar .TUESDAY == dayOfWeek ) return (mDayNameLength == LENGTH_SHORT ? "T" : "TUE" );
1396
- else if (Calendar .WEDNESDAY == dayOfWeek ) return (mDayNameLength == LENGTH_SHORT ? "W" : "WED" );
1397
- else if (Calendar .THURSDAY == dayOfWeek ) return (mDayNameLength == LENGTH_SHORT ? "T" : "THU" );
1398
- else if (Calendar .FRIDAY == dayOfWeek ) return (mDayNameLength == LENGTH_SHORT ? "F" : "FRI" );
1399
- else if (Calendar .SATURDAY == dayOfWeek ) return (mDayNameLength == LENGTH_SHORT ? "S" : "SAT" );
1400
- else if (Calendar .SUNDAY == dayOfWeek ) return (mDayNameLength == LENGTH_SHORT ? "S" : "SUN" );
1401
- return "" ;
1402
- }
1403
1431
}
0 commit comments