Allow importer to update an existing track. Add clearTrack in MyTracksProviderUtils

This commit is contained in:
Jimmy Shih
2013-01-23 11:04:15 -08:00
parent 6b5f9c976b
commit 07cd1dcdbb
6 changed files with 95 additions and 32 deletions
@@ -101,6 +101,7 @@ abstract class AbstractImporter extends DefaultHandler {
private static final int MAX_BUFFERED_LOCATIONS = 512;
private final Context context;
private final long importTrackId;
private final MyTracksProviderUtils myTracksProviderUtils;
private final int minRecordingDistance;
private final List<Long> trackIds;
@@ -124,17 +125,25 @@ abstract class AbstractImporter extends DefaultHandler {
protected String time;
protected String waypointType;
protected AbstractImporter(Context context) {
this(context, MyTracksProviderUtils.Factory.get(context));
/**
* Constructor.
*
* @param context the context
* @param importTrackId the track id to import to. -1L to import to a new
* track.
*/
protected AbstractImporter(Context context, long importTrackId) {
this(context, importTrackId, MyTracksProviderUtils.Factory.get(context));
}
@VisibleForTesting
protected AbstractImporter(
Context context, MyTracksProviderUtils myTracksProviderUtils) {
Context context, long importTrackId, MyTracksProviderUtils myTracksProviderUtils) {
this.context = context;
this.importTrackId = importTrackId;
this.myTracksProviderUtils = myTracksProviderUtils;
this.minRecordingDistance = PreferencesUtils.getInt(context, R.string.min_recording_distance_key,
PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT);
this.minRecordingDistance = PreferencesUtils.getInt(context,
R.string.min_recording_distance_key, PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT);
trackIds = new ArrayList<Long>();
waypoints = new ArrayList<Waypoint>();
}
@@ -286,12 +295,22 @@ abstract class AbstractImporter extends DefaultHandler {
/**
* On track start.
*/
protected void onTrackStart() {
protected void onTrackStart() throws SAXException {
trackData = new TrackData();
Uri uri = myTracksProviderUtils.insertTrack(trackData.track);
long id = Long.parseLong(uri.getLastPathSegment());
trackIds.add(id);
trackData.track.setId(id);
long trackId;
if (importTrackId == -1L) {
Uri uri = myTracksProviderUtils.insertTrack(trackData.track);
trackId = Long.parseLong(uri.getLastPathSegment());
} else {
if (trackIds.size() > 0) {
throw new SAXException(createErrorMessage(
"Cannot import more than one track to an existing track " + importTrackId));
}
trackId = importTrackId;
myTracksProviderUtils.clearTrack(trackId);
}
trackIds.add(trackId);
trackData.track.setId(trackId);
}
/**
@@ -48,13 +48,19 @@ public class GpxImporter extends AbstractImporter {
private static final String ATTRIBUTE_LAT = "lat";
private static final String ATTRIBUTE_LON = "lon";
public GpxImporter(Context context) {
super(context);
/**
* Constructor.
*
* @param context the context
* @param importTrackId track id to import to. -1L to import to a new track.
*/
public GpxImporter(Context context, long importTrackId) {
super(context, importTrackId);
}
@VisibleForTesting
public GpxImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) {
super(context, myTracksProviderUtils);
super(context, -1L, myTracksProviderUtils);
}
@Override
@@ -112,7 +118,7 @@ public class GpxImporter extends AbstractImporter {
}
@Override
protected void onTrackStart() {
protected void onTrackStart() throws SAXException {
super.onTrackStart();
name = null;
description = null;
@@ -73,7 +73,7 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
this.trackFileFormat = trackFileFormat;
this.path = path;
context = importActivity.getApplicationContext();
// Get the wake lock if not recording or paused
if (PreferencesUtils.getLong(importActivity, R.string.recording_track_id_key)
== PreferencesUtils.RECORDING_TRACK_ID_DEFAULT || PreferencesUtils.getBoolean(
@@ -157,8 +157,9 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
*/
private boolean importFile(final File file) {
try {
AbstractImporter importer = trackFileFormat == TrackFileFormat.KML ? new KmlImporter(context)
: new GpxImporter(context);
AbstractImporter importer = trackFileFormat == TrackFileFormat.KML ? new KmlImporter(
context, -1L)
: new GpxImporter(context, -1L);
long trackIds[] = importer.importFile(new FileInputStream(file));
int length = trackIds.length;
if (length > 0) {
@@ -67,15 +67,21 @@ public class KmlImporter extends AbstractImporter {
private ArrayList<Integer> heartRateList;
private ArrayList<Integer> powerList;
public KmlImporter(Context context) {
super(context);
/**
* Constructor.
*
* @param context the context
* @param importTrackId track id to import to. -1L to import to a new track.
*/
public KmlImporter(Context context, long importTrackId) {
super(context, importTrackId);
}
@VisibleForTesting
public KmlImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) {
super(context, myTracksProviderUtils);
super(context, -1L, myTracksProviderUtils);
}
@Override
public void startElement(String uri, String localName, String tag, Attributes attributes)
throws SAXException {