forked from upstream-mirrors/OpenTracks
@@ -7,6 +7,8 @@ import androidx.annotation.Nullable;
|
||||
import java.time.Duration;
|
||||
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.content.data.Distance;
|
||||
import de.dennisguse.opentracks.content.data.Speed;
|
||||
import de.dennisguse.opentracks.stats.TrackStatistics;
|
||||
import de.dennisguse.opentracks.viewmodels.IntervalStatistics;
|
||||
|
||||
@@ -16,17 +18,15 @@ public class AnnouncementUtils {
|
||||
}
|
||||
|
||||
public static String getAnnouncement(Context context, TrackStatistics trackStatistics, boolean isMetricUnits, boolean isReportSpeed, @Nullable IntervalStatistics.Interval currentInterval) {
|
||||
double distance = trackStatistics.getTotalDistance() * UnitConversions.M_TO_KM;
|
||||
double distancePerTime = trackStatistics.getAverageMovingSpeed() * UnitConversions.MPS_TO_KMH;
|
||||
double currentDistancePerTime = currentInterval != null ? currentInterval.getSpeed_ms() * UnitConversions.MPS_TO_KMH : 0;
|
||||
Distance distance = trackStatistics.getTotalDistance();
|
||||
Speed distancePerTime = trackStatistics.getAverageMovingSpeed();
|
||||
double currentDistancePerTime = currentInterval != null ? currentInterval.getSpeed_ms() * UnitConversions.MPS_TO_KMH : 0; //TODO Use Speed?
|
||||
|
||||
if (distance == 0) {
|
||||
if (distance.isZero()) {
|
||||
return context.getString(R.string.voice_total_distance_zero);
|
||||
}
|
||||
|
||||
if (!isMetricUnits) {
|
||||
distance *= UnitConversions.KM_TO_MI;
|
||||
distancePerTime *= UnitConversions.KM_TO_MI;
|
||||
currentDistancePerTime *= UnitConversions.KM_TO_MI;
|
||||
}
|
||||
|
||||
@@ -35,24 +35,27 @@ public class AnnouncementUtils {
|
||||
String currentRateMsg;
|
||||
if (isReportSpeed) {
|
||||
int speedId = isMetricUnits ? R.plurals.voiceSpeedKilometersPerHour : R.plurals.voiceSpeedMilesPerHour;
|
||||
rate = context.getResources().getQuantityString(speedId, getQuantityCount(distancePerTime), distancePerTime);
|
||||
double distanceInUnit = distancePerTime.to(isMetricUnits);
|
||||
rate = context.getResources().getQuantityString(speedId, getQuantityCount(distanceInUnit), distanceInUnit);
|
||||
|
||||
currentRate = context.getResources().getQuantityString(speedId, getQuantityCount(currentDistancePerTime), currentDistancePerTime);
|
||||
currentRateMsg = context.getString(R.string.voice_speed_lap, currentRate);
|
||||
} else {
|
||||
double timePerDistance = distancePerTime == 0 ? 0.0 : 1 / distancePerTime;
|
||||
double timePerDistance = distancePerTime.isZero() ? 0.0 : 1 / distancePerTime.to(isMetricUnits); //TODO PACE
|
||||
|
||||
int paceId = isMetricUnits ? R.string.voice_pace_per_kilometer : R.string.voice_pace_per_mile;
|
||||
Duration time = Duration.ofMillis((long) (timePerDistance * UnitConversions.HR_TO_MIN * UnitConversions.MIN_TO_S * UnitConversions.S_TO_MS));
|
||||
rate = context.getString(paceId, getAnnounceTime(context, time));
|
||||
|
||||
double currentTimePerDistance = currentDistancePerTime == 0 ? 0.0 : 1 / currentDistancePerTime;
|
||||
double currentTimePerDistance = currentDistancePerTime == 0 ? 0.0 : 1 / currentDistancePerTime; //TODO PACE
|
||||
Duration currentTime = Duration.ofMillis((long) (currentTimePerDistance * UnitConversions.HR_TO_MIN * UnitConversions.MIN_TO_S * UnitConversions.S_TO_MS));
|
||||
currentRate = context.getString(paceId, getAnnounceTime(context, currentTime));
|
||||
currentRateMsg = context.getString(R.string.voice_pace_lap, currentRate);
|
||||
}
|
||||
|
||||
int totalDistanceId = isMetricUnits ? R.plurals.voiceTotalDistanceKilometers : R.plurals.voiceTotalDistanceMiles;
|
||||
String totalDistance = context.getResources().getQuantityString(totalDistanceId, getQuantityCount(distance), distance);
|
||||
double distanceInUnit = distance.to(isMetricUnits);
|
||||
String totalDistance = context.getResources().getQuantityString(totalDistanceId, getQuantityCount(distanceInUnit), distanceInUnit);
|
||||
|
||||
currentRateMsg = currentInterval == null ? "" : " " + currentRateMsg;
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import androidx.documentfile.provider.DocumentFile;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.content.data.Distance;
|
||||
import de.dennisguse.opentracks.io.file.TrackFileFormat;
|
||||
|
||||
/**
|
||||
@@ -180,17 +181,21 @@ public class PreferencesUtils {
|
||||
return getInt(sharedPreferences, context, R.string.voice_frequency_key, VOICE_FREQUENCY_DEFAULT);
|
||||
}
|
||||
|
||||
public static int getRecordingDistanceInterval(SharedPreferences sharedPreferences, Context context) {
|
||||
return getInt(sharedPreferences, context, R.string.recording_distance_interval_key, getRecordingDistanceIntervalDefault(context));
|
||||
public static Distance getRecordingDistanceInterval(SharedPreferences sharedPreferences, Context context) {
|
||||
return Distance.of(getInt(sharedPreferences, context, R.string.recording_distance_interval_key, getRecordingDistanceIntervalDefaultInternal(context)));
|
||||
}
|
||||
|
||||
public static int getRecordingDistanceIntervalDefault(Context context) {
|
||||
public static Distance getRecordingDistanceIntervalDefault(Context context) {
|
||||
return Distance.of(getRecordingDistanceIntervalDefaultInternal(context));
|
||||
}
|
||||
|
||||
private static int getRecordingDistanceIntervalDefaultInternal(Context context) {
|
||||
return Integer.parseInt(context.getResources().getString(R.string.recording_distance_interval_default));
|
||||
}
|
||||
|
||||
public static int getMaxRecordingDistance(SharedPreferences sharedPreferences, Context context) {
|
||||
public static Distance getMaxRecordingDistance(SharedPreferences sharedPreferences, Context context) {
|
||||
final int MAX_RECORDING_DISTANCE = Integer.parseInt(context.getResources().getString(R.string.max_recording_distance_default));
|
||||
return getInt(sharedPreferences, context, R.string.max_recording_distance_key, MAX_RECORDING_DISTANCE);
|
||||
return Distance.of(getInt(sharedPreferences, context, R.string.max_recording_distance_key, MAX_RECORDING_DISTANCE));
|
||||
}
|
||||
|
||||
//TODO Duration
|
||||
|
||||
@@ -36,6 +36,8 @@ import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Locale;
|
||||
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.content.data.Distance;
|
||||
import de.dennisguse.opentracks.content.data.Speed;
|
||||
|
||||
/**
|
||||
* Various string manipulation methods.
|
||||
@@ -86,50 +88,38 @@ public class StringUtils {
|
||||
* Formats the distance in meters.
|
||||
*
|
||||
* @param context the context
|
||||
* @param distance_m the distance_m
|
||||
* @param distance the distance
|
||||
* @param metricUnits true to use metric units. False to use imperial units
|
||||
*/
|
||||
public static String formatDistance(Context context, double distance_m, boolean metricUnits) {
|
||||
if (Double.isNaN(distance_m) || Double.isInfinite(distance_m)) {
|
||||
public static String formatDistance(Context context, Distance distance, boolean metricUnits) {
|
||||
if (distance.isInvalid()) {
|
||||
return context.getString(R.string.value_unknown);
|
||||
}
|
||||
|
||||
if (metricUnits) {
|
||||
if (distance_m > 500.0) {
|
||||
distance_m *= UnitConversions.M_TO_KM;
|
||||
return context.getString(R.string.value_float_kilometer, distance_m);
|
||||
if (distance.greaterThan(Distance.of(500))) {
|
||||
return context.getString(R.string.value_float_kilometer, distance.toKM());
|
||||
} else {
|
||||
return context.getString(R.string.value_float_meter, distance_m);
|
||||
return context.getString(R.string.value_float_meter, distance.toM());
|
||||
}
|
||||
} else {
|
||||
if (distance_m * UnitConversions.M_TO_MI > 0.5) {
|
||||
distance_m *= UnitConversions.M_TO_MI;
|
||||
return context.getString(R.string.value_float_mile, distance_m);
|
||||
if (distance.greaterThan(Distance.ofMile(0.5))) {
|
||||
return context.getString(R.string.value_float_mile, distance.toMI());
|
||||
} else {
|
||||
distance_m *= UnitConversions.M_TO_FT;
|
||||
return context.getString(R.string.value_float_feet, distance_m);
|
||||
return context.getString(R.string.value_float_feet, distance.toFT());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String formatSpeed(Context context, double speed_mps, boolean metricUnits, boolean reportSpeed) {
|
||||
if (Double.isNaN(speed_mps) || Double.isInfinite(speed_mps)) {
|
||||
speed_mps = 0;
|
||||
}
|
||||
|
||||
double speed = speed_mps * UnitConversions.M_TO_KM; //KM p sec
|
||||
if (!metricUnits) {
|
||||
speed *= UnitConversions.KM_TO_MI;
|
||||
}
|
||||
|
||||
public static String formatSpeed(Context context, Speed speed, boolean metricUnits, boolean reportSpeed) {
|
||||
if (reportSpeed) {
|
||||
if (metricUnits) {
|
||||
return context.getString(R.string.value_float_kilometer_hour, speed * UnitConversions.S_TO_HR);
|
||||
return context.getString(R.string.value_float_kilometer_hour, speed.toKMH());
|
||||
} else {
|
||||
return context.getString(R.string.value_float_mile_hour, speed * UnitConversions.S_TO_HR);
|
||||
return context.getString(R.string.value_float_mile_hour, speed.toMPH());
|
||||
}
|
||||
} else {
|
||||
int pace = speed == 0 ? 0 : (int) Math.round(1 / speed); //sec / [KM | MI]
|
||||
int pace = (int) speed.toPace(metricUnits).getSeconds();
|
||||
int minutes = pace / 60;
|
||||
int seconds = pace % 60;
|
||||
if (metricUnits) {
|
||||
@@ -167,45 +157,40 @@ public class StringUtils {
|
||||
* Get the formatted distance with unit.
|
||||
*
|
||||
* @param context the context
|
||||
* @param distance_m the distance
|
||||
* @param distance the distance
|
||||
* @param metricUnits true to use metric unit
|
||||
* @return the formatted distance (or null) and it's unit as {@link Pair}
|
||||
*/
|
||||
public static Pair<String, String> getDistanceParts(Context context, double distance_m, boolean metricUnits) {
|
||||
if (Double.isNaN(distance_m) || Double.isInfinite(distance_m)) {
|
||||
public static Pair<String, String> getDistanceParts(Context context, Distance distance, boolean metricUnits) {
|
||||
if (distance.isInvalid()) {
|
||||
return new Pair<>(null, context.getString(metricUnits ? R.string.unit_meter : R.string.unit_feet));
|
||||
}
|
||||
|
||||
int unitId;
|
||||
if (metricUnits) {
|
||||
if (distance_m > 500.0) {
|
||||
distance_m *= UnitConversions.M_TO_KM;
|
||||
unitId = R.string.unit_kilometer;
|
||||
if (distance.greaterThan(Distance.of(500))) {
|
||||
return new Pair<>(formatDecimal(distance.toKM()), context.getString(R.string.unit_kilometer));
|
||||
} else {
|
||||
unitId = R.string.unit_meter;
|
||||
return new Pair<>(formatDecimal(distance.toM()), context.getString(R.string.unit_meter));
|
||||
}
|
||||
} else {
|
||||
if (distance_m * UnitConversions.M_TO_MI > 0.5) {
|
||||
distance_m *= UnitConversions.M_TO_MI;
|
||||
unitId = R.string.unit_mile;
|
||||
if (distance.greaterThan(Distance.of(0.5 * UnitConversions.M_TO_MI))) {
|
||||
return new Pair<>(formatDecimal(distance.toMI()), context.getString(R.string.unit_mile));
|
||||
} else {
|
||||
distance_m *= UnitConversions.M_TO_FT;
|
||||
unitId = R.string.unit_feet;
|
||||
return new Pair<>(formatDecimal(distance.toFT()), context.getString(R.string.unit_feet));
|
||||
}
|
||||
}
|
||||
return new Pair<>(formatDecimal(distance_m), context.getString(unitId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the formatted speed with unit.
|
||||
*
|
||||
* @param context the context
|
||||
* @param speed_mps the speed
|
||||
* @param speed the speed
|
||||
* @param metricUnits true to use metric unit
|
||||
* @param reportSpeed true to report speed; false for pace
|
||||
* @return the formatted speed (or null) and it's unit as {@link Pair}
|
||||
*/
|
||||
public static Pair<String, String> getSpeedParts(Context context, double speed_mps, boolean metricUnits, boolean reportSpeed) {
|
||||
public static Pair<String, String> getSpeedParts(Context context, Speed speed, boolean metricUnits, boolean reportSpeed) {
|
||||
int unitId;
|
||||
if (metricUnits) {
|
||||
unitId = reportSpeed ? R.string.unit_kilometer_per_hour : R.string.unit_minute_per_kilometer;
|
||||
@@ -214,21 +199,15 @@ public class StringUtils {
|
||||
}
|
||||
String unitString = context.getString(unitId);
|
||||
|
||||
|
||||
if (Double.isNaN(speed_mps) || Double.isInfinite(speed_mps)) {
|
||||
speed_mps = 0;
|
||||
}
|
||||
|
||||
double speed = speed_mps * UnitConversions.M_TO_KM; //KM p sec
|
||||
if (!metricUnits) {
|
||||
speed *= UnitConversions.KM_TO_MI;
|
||||
if (speed == null) {
|
||||
speed = Speed.zero();
|
||||
}
|
||||
|
||||
if (reportSpeed) {
|
||||
return new Pair<>(StringUtils.formatDecimal(speed * UnitConversions.S_TO_HR, 1), unitString);
|
||||
return new Pair<>(StringUtils.formatDecimal(speed.to(metricUnits), 1), unitString);
|
||||
}
|
||||
|
||||
int pace = speed == 0 ? 0 : (int) Math.round(1 / speed); //sec / [KM | MI]
|
||||
int pace = (int) speed.toPace(metricUnits).getSeconds();
|
||||
|
||||
int minutes = pace / 60;
|
||||
int seconds = pace % 60;
|
||||
|
||||
@@ -49,7 +49,7 @@ public class UnitConversions {
|
||||
|
||||
// Distance
|
||||
// multiplication factor to convert miles to feet
|
||||
public static final double MI_TO_FT = 5280.0;
|
||||
private static final double MI_TO_FT = 5280.0;
|
||||
// multiplication factor to convert feet to miles
|
||||
public static final double FT_TO_MI = 1 / MI_TO_FT;
|
||||
// multiplication factor to covert kilometers to meters
|
||||
|
||||
Reference in New Issue
Block a user