1
1
package com .mapbox .services .android .navigation .v5 .utils .time ;
2
2
3
+ import android .content .Context ;
4
+ import android .content .res .Resources ;
3
5
import android .graphics .Typeface ;
4
6
import android .text .SpannableStringBuilder ;
5
7
import android .text .style .RelativeSizeSpan ;
6
8
import android .text .style .StyleSpan ;
7
9
10
+ import com .mapbox .services .android .navigation .R ;
8
11
import com .mapbox .services .android .navigation .v5 .navigation .NavigationTimeFormat ;
9
12
import com .mapbox .services .android .navigation .v5 .utils .span .SpanItem ;
10
13
import com .mapbox .services .android .navigation .v5 .utils .span .SpanUtils ;
11
14
import com .mapbox .services .android .navigation .v5 .utils .span .TextSpanItem ;
12
15
13
16
import java .util .ArrayList ;
14
17
import java .util .Calendar ;
15
- import java .util .Date ;
16
18
import java .util .List ;
17
19
import java .util .Locale ;
18
20
import java .util .concurrent .TimeUnit ;
19
21
20
- public class TimeUtils {
22
+ public class TimeFormatter {
21
23
22
24
private static final String ARRIVAL_TIME_STRING_FORMAT = "%tl:%tM %tp" ;
23
- private static final String DAY = " day " ;
24
- private static final String DAYS = " days " ;
25
- private static final String HOUR = " hr " ;
26
- private static final String MINUTE = " min " ;
25
+ private static final String TIME_STRING_FORMAT = " %s " ;
27
26
28
27
public static String formatArrivalTime (double routeDuration ) {
29
28
Calendar calendar = Calendar .getInstance ();
@@ -37,11 +36,10 @@ public static String formatTime(Calendar time, double routeDuration, @Navigation
37
36
boolean isDeviceTwentyFourHourFormat ) {
38
37
time .add (Calendar .SECOND , (int ) routeDuration );
39
38
TimeFormattingChain chain = new TimeFormattingChain ();
40
- String formattedTime = chain .setup (isDeviceTwentyFourHourFormat ).obtainTimeFormatted (type , time );
41
- return formattedTime ;
39
+ return chain .setup (isDeviceTwentyFourHourFormat ).obtainTimeFormatted (type , time );
42
40
}
43
41
44
- public static SpannableStringBuilder formatTimeRemaining (double routeDuration ) {
42
+ public static SpannableStringBuilder formatTimeRemaining (Context context , double routeDuration ) {
45
43
long seconds = (long ) routeDuration ;
46
44
47
45
if (seconds < 0 ) {
@@ -60,30 +58,45 @@ public static SpannableStringBuilder formatTimeRemaining(double routeDuration) {
60
58
}
61
59
62
60
List <SpanItem > textSpanItems = new ArrayList <>();
61
+ Resources resources = context .getResources ();
62
+ formatDays (resources , days , textSpanItems );
63
+ formatHours (context , hours , textSpanItems );
64
+ formatMinutes (context , minutes , textSpanItems );
65
+ formatNoData (context , days , hours , minutes , textSpanItems );
66
+ return SpanUtils .combineSpans (textSpanItems );
67
+ }
68
+
69
+ private static void formatDays (Resources resources , long days , List <SpanItem > textSpanItems ) {
63
70
if (days != 0 ) {
64
- String dayFormat = days > 1 ? DAYS : DAY ;
71
+ String dayQuantityString = resources .getQuantityString (R .plurals .numberOfDays , (int ) days );
72
+ String dayString = String .format (TIME_STRING_FORMAT , dayQuantityString );
65
73
textSpanItems .add (new TextSpanItem (new StyleSpan (Typeface .BOLD ), String .valueOf (days )));
66
- textSpanItems .add (new TextSpanItem (new RelativeSizeSpan (1f ), dayFormat ));
74
+ textSpanItems .add (new TextSpanItem (new RelativeSizeSpan (1f ), dayString ));
67
75
}
76
+ }
77
+
78
+ private static void formatHours (Context context , long hours , List <SpanItem > textSpanItems ) {
68
79
if (hours != 0 ) {
80
+ String hourString = String .format (TIME_STRING_FORMAT , context .getString (R .string .hr ));
69
81
textSpanItems .add (new TextSpanItem (new StyleSpan (Typeface .BOLD ), String .valueOf (hours )));
70
- textSpanItems .add (new TextSpanItem (new RelativeSizeSpan (1f ), HOUR ));
82
+ textSpanItems .add (new TextSpanItem (new RelativeSizeSpan (1f ), hourString ));
71
83
}
84
+ }
85
+
86
+ private static void formatMinutes (Context context , long minutes , List <SpanItem > textSpanItems ) {
72
87
if (minutes != 0 ) {
88
+ String minuteString = String .format (TIME_STRING_FORMAT , context .getString (R .string .min ));
73
89
textSpanItems .add (new TextSpanItem (new StyleSpan (Typeface .BOLD ), String .valueOf (minutes )));
74
- textSpanItems .add (new TextSpanItem (new RelativeSizeSpan (1f ), MINUTE ));
90
+ textSpanItems .add (new TextSpanItem (new RelativeSizeSpan (1f ), minuteString ));
75
91
}
92
+ }
93
+
94
+ private static void formatNoData (Context context , long days , long hours , long minutes ,
95
+ List <SpanItem > textSpanItems ) {
76
96
if (days == 0 && hours == 0 && minutes == 0 ) {
97
+ String minuteString = String .format (TIME_STRING_FORMAT , context .getString (R .string .min ));
77
98
textSpanItems .add (new TextSpanItem (new StyleSpan (Typeface .BOLD ), String .valueOf (1 )));
78
- textSpanItems .add (new TextSpanItem (new RelativeSizeSpan (1f ), MINUTE ));
99
+ textSpanItems .add (new TextSpanItem (new RelativeSizeSpan (1f ), minuteString ));
79
100
}
80
-
81
- return SpanUtils .combineSpans (textSpanItems );
82
101
}
83
-
84
- public static long dateDiff (Date date1 , Date date2 , TimeUnit timeUnit ) {
85
- long diffInMillies = date2 .getTime () - date1 .getTime ();
86
- return timeUnit .convert (diffInMillies , TimeUnit .MILLISECONDS );
87
- }
88
-
89
102
}
0 commit comments