forked from upstream-mirrors/OpenTracks
Cleanup: Use Pair instead of String[2].
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
package de.dennisguse.opentracks.content;
|
package de.dennisguse.opentracks.content;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.util.Pair;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
@@ -95,8 +96,7 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
|||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
// Total distance
|
// Total distance
|
||||||
writeDistance(
|
writeDistance(stats.getTotalDistance(), builder, R.string.description_total_distance, lineBreak);
|
||||||
stats.getTotalDistance(), builder, R.string.description_total_distance, lineBreak);
|
|
||||||
|
|
||||||
// Total time
|
// Total time
|
||||||
writeTime(stats.getTotalTime(), builder, R.string.description_total_time, lineBreak);
|
writeTime(stats.getTotalTime(), builder, R.string.description_total_time, lineBreak);
|
||||||
@@ -108,8 +108,7 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
|||||||
writeSpeed(stats.getAverageSpeed(), builder, R.string.description_average_speed, lineBreak);
|
writeSpeed(stats.getAverageSpeed(), builder, R.string.description_average_speed, lineBreak);
|
||||||
|
|
||||||
// Average moving speed
|
// Average moving speed
|
||||||
writeSpeed(stats.getAverageMovingSpeed(), builder, R.string.description_average_moving_speed,
|
writeSpeed(stats.getAverageMovingSpeed(), builder, R.string.description_average_moving_speed, lineBreak);
|
||||||
lineBreak);
|
|
||||||
|
|
||||||
// Max speed
|
// Max speed
|
||||||
writeSpeed(stats.getMaxSpeed(), builder, R.string.description_max_speed, lineBreak);
|
writeSpeed(stats.getMaxSpeed(), builder, R.string.description_max_speed, lineBreak);
|
||||||
@@ -118,8 +117,7 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
|||||||
writePace(stats.getAverageSpeed(), builder, R.string.description_average_pace_in_minute, lineBreak);
|
writePace(stats.getAverageSpeed(), builder, R.string.description_average_pace_in_minute, lineBreak);
|
||||||
|
|
||||||
// Average moving pace
|
// Average moving pace
|
||||||
writePace(stats.getAverageMovingSpeed(), builder,
|
writePace(stats.getAverageMovingSpeed(), builder, R.string.description_average_moving_pace_in_minute, lineBreak);
|
||||||
R.string.description_average_moving_pace_in_minute, lineBreak);
|
|
||||||
|
|
||||||
// Fastest pace
|
// Fastest pace
|
||||||
writePace(stats.getMaxSpeed(), builder, R.string.description_fastest_pace_in_minute, lineBreak);
|
writePace(stats.getMaxSpeed(), builder, R.string.description_fastest_pace_in_minute, lineBreak);
|
||||||
@@ -187,8 +185,7 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
|||||||
* @param lineBreak line break string
|
* @param lineBreak line break string
|
||||||
*/
|
*/
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void writeSpeed(
|
void writeSpeed(double speed, StringBuilder builder, int resId, String lineBreak) {
|
||||||
double speed, StringBuilder builder, int resId, String lineBreak) {
|
|
||||||
double speedInKmHr = speed * UnitConversions.MS_TO_KMH;
|
double speedInKmHr = speed * UnitConversions.MS_TO_KMH;
|
||||||
double speedInMiHr = speedInKmHr * UnitConversions.KM_TO_MI;
|
double speedInMiHr = speedInKmHr * UnitConversions.KM_TO_MI;
|
||||||
builder.append(context.getString(resId, speedInKmHr, speedInMiHr));
|
builder.append(context.getString(resId, speedInKmHr, speedInMiHr));
|
||||||
@@ -205,18 +202,13 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
|||||||
*/
|
*/
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void writePace(double speed, StringBuilder builder, int resId, String lineBreak) {
|
void writePace(double speed, StringBuilder builder, int resId, String lineBreak) {
|
||||||
String[] paceInMetrics = StringUtils.getSpeedParts(context, speed, true, false);
|
Pair<String, String> paceInMetrics = StringUtils.getSpeedParts(context, speed, true, false);
|
||||||
String[] paceInImperial = StringUtils.getSpeedParts(context, speed, false, false);
|
Pair<String, String> paceInImperial = StringUtils.getSpeedParts(context, speed, false, false);
|
||||||
|
|
||||||
if (paceInMetrics[0] == null) {
|
String formattedPaceMetrics = paceInMetrics.first != null ? paceInMetrics.first : context.getString(R.string.value_unknown);
|
||||||
paceInMetrics[0] = context.getString(R.string.value_unknown);
|
String formattedPaceImperial = paceInImperial.first != null ? paceInImperial.first : context.getString(R.string.value_unknown);
|
||||||
}
|
|
||||||
|
|
||||||
if (paceInImperial[0] == null) {
|
builder.append(context.getString(resId, formattedPaceMetrics, formattedPaceImperial));
|
||||||
paceInImperial[0] = context.getString(R.string.value_unknown);
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.append(context.getString(resId, paceInMetrics[0], paceInImperial[0]));
|
|
||||||
builder.append(lineBreak);
|
builder.append(lineBreak);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,8 +221,7 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
|||||||
* @param lineBreak line break string
|
* @param lineBreak line break string
|
||||||
*/
|
*/
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void writeElevation(
|
void writeElevation(double elevation, StringBuilder builder, int resId, String lineBreak) {
|
||||||
double elevation, StringBuilder builder, int resId, String lineBreak) {
|
|
||||||
long elevationInM = Math.round(elevation);
|
long elevationInM = Math.round(elevation);
|
||||||
long elevationInFt = Math.round(elevation * UnitConversions.M_TO_FT);
|
long elevationInFt = Math.round(elevation * UnitConversions.M_TO_FT);
|
||||||
builder.append(context.getString(resId, elevationInM, elevationInFt));
|
builder.append(context.getString(resId, elevationInM, elevationInFt));
|
||||||
@@ -247,8 +238,7 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
|||||||
*/
|
*/
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void writeGrade(double grade, StringBuilder builder, int resId, String lineBreak) {
|
void writeGrade(double grade, StringBuilder builder, int resId, String lineBreak) {
|
||||||
long gradeInPercent = Double.isNaN(grade) || Double.isInfinite(grade) ? 0L
|
long gradeInPercent = Double.isNaN(grade) || Double.isInfinite(grade) ? 0L : Math.round(grade * 100);
|
||||||
: Math.round(grade * 100);
|
|
||||||
builder.append(context.getString(resId, gradeInPercent));
|
builder.append(context.getString(resId, gradeInPercent));
|
||||||
builder.append(lineBreak);
|
builder.append(lineBreak);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ package de.dennisguse.opentracks.util;
|
|||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
|
import android.util.Pair;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Spinner;
|
import android.widget.Spinner;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
@@ -191,8 +192,8 @@ public class StatsUtils {
|
|||||||
* @param reportSpeed true if report speed
|
* @param reportSpeed true if report speed
|
||||||
*/
|
*/
|
||||||
private static void setSpeed(Context context, View view, int speedLabelId, int paceLabelId, double speed, boolean metricUnits, boolean reportSpeed) {
|
private static void setSpeed(Context context, View view, int speedLabelId, int paceLabelId, double speed, boolean metricUnits, boolean reportSpeed) {
|
||||||
String[] parts = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed);
|
Pair<String, String> parts = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed);
|
||||||
setItem(context, view, reportSpeed ? speedLabelId : paceLabelId, parts[0], parts[1]);
|
setItem(context, view, reportSpeed ? speedLabelId : paceLabelId, parts.first, parts.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -204,8 +205,8 @@ public class StatsUtils {
|
|||||||
* @param metricUnits true if metric units
|
* @param metricUnits true if metric units
|
||||||
*/
|
*/
|
||||||
private static void setDistanceValue(Context context, View view, double distance, boolean metricUnits) {
|
private static void setDistanceValue(Context context, View view, double distance, boolean metricUnits) {
|
||||||
String[] parts = StringUtils.getDistanceParts(context, distance, metricUnits);
|
Pair<String, String> parts = StringUtils.getDistanceParts(context, distance, metricUnits);
|
||||||
setItem(context, view, R.string.stats_distance, parts[0], parts[1]);
|
setItem(context, view, R.string.stats_distance, parts.first, parts.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import android.content.Context;
|
|||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.text.format.DateUtils;
|
import android.text.format.DateUtils;
|
||||||
|
import android.util.Pair;
|
||||||
|
|
||||||
import java.text.ParsePosition;
|
import java.text.ParsePosition;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
@@ -142,22 +143,16 @@ public class StringUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the distance in an array of two strings.
|
* Get the formatted distance with unit.
|
||||||
* The first string is the distance.
|
|
||||||
* The second string is the unit.
|
|
||||||
* The first string is null if the distance is invalid.
|
|
||||||
*
|
*
|
||||||
* @param context the context
|
* @param context the context
|
||||||
* @param distance the distance
|
* @param distance the distance
|
||||||
* @param metricUnits true to use metric unit
|
* @param metricUnits true to use metric unit
|
||||||
|
* @return the formatted distance (or null) and it's unit as {@link Pair}
|
||||||
*/
|
*/
|
||||||
//TODO Return Pair<Distance, Unit>
|
static Pair<String, String> getDistanceParts(Context context, double distance, boolean metricUnits) {
|
||||||
public static String[] getDistanceParts(Context context, double distance, boolean metricUnits) {
|
|
||||||
String[] result = new String[2];
|
|
||||||
if (Double.isNaN(distance) || Double.isInfinite(distance)) {
|
if (Double.isNaN(distance) || Double.isInfinite(distance)) {
|
||||||
result[0] = null;
|
return new Pair<>(null, context.getString(metricUnits ? R.string.unit_meter : R.string.unit_feet));
|
||||||
result[1] = context.getString(metricUnits ? R.string.unit_meter : R.string.unit_feet);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int unitId;
|
int unitId;
|
||||||
@@ -177,50 +172,45 @@ public class StringUtils {
|
|||||||
unitId = R.string.unit_feet;
|
unitId = R.string.unit_feet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result[0] = formatDecimal(distance);
|
return new Pair<>(formatDecimal(distance), context.getString(unitId));
|
||||||
result[1] = context.getString(unitId);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the speed in an array of two strings.
|
* Gets the formatted speed with unit.
|
||||||
* The first string is the speed.
|
|
||||||
* The second string is the unit.
|
|
||||||
* The first string is null if speed is invalid.
|
|
||||||
*
|
*
|
||||||
* @param context the context
|
* @param context the context
|
||||||
* @param speed the speed
|
* @param speed_mps the speed
|
||||||
* @param metricUnits true to use metric unit
|
* @param metricUnits true to use metric unit
|
||||||
* @param reportSpeed true to report speed
|
* @param reportSpeed true to report speed; false for pace
|
||||||
|
* @return the formatted speed (or null) and it's unit as {@link Pair}
|
||||||
*/
|
*/
|
||||||
//TODO Return Pair<Distance, Unit>
|
public static Pair<String, String> getSpeedParts(Context context, double speed_mps, boolean metricUnits, boolean reportSpeed) {
|
||||||
public static String[] getSpeedParts(Context context, double speed, boolean metricUnits, boolean reportSpeed) {
|
|
||||||
String[] result = new String[2];
|
|
||||||
int unitId;
|
int unitId;
|
||||||
if (metricUnits) {
|
if (metricUnits) {
|
||||||
unitId = reportSpeed ? R.string.unit_kilometer_per_hour : R.string.unit_minute_per_kilometer;
|
unitId = reportSpeed ? R.string.unit_kilometer_per_hour : R.string.unit_minute_per_kilometer;
|
||||||
} else {
|
} else {
|
||||||
unitId = reportSpeed ? R.string.unit_mile_per_hour : R.string.unit_minute_per_mile;
|
unitId = reportSpeed ? R.string.unit_mile_per_hour : R.string.unit_minute_per_mile;
|
||||||
}
|
}
|
||||||
result[1] = context.getString(unitId);
|
String unitString = context.getString(unitId);
|
||||||
if (Double.isNaN(speed) || Double.isInfinite(speed)) {
|
|
||||||
result[0] = null;
|
if (Double.isNaN(speed_mps) || Double.isInfinite(speed_mps)) {
|
||||||
return result;
|
return new Pair<>(null, unitString);
|
||||||
}
|
}
|
||||||
speed *= UnitConversions.MS_TO_KMH;
|
|
||||||
|
speed_mps *= UnitConversions.MS_TO_KMH;
|
||||||
if (!metricUnits) {
|
if (!metricUnits) {
|
||||||
speed *= UnitConversions.KM_TO_MI;
|
speed_mps *= UnitConversions.KM_TO_MI;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reportSpeed) {
|
if (reportSpeed) {
|
||||||
result[0] = StringUtils.formatDecimal(speed);
|
return new Pair<>(StringUtils.formatDecimal(speed_mps), unitString);
|
||||||
} else {
|
|
||||||
// convert from hours to minutes
|
|
||||||
double pace = speed == 0 ? 0.0 : 60.0 / speed;
|
|
||||||
int minutes = (int) pace;
|
|
||||||
int seconds = (int) Math.round((pace - minutes) * 60.0);
|
|
||||||
result[0] = String.format(Locale.US, "%d:%02d", minutes, seconds);
|
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
|
// convert from hours to minutes
|
||||||
|
double pace = speed_mps == 0 ? 0.0 : 60.0 / speed_mps;
|
||||||
|
int minutes = (int) pace;
|
||||||
|
int seconds = (int) Math.round((pace - minutes) * 60.0);
|
||||||
|
return new Pair<>(String.format(Locale.US, "%d:%02d", minutes, seconds), unitString);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user