diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/AbstractImporter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/AbstractImporter.java index 37a62fa48..887399d88 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/AbstractImporter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/AbstractImporter.java @@ -101,7 +101,6 @@ 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 trackIds; @@ -129,18 +128,14 @@ abstract class AbstractImporter extends DefaultHandler { * 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)); + protected AbstractImporter(Context context) { + this(context, MyTracksProviderUtils.Factory.get(context)); } @VisibleForTesting - protected AbstractImporter( - Context context, long importTrackId, MyTracksProviderUtils myTracksProviderUtils) { + protected AbstractImporter(Context context, 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); @@ -295,20 +290,10 @@ abstract class AbstractImporter extends DefaultHandler { /** * On track start. */ - protected void onTrackStart() throws SAXException { + protected void onTrackStart() { trackData = new TrackData(); - 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); - } + Uri uri = myTracksProviderUtils.insertTrack(trackData.track); + long trackId = Long.parseLong(uri.getLastPathSegment()); trackIds.add(trackId); trackData.track.setId(trackId); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/GpxImporter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/GpxImporter.java index 3f6acc670..ee1f8fc8f 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/GpxImporter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/GpxImporter.java @@ -52,15 +52,14 @@ public class GpxImporter extends AbstractImporter { * 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); + public GpxImporter(Context context) { + super(context); } @VisibleForTesting public GpxImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) { - super(context, -1L, myTracksProviderUtils); + super(context, myTracksProviderUtils); } @Override @@ -118,7 +117,7 @@ public class GpxImporter extends AbstractImporter { } @Override - protected void onTrackStart() throws SAXException { + protected void onTrackStart() { super.onTrackStart(); name = null; description = null; diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/ImportAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/ImportAsyncTask.java index e600b41e6..15404970e 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/ImportAsyncTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/ImportAsyncTask.java @@ -157,9 +157,8 @@ public class ImportAsyncTask extends AsyncTask { */ private boolean importFile(final File file) { try { - AbstractImporter importer = trackFileFormat == TrackFileFormat.KML ? new KmlImporter( - context, -1L) - : new GpxImporter(context, -1L); + AbstractImporter importer = trackFileFormat == TrackFileFormat.KML ? new KmlImporter(context) + : new GpxImporter(context); long trackIds[] = importer.importFile(new FileInputStream(file)); int length = trackIds.length; if (length > 0) { diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlImporter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlImporter.java index 59dd5b2e4..a78c321ec 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlImporter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlImporter.java @@ -71,15 +71,14 @@ public class KmlImporter extends AbstractImporter { * 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); + public KmlImporter(Context context) { + super(context); } @VisibleForTesting public KmlImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) { - super(context, -1L, myTracksProviderUtils); + super(context, myTracksProviderUtils); } @Override diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncAdapter.java b/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncAdapter.java index 637c51929..e0c932208 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncAdapter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncAdapter.java @@ -254,7 +254,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter { InputStream inputStream = downloadDriveFile(driveFile); // TODO: should retry if inputStream is null if (inputStream != null) { - KmlImporter kmlImporter = new KmlImporter(context, -1L); + KmlImporter kmlImporter = new KmlImporter(context); long[] tracksIds = kmlImporter.importFile(inputStream); if (tracksIds.length == 1) { Track track = myTracksProviderUtils.getTrack(tracksIds[0]); @@ -333,20 +333,29 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter { track.setModifiedTime(driveModifiedTime); myTracksProviderUtils.updateTrack(track); } else { - KmlImporter kmlImporter = new KmlImporter(context, track.getId()); + KmlImporter kmlImporter = new KmlImporter(context); long[] tracksIds = kmlImporter.importFile(inputStream); if (tracksIds.length == 1) { + // Delete track and update the new imported track + track.setDriveId(""); + myTracksProviderUtils.updateTrack(track); + myTracksProviderUtils.deleteTrack(track.getId()); + Track newTrack = myTracksProviderUtils.getTrack(tracksIds[0]); newTrack.setDriveId(driveFile.getId()); newTrack.setModifiedTime(driveModifiedTime); myTracksProviderUtils.updateTrack(newTrack); } else { - /* - * TODO: Should revert the track back to the original. + * Do not need to retry, the drive file is invalid. Remove imported + * tracks and update the track modified time. */ - Log.e( - TAG, "Unable to update drive change. Imported size is not 1 for " + track.getName()); + Log.e(TAG, "Unable to update drive change for track " + track.getName()); + for (int i = 0; i < tracksIds.length; i++) { + myTracksProviderUtils.deleteTrack(tracksIds[i]); + } + track.setModifiedTime(driveModifiedTime); + myTracksProviderUtils.updateTrack(track); } } } diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtils.java b/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtils.java index dee6ed262..6b9b41d4f 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtils.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtils.java @@ -40,13 +40,6 @@ public interface MyTracksProviderUtils { */ public static final String AUTHORITY = "com.google.android.maps.mytracks"; - /** - * Clears a track. Removes waypoints and trackpoints. Only keeps the track id. - * - * @param trackId the track id - */ - public void clearTrack(long trackId); - /** * Creates a {@link Track} from a cursor. * diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtilsImpl.java b/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtilsImpl.java index 6f3a7bbb6..97903ca17 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtilsImpl.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtilsImpl.java @@ -50,14 +50,6 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils { this.contentResolver = contentResolver; } - @Override - public void clearTrack(long trackId) { - deleteTrackPointsAndWaypoints(trackId); - Track track = new Track(); - track.setId(trackId); - updateTrack(track); - } - @Override public Track createTrack(Cursor cursor) { int idIndex = cursor.getColumnIndexOrThrow(TracksColumns._ID); @@ -184,21 +176,6 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils { @Override public void deleteTrack(long trackId) { - deleteTrackPointsAndWaypoints(trackId); - - // Delete track last since it triggers a database vaccum call - contentResolver.delete(TracksColumns.CONTENT_URI, TracksColumns._ID + "=?", - new String[] { Long.toString(trackId) }); - } - - /** - * Deletes track points and waypoints of a track. Assumes - * {@link TracksColumns#STARTID}, {@link TracksColumns#STOPID}, and - * {@link TracksColumns#NUMPOINTS} will be updated by the caller. - * - * @param trackId the track id - */ - private void deleteTrackPointsAndWaypoints(long trackId) { Track track = getTrack(trackId); if (track != null) { String where = TrackPointsColumns._ID + ">=? AND " + TrackPointsColumns._ID + "<=?"; @@ -208,6 +185,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils { } contentResolver.delete(WaypointsColumns.CONTENT_URI, WaypointsColumns.TRACKID + "=?", new String[] { Long.toString(trackId) }); + + // Delete track last since it triggers a database vaccum call + contentResolver.delete(TracksColumns.CONTENT_URI, TracksColumns._ID + "=?", + new String[] { Long.toString(trackId) }); } @Override