1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-13 11:17:33 +01:00

More improvements of GPX export, optimize nearest closest trackpoint finding a bit

This commit is contained in:
cpfeiffer 2018-04-13 01:10:45 +02:00
parent 1b9b5f821e
commit 66063256a7

View File

@ -1,6 +1,7 @@
package nodomain.freeyourgadget.gadgetbridge.export; package nodomain.freeyourgadget.gadgetbridge.export;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Xml; import android.util.Xml;
import org.xmlpull.v1.XmlSerializer; import org.xmlpull.v1.XmlSerializer;
@ -31,7 +32,7 @@ public class GPXExporter implements ActivityTrackExporter {
private String creator; private String creator;
private boolean includeHeartRate = true; private boolean includeHeartRate = true;
private boolean includeHeartRateByTime = true; private boolean includeHeartRateOfNearestSample = true;
@NonNull @NonNull
@Override @Override
@ -90,7 +91,7 @@ public class GPXExporter implements ActivityTrackExporter {
String source = getSource(track); String source = getSource(track);
boolean atLeastOnePointExported = false; boolean atLeastOnePointExported = false;
for (ActivityPoint point : trackPoints) { for (ActivityPoint point : trackPoints) {
atLeastOnePointExported = exportTrackPoint(ser, point, source, trackPoints) | atLeastOnePointExported ; atLeastOnePointExported |= exportTrackPoint(ser, point, source, trackPoints);
} }
if(!atLeastOnePointExported) { if(!atLeastOnePointExported) {
@ -135,30 +136,19 @@ public class GPXExporter implements ActivityTrackExporter {
int hr = point.getHeartRate(); int hr = point.getHeartRate();
if (!HeartRateUtils.isValidHeartRateValue(hr)) { if (!HeartRateUtils.isValidHeartRateValue(hr)) {
if(!includeHeartRateByTime) { return; } if (!includeHeartRateOfNearestSample) {
return;
}
Date time = point.getTime(); ActivityPoint closestPointItem = findClosestSensibleActivityPoint(point.getTime(), trackPoints);
ActivityPoint closestPointItem = null; if(closestPointItem == null) {
long lowestDifference = Long.MAX_VALUE; return;
for (ActivityPoint pointItem : trackPoints) {
int hrItem = pointItem.getHeartRate();
if(HeartRateUtils.isValidHeartRateValue(hrItem)) {
Date timeItem = pointItem.getTime();
if (timeItem.after(time) || timeItem.equals(time)) continue;
long difference = time.getTime() - timeItem.getTime();
if (difference < lowestDifference) {
lowestDifference = difference;
closestPointItem = pointItem;
}
}
} }
if(closestPointItem != null) {
hr = closestPointItem.getHeartRate(); hr = closestPointItem.getHeartRate();
if (!HeartRateUtils.isValidHeartRateValue(hr)) { if (!HeartRateUtils.isValidHeartRateValue(hr)) {
return; return;
}
} }
return;
} }
ser.startTag(NS_DEFAULT, "extensions"); ser.startTag(NS_DEFAULT, "extensions");
@ -169,6 +159,27 @@ public class GPXExporter implements ActivityTrackExporter {
ser.endTag(NS_DEFAULT, "extensions"); ser.endTag(NS_DEFAULT, "extensions");
} }
private @Nullable ActivityPoint findClosestSensibleActivityPoint(Date time, List<ActivityPoint> trackPoints) {
ActivityPoint closestPointItem = null;
long lowestDifference = 60 * 2 * 1000; // minimum distance is 2min
for (ActivityPoint pointItem : trackPoints) {
int hrItem = pointItem.getHeartRate();
if (HeartRateUtils.isValidHeartRateValue(hrItem)) {
Date timeItem = pointItem.getTime();
if (timeItem.after(time) || timeItem.equals(time)) {
break; // we assume that the given trackPoints are sorted in time ascending order (oldest first)
}
long difference = time.getTime() - timeItem.getTime();
if (difference < lowestDifference) {
lowestDifference = difference;
closestPointItem = pointItem;
}
}
}
return closestPointItem;
}
private String formatLocation(double value) { private String formatLocation(double value) {
return new BigDecimal(value).setScale(GPSCoordinate.GPS_DECIMAL_DEGREES_SCALE, RoundingMode.HALF_UP).toPlainString(); return new BigDecimal(value).setScale(GPSCoordinate.GPS_DECIMAL_DEGREES_SCALE, RoundingMode.HALF_UP).toPlainString();
} }