Add support for singular/plural.

This commit is contained in:
Jimmy Shih
2011-11-17 14:31:37 -08:00
parent 0ea4f6f13a
commit f6983bba4b
15 changed files with 329 additions and 268 deletions
@@ -128,8 +128,10 @@ public class ImportAllTracks {
if (gpxFileCount == 0) {
builder.setMessage(activity.getString(R.string.import_no_file, gpxPath));
} else {
String totalFiles = activity.getResources().getQuantityString(
R.plurals.importGpxFiles, gpxFileCount, gpxFileCount);
builder.setMessage(
activity.getString(R.string.import_success, importSuccessCount, gpxFileCount, gpxPath));
activity.getString(R.string.import_success, importSuccessCount, totalFiles, gpxPath));
}
builder.setPositiveButton(R.string.generic_ok, new DialogInterface.OnClickListener() {
@Override
@@ -150,66 +150,72 @@ public class StatusAnnouncerTask implements PeriodicTask {
*/
// @VisibleForTesting
protected String getAnnouncement(TripStatistics stats) {
SharedPreferences preferences =
context.getSharedPreferences(Constants.SETTINGS_NAME, 0);
boolean metricUnits = true;
boolean reportSpeed = true;
SharedPreferences preferences = context.getSharedPreferences(Constants.SETTINGS_NAME, 0);
if (preferences != null) {
metricUnits =
preferences.getBoolean(context.getString(R.string.metric_units_key),
true);
reportSpeed =
preferences.getBoolean(context.getString(R.string.report_speed_key),
true);
metricUnits = preferences.getBoolean(context.getString(R.string.metric_units_key), true);
reportSpeed = preferences.getBoolean(context.getString(R.string.report_speed_key), true);
}
double d = stats.getTotalDistance() / 1000;
double s = stats.getAverageMovingSpeed() * 3.6;
double d = stats.getTotalDistance() / 1000; // d is in kilometers
double s = stats.getAverageMovingSpeed() * 3.6; // s is in kilometers per hour
if (d == 0) {
return context.getString(R.string.voice_no_distance);
return context.getString(R.string.voice_total_distance_zero);
}
int speedLabel;
if (metricUnits) {
if (reportSpeed) {
speedLabel = R.string.voice_kilometer_per_hour;
} else {
speedLabel = R.string.voice_per_kilometer;
}
} else {
s *= UnitConversions.KMH_TO_MPH;
if (!metricUnits) {
d *= UnitConversions.KM_TO_MI;
if (reportSpeed) {
speedLabel = R.string.voice_mile_per_hour;
} else {
speedLabel = R.string.voice_per_mile;
}
s *= UnitConversions.KMH_TO_MPH;
}
String speed = null;
if ((s == 0) || Double.isNaN(s)) {
speed = context.getString(R.string.value_unknown);
if (!reportSpeed) {
s = 3600000.0 / s; // converts from speed to pace
}
// Makes sure s is not NaN.
if (Double.isNaN(s)) {
s = 0;
}
String speed;
if (reportSpeed) {
int speedId = metricUnits ? R.plurals.voiceSpeedKilometersPerHour
: R.plurals.voiceSpeedMilesPerHour;
speed = context.getResources().getQuantityString(speedId, getQuantityCount(s), s);
} else {
if (reportSpeed) {
speed = String.format("%.1f", s);
} else {
double pace = 3600000.0 / s;
Log.w(Constants.TAG,
"Converted speed: " + s + " to pace: " + pace);
speed = getAnnounceTime((long) pace);
}
int perDistancelId = metricUnits ? R.string.voice_per_kilometer : R.string.voice_per_mile;
speed = getAnnounceTime((long) s) + " " + context.getString(perDistancelId);
}
return context.getString(R.string.voice_template,
d,
context.getString(metricUnits
? R.string.voice_kilometers
: R.string.voice_miles),
getAnnounceTime(stats.getMovingTime()),
speed,
context.getString(speedLabel));
}
int totalDistanceId = metricUnits ? R.plurals.voiceTotalDistanceKilometers
: R.plurals.voiceTotalDistanceMiles;
String totalDistance = context.getResources().getQuantityString(
totalDistanceId, getQuantityCount(d), d);
return context.getString(
R.string.voice_template, totalDistance, getAnnounceTime(stats.getMovingTime()), speed);
}
/**
* Gets the plural count to be used by getQuantityString.
*
* @param d the double value
*/
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 count < 3 ? 3 : count;
}
}
@Override
public void start() {
Log.i(Constants.TAG, "Starting TTS");
@@ -331,30 +337,22 @@ public class StatusAnnouncerTask implements PeriodicTask {
@VisibleForTesting
String getAnnounceTime(long time) {
int[] parts = StringUtils.getTimeParts(time);
String secLabel =
context.getString(parts[0] == 1 ? R.string.voice_second : R.string.voice_seconds);
String minLabel =
context.getString(parts[1] == 1 ? R.string.voice_minute : R.string.voice_minutes);
String hourLabel =
context.getString(parts[2] == 1 ? R.string.voice_hour : R.string.voice_hours);
String seconds = context.getResources().getQuantityString(
R.plurals.voiceSeconds, parts[0], parts[0]);
String minutes = context.getResources().getQuantityString(
R.plurals.voiceMinutes, parts[1], parts[1]);
String hours = context.getResources().getQuantityString(
R.plurals.voiceHours, parts[2], parts[2]);
StringBuilder sb = new StringBuilder();
if (parts[2] != 0) {
sb.append(parts[2]);
sb.append(hours);
sb.append(" ");
sb.append(hourLabel);
sb.append(" ");
sb.append(parts[1]);
sb.append(" ");
sb.append(minLabel);
sb.append(minutes);
} else {
sb.append(parts[1]);
sb.append(minutes);
sb.append(" ");
sb.append(minLabel);
sb.append(" ");
sb.append(parts[0]);
sb.append(" ");
sb.append(secLabel);
sb.append(seconds);
}
return sb.toString();
}