mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-10 13:13:05 +02:00
Voice announcements: cleanup.
This commit is contained in:
@@ -26,7 +26,9 @@ public class AnnouncementUtilsTest {
|
|||||||
private final Context context = ApplicationProvider.getApplicationContext();
|
private final Context context = ApplicationProvider.getApplicationContext();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAnnouncement() {
|
public void getAnnouncement_metric() {
|
||||||
|
PreferencesUtils.setString(context, R.string.stats_units_key, context.getString(R.string.stats_units_metric));
|
||||||
|
|
||||||
TrackStatistics stats = new TrackStatistics();
|
TrackStatistics stats = new TrackStatistics();
|
||||||
stats.setTotalDistance(20000);
|
stats.setTotalDistance(20000);
|
||||||
stats.setTotalTime(Duration.ofMillis(600000));
|
stats.setTotalTime(Duration.ofMillis(600000));
|
||||||
@@ -42,7 +44,9 @@ public class AnnouncementUtilsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAnnouncement_withInterval() {
|
public void getAnnouncement_withInterval_metric() {
|
||||||
|
PreferencesUtils.setString(context, R.string.stats_units_key, context.getString(R.string.stats_units_metric));
|
||||||
|
|
||||||
TrackStatistics stats = new TrackStatistics();
|
TrackStatistics stats = new TrackStatistics();
|
||||||
stats.setTotalDistance(20000);
|
stats.setTotalDistance(20000);
|
||||||
stats.setTotalTime(Duration.ofMillis(600000));
|
stats.setTotalTime(Duration.ofMillis(600000));
|
||||||
@@ -54,30 +58,12 @@ public class AnnouncementUtilsTest {
|
|||||||
IntervalStatistics intervalStatistics = new IntervalStatistics(trackPoints, 1000);
|
IntervalStatistics intervalStatistics = new IntervalStatistics(trackPoints, 1000);
|
||||||
IntervalStatistics.Interval lastInterval = intervalStatistics.getIntervalList().get(intervalStatistics.getIntervalList().size() - 1);
|
IntervalStatistics.Interval lastInterval = intervalStatistics.getIntervalList().get(intervalStatistics.getIntervalList().size() - 1);
|
||||||
|
|
||||||
int speedId = R.plurals.voiceSpeedKilometersPerHour;
|
String expected = "OpenTracks total distance 20.00 kilometers in 5 minutes 0 seconds at 240.0 kilometers per hour Lap speed of 51.2 kilometers per hour";
|
||||||
double kmPerHour = lastInterval.getSpeed_ms() * UnitConversions.MPS_TO_KMH;
|
|
||||||
|
|
||||||
String firstPartMsg = "OpenTracks total distance 20.00 kilometers in 5 minutes 0 seconds at 240.0 kilometers per hour";
|
|
||||||
String rateMsg = " Lap speed of " + context.getResources().getQuantityString(speedId, getQuantityCount(kmPerHour), kmPerHour);
|
|
||||||
String msg = firstPartMsg + rateMsg;
|
|
||||||
|
|
||||||
// when
|
// when
|
||||||
String announcement = AnnouncementUtils.getAnnouncement(context, stats, "airplane", lastInterval);
|
String announcement = AnnouncementUtils.getAnnouncement(context, stats, "airplane", lastInterval);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertEquals(msg, announcement);
|
assertEquals(expected, announcement);
|
||||||
}
|
|
||||||
|
|
||||||
private int getQuantityCount(double d) {
|
|
||||||
if (d == 0) {
|
|
||||||
return 0;
|
|
||||||
} else if (d == 1) {
|
|
||||||
return 1;
|
|
||||||
} else if (d == 2) {
|
|
||||||
return 2;
|
|
||||||
} else {
|
|
||||||
int count = (int) d;
|
|
||||||
return Math.max(count, 3);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,29 +82,8 @@ public class AnnouncementUtils {
|
|||||||
return result + minutesText + " " + secondsText;
|
return result + minutesText + " " + secondsText;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static int getQuantityCount(double d) {
|
||||||
* Gets the plural count to be used by getQuantityString.
|
return (int) d;
|
||||||
* getQuantityString only supports integer quantities, not a double quantity like "2.2".
|
|
||||||
* <p>
|
|
||||||
* As a temporary workaround, we convert a double quantity to an integer quantity.
|
|
||||||
* If the double quantity is exactly 0, 1, or 2, then we can return these integer quantities.
|
|
||||||
* Otherwise, we cast the double quantity to an integer quantity.
|
|
||||||
* However, we need to make sure that if the casted value is 0, 1, or 2, we don't return those, instead, return the next biggest integer 3.
|
|
||||||
*
|
|
||||||
* @param d the double value
|
|
||||||
*/
|
|
||||||
private static int getQuantityCount(double d) {
|
|
||||||
if (d == 0) {
|
|
||||||
return 0;
|
|
||||||
} else if (d == 1) {
|
|
||||||
return 1;
|
|
||||||
} else if (d == 2) {
|
|
||||||
return 2;
|
|
||||||
} else {
|
|
||||||
//TODO This seems weird; why not use Math.round(d) or Math.ceil()?
|
|
||||||
int count = (int) d;
|
|
||||||
return Math.max(count, 3);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user