When updating a track based on google drive changes, revert

if the google drive file is invalid.
This commit is contained in:
Jimmy Shih
2013-01-25 15:50:08 -08:00
parent 19131fe4a0
commit 642a843397
7 changed files with 34 additions and 69 deletions
@@ -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<Long> 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);
}
@@ -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;
@@ -157,9 +157,8 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
*/
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) {
@@ -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
@@ -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);
}
}
}
@@ -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.
*
@@ -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