Add track category, waypoint type, waypoint category to gpx importer/exporter.

This commit is contained in:
Jimmy Shih
2012-12-20 15:55:06 -08:00
parent efaa7df201
commit 283291b51b
4 changed files with 120 additions and 21 deletions
@@ -17,14 +17,17 @@
package com.google.android.apps.mytracks.io.file; package com.google.android.apps.mytracks.io.file;
import com.google.android.apps.mytracks.Constants; import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.content.DescriptionGeneratorImpl;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils; import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.Waypoint; import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.content.Waypoint.WaypointType; import com.google.android.apps.mytracks.content.Waypoint.WaypointType;
import com.google.android.apps.mytracks.services.TrackRecordingService; import com.google.android.apps.mytracks.services.TrackRecordingService;
import com.google.android.apps.mytracks.stats.TripStatistics;
import com.google.android.apps.mytracks.stats.TripStatisticsUpdater; import com.google.android.apps.mytracks.stats.TripStatisticsUpdater;
import com.google.android.apps.mytracks.util.LocationUtils; import com.google.android.apps.mytracks.util.LocationUtils;
import com.google.android.apps.mytracks.util.StringUtils; import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.apps.mytracks.util.TrackIconUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.content.Context; import android.content.Context;
@@ -99,12 +102,14 @@ public class GpxImporter extends DefaultHandler {
// GPX tags // GPX tags
private static final String TAG_ALTITUDE = "ele"; private static final String TAG_ALTITUDE = "ele";
private static final String TAG_DESCRIPTION = "desc"; private static final String TAG_DESCRIPTION = "desc";
private static final String TAG_COMMENT = "cmt";
private static final String TAG_GPX = "gpx"; private static final String TAG_GPX = "gpx";
private static final String TAG_NAME = "name"; private static final String TAG_NAME = "name";
private static final String TAG_TIME = "time"; private static final String TAG_TIME = "time";
private static final String TAG_TRACK = "trk"; private static final String TAG_TRACK = "trk";
private static final String TAG_TRACK_POINT = "trkpt"; private static final String TAG_TRACK_POINT = "trkpt";
private static final String TAG_TRACK_SEGMENT = "trkseg"; private static final String TAG_TRACK_SEGMENT = "trkseg";
private static final String TAG_TYPE = "type";
private static final String TAG_WAYPOINT = "wpt"; private static final String TAG_WAYPOINT = "wpt";
// GPX attributes // GPX attributes
@@ -227,6 +232,8 @@ public class GpxImporter extends DefaultHandler {
onNameElementEnd(); onNameElementEnd();
} else if (localName.equals(TAG_DESCRIPTION)) { } else if (localName.equals(TAG_DESCRIPTION)) {
onDescriptionElementEnd(); onDescriptionElementEnd();
} else if (localName.equals(TAG_TYPE)) {
onTypeElementEnd();
} else if (localName.equals(TAG_ALTITUDE)) { } else if (localName.equals(TAG_ALTITUDE)) {
onAltitudeElementEnd(); onAltitudeElementEnd();
} else if (localName.equals(TAG_TIME)) { } else if (localName.equals(TAG_TIME)) {
@@ -247,6 +254,8 @@ public class GpxImporter extends DefaultHandler {
} else { } else {
if (localName.equals(TAG_WAYPOINT)) { if (localName.equals(TAG_WAYPOINT)) {
onWaypointElementEnd(); onWaypointElementEnd();
} else if (localName.equals(TAG_COMMENT)) {
onCommentElementEnd();
} }
} }
@@ -425,6 +434,21 @@ public class GpxImporter extends DefaultHandler {
} }
} }
/**
* On type element end.
*/
private void onTypeElementEnd() {
if (content != null) {
String type = content.toString().trim();
if (trackData != null && trackData.parsedDepth == 1) {
trackData.track.setCategory(type);
trackData.track.setIcon(TrackIconUtils.getIconValue(context, type));
} else if (currentWaypoint != null) {
currentWaypoint.setCategory(type);
}
}
}
/** /**
* On altitude element end. * On altitude element end.
*/ */
@@ -484,6 +508,21 @@ public class GpxImporter extends DefaultHandler {
} }
} }
/**
* On comment element end.
*/
private void onCommentElementEnd() {
if (content != null) {
String comment = content.toString().trim();
if (currentWaypoint != null) {
WaypointType waypointType = WaypointType.STATISTICS.name().equals(comment) ? WaypointType.STATISTICS
: WaypointType.WAYPOINT;
currentWaypoint.setType(waypointType);
}
}
}
/** /**
* Parses a location. * Parses a location.
* *
@@ -603,14 +642,21 @@ public class GpxImporter extends DefaultHandler {
* @param track the track * @param track the track
*/ */
private void insertFirstWaypoint(Track track) { private void insertFirstWaypoint(Track track) {
String name = context.getString(R.string.marker_split_name_format, 0);
String category = "";
TripStatisticsUpdater updater = new TripStatisticsUpdater(
track.getTripStatistics().getStartTime());
TripStatistics tripStatistics = updater.getTripStatistics();
String description = new DescriptionGeneratorImpl(context).generateWaypointDescription(
tripStatistics);
String icon = context.getString(R.string.marker_statistics_icon_url); String icon = context.getString(R.string.marker_statistics_icon_url);
double length = 0.0;
// For track statistics, make it an impossible location long duration = 0L;
Location waypointLocation = new Location(""); Location waypointLocation = new Location("");
waypointLocation.setLatitude(100); waypointLocation.setLatitude(100);
waypointLocation.setLongitude(180); waypointLocation.setLongitude(180);
Waypoint waypoint = new Waypoint("", "", "", icon, track.getId(), WaypointType.STATISTICS, 0, Waypoint waypoint = new Waypoint(name, description, category, icon, track.getId(),
0, -1L, -1L, waypointLocation, null); WaypointType.STATISTICS, length, duration, -1L, -1L, waypointLocation, tripStatistics);
myTracksProviderUtils.insertWaypoint(waypoint); myTracksProviderUtils.insertWaypoint(waypoint);
} }
@@ -630,7 +676,9 @@ public class GpxImporter extends DefaultHandler {
Waypoint waypoint = null; Waypoint waypoint = null;
int trackPointPosition = -1; int trackPointPosition = -1;
Location trackPoint = null; Location trackPoint = null;
TripStatisticsUpdater updater = new TripStatisticsUpdater( TripStatisticsUpdater trackTripStatisticstrackUpdater = new TripStatisticsUpdater(
track.getTripStatistics().getStartTime());
TripStatisticsUpdater markerTripStatisticsUpdater = new TripStatisticsUpdater(
track.getTripStatistics().getStartTime()); track.getTripStatistics().getStartTime());
while (true) { while (true) {
@@ -651,7 +699,8 @@ public class GpxImporter extends DefaultHandler {
// No more track points. Ignore the rest of the waypoints. // No more track points. Ignore the rest of the waypoints.
return; return;
} }
updater.addLocation(trackPoint, minRecordingDistance); trackTripStatisticstrackUpdater.addLocation(trackPoint, minRecordingDistance);
markerTripStatisticsUpdater.addLocation(trackPoint, minRecordingDistance);
} }
if (waypoint.getLocation().getTime() > trackPoint.getTime()) { if (waypoint.getLocation().getTime() > trackPoint.getTime()) {
trackPoint = null; trackPoint = null;
@@ -661,13 +710,32 @@ public class GpxImporter extends DefaultHandler {
// The waypoint location time matches the track point time // The waypoint location time matches the track point time
if (trackPoint.getLatitude() == waypoint.getLocation().getLatitude() if (trackPoint.getLatitude() == waypoint.getLocation().getLatitude()
&& trackPoint.getLongitude() == waypoint.getLocation().getLongitude()) { && trackPoint.getLongitude() == waypoint.getLocation().getLongitude()) {
waypoint.setDuration(updater.getTripStatistics().getTotalTime());
waypoint.setIcon(context.getString(R.string.marker_waypoint_icon_url)); // Get tripStatistics, description, and icon
waypoint.setLength(updater.getTripStatistics().getTotalDistance()); TripStatistics tripStatistics;
waypoint.setLocation(trackPoint); String description;
waypoint.setTrackId(track.getId()); String icon;
waypoint.setType(WaypointType.WAYPOINT); if (waypoint.getType() == WaypointType.STATISTICS) {
myTracksProviderUtils.insertWaypoint(waypoint); tripStatistics = markerTripStatisticsUpdater.getTripStatistics();
markerTripStatisticsUpdater = new TripStatisticsUpdater(trackPoint.getTime());
description = new DescriptionGeneratorImpl(context).generateWaypointDescription(
tripStatistics);
icon = context.getString(R.string.marker_statistics_icon_url);
} else {
tripStatistics = null;
description = waypoint.getDescription();
icon = context.getString(R.string.marker_waypoint_icon_url);
}
// Get length and duration
double length = trackTripStatisticstrackUpdater.getTripStatistics().getTotalDistance();
long duration = trackTripStatisticstrackUpdater.getTripStatistics().getTotalTime();
// Insert waypoint
Waypoint newWaypoint = new Waypoint(waypoint.getName(), description,
waypoint.getCategory(), icon, track.getId(), waypoint.getType(), length, duration,
-1L, -1L, trackPoint, tripStatistics);
myTracksProviderUtils.insertWaypoint(newWaypoint);
} }
waypoint = null; waypoint = null;
} }
@@ -115,6 +115,7 @@ public class GpxTrackWriter implements TrackFormatWriter {
printWriter.println("<trk>"); printWriter.println("<trk>");
printWriter.println("<name>" + StringUtils.formatCData(track.getName()) + "</name>"); printWriter.println("<name>" + StringUtils.formatCData(track.getName()) + "</name>");
printWriter.println("<desc>" + StringUtils.formatCData(track.getDescription()) + "</desc>"); printWriter.println("<desc>" + StringUtils.formatCData(track.getDescription()) + "</desc>");
printWriter.println("<type>" + StringUtils.formatCData(track.getCategory()) + "</type>");
printWriter.println("<extensions><topografix:color>c0c0c0</topografix:color></extensions>"); printWriter.println("<extensions><topografix:color>c0c0c0</topografix:color></extensions>");
} }
} }
@@ -167,8 +168,10 @@ public class GpxTrackWriter implements TrackFormatWriter {
printWriter.println( printWriter.println(
"<time>" + StringUtils.formatDateTimeIso8601(location.getTime()) + "</time>"); "<time>" + StringUtils.formatDateTimeIso8601(location.getTime()) + "</time>");
printWriter.println("<name>" + StringUtils.formatCData(waypoint.getName()) + "</name>"); printWriter.println("<name>" + StringUtils.formatCData(waypoint.getName()) + "</name>");
printWriter.println("<cmt>" + StringUtils.formatCData(waypoint.getType().name()) + "</cmt>");
printWriter.println( printWriter.println(
"<desc>" + StringUtils.formatCData(waypoint.getDescription()) + "</desc>"); "<desc>" + StringUtils.formatCData(waypoint.getDescription()) + "</desc>");
printWriter.println("<type>" + StringUtils.formatCData(waypoint.getCategory()) + "</type>");
printWriter.println("</wpt>"); printWriter.println("</wpt>");
} }
} }
@@ -415,6 +415,8 @@ public class TrackRecordingService extends Service {
WaypointType waypointType = waypointCreationRequest.getType(); WaypointType waypointType = waypointCreationRequest.getType();
boolean isStatistics = waypointType == WaypointType.STATISTICS; boolean isStatistics = waypointType == WaypointType.STATISTICS;
// Get name
String name; String name;
if (waypointCreationRequest.getName() != null) { if (waypointCreationRequest.getName() != null) {
name = waypointCreationRequest.getName(); name = waypointCreationRequest.getName();
@@ -429,28 +431,33 @@ public class TrackRecordingService extends Service {
nextWaypointNumber); nextWaypointNumber);
} }
// Get category
String category = waypointCreationRequest.getCategory() != null ? waypointCreationRequest
.getCategory()
: "";
// Get tripStatistics, description, and icon
TripStatistics tripStatistics; TripStatistics tripStatistics;
String description; String description;
String icon;
if (isStatistics) { if (isStatistics) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
markerTripStatisticsUpdater.updateTime(now); markerTripStatisticsUpdater.updateTime(now);
tripStatistics = markerTripStatisticsUpdater.getTripStatistics(); tripStatistics = markerTripStatisticsUpdater.getTripStatistics();
markerTripStatisticsUpdater = new TripStatisticsUpdater(now); markerTripStatisticsUpdater = new TripStatisticsUpdater(now);
description = new DescriptionGeneratorImpl(this).generateWaypointDescription(tripStatistics); description = new DescriptionGeneratorImpl(this).generateWaypointDescription(tripStatistics);
icon = getString(R.string.marker_statistics_icon_url);
} else { } else {
tripStatistics = null; tripStatistics = null;
description = waypointCreationRequest.getDescription() != null ? waypointCreationRequest description = waypointCreationRequest.getDescription() != null ? waypointCreationRequest
.getDescription() .getDescription()
: ""; : "";
icon = getString(R.string.marker_waypoint_icon_url);
} }
String category = waypointCreationRequest.getCategory() != null ? waypointCreationRequest // Get length and duration
.getCategory()
: "";
String icon = getString(
isStatistics ? R.string.marker_statistics_icon_url : R.string.marker_waypoint_icon_url);
long duration;
double length; double length;
long duration;
Location location = getLastValidTrackPointInCurrentSegment(recordingTrackId); Location location = getLastValidTrackPointInCurrentSegment(recordingTrackId);
if (location != null && trackTripStatisticsUpdater != null) { if (location != null && trackTripStatisticsUpdater != null) {
TripStatistics stats = trackTripStatisticsUpdater.getTripStatistics(); TripStatistics stats = trackTripStatisticsUpdater.getTripStatistics();
@@ -464,9 +471,11 @@ public class TrackRecordingService extends Service {
location = new Location(""); location = new Location("");
location.setLatitude(100); location.setLatitude(100);
location.setLongitude(180); location.setLongitude(180);
length = 0; length = 0.0;
duration = 0; duration = 0L;
} }
// Insert waypoint
Waypoint waypoint = new Waypoint(name, description, category, icon, recordingTrackId, waypointType, Waypoint waypoint = new Waypoint(name, description, category, icon, recordingTrackId, waypointType,
length, duration, -1L, -1L, location, tripStatistics); length, duration, -1L, -1L, location, tripStatistics);
Uri uri = myTracksProviderUtils.insertWaypoint(waypoint); Uri uri = myTracksProviderUtils.insertWaypoint(waypoint);
@@ -1,4 +1,9 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore
org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning
org.eclipse.jdt.core.compiler.problem.autoboxing=ignore org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning
@@ -8,6 +13,7 @@ org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled
org.eclipse.jdt.core.compiler.problem.discouragedReference=warning org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.fallthroughCase=warning org.eclipse.jdt.core.compiler.problem.fallthroughCase=warning
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=warning org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
@@ -15,12 +21,15 @@ org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning
org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning
org.eclipse.jdt.core.compiler.problem.forbiddenReference=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=error
org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning
org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled
org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning
org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore
org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore
org.eclipse.jdt.core.compiler.problem.localVariableHiding=warning org.eclipse.jdt.core.compiler.problem.localVariableHiding=warning
org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning
org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore
org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore
org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled
org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=warning org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=warning
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled
@@ -29,21 +38,31 @@ org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=warni
org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning
org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore
org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error
org.eclipse.jdt.core.compiler.problem.nullReference=warning org.eclipse.jdt.core.compiler.problem.nullReference=warning
org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error
org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning
org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning
org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore
org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore
org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore
org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore
org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning
org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning
org.eclipse.jdt.core.compiler.problem.redundantNullCheck=warning org.eclipse.jdt.core.compiler.problem.redundantNullCheck=warning
org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore
org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore
org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore
org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning
org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled
org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning
org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning
org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore
org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning
org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore