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 static final int MAX_BUFFERED_LOCATIONS = 512;
private final Context context; private final Context context;
private final long importTrackId;
private final MyTracksProviderUtils myTracksProviderUtils; private final MyTracksProviderUtils myTracksProviderUtils;
private final int minRecordingDistance; private final int minRecordingDistance;
private final List<Long> trackIds; private final List<Long> trackIds;
@@ -129,18 +128,14 @@ abstract class AbstractImporter extends DefaultHandler {
* Constructor. * Constructor.
* *
* @param context the context * @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) { protected AbstractImporter(Context context) {
this(context, importTrackId, MyTracksProviderUtils.Factory.get(context)); this(context, MyTracksProviderUtils.Factory.get(context));
} }
@VisibleForTesting @VisibleForTesting
protected AbstractImporter( protected AbstractImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) {
Context context, long importTrackId, MyTracksProviderUtils myTracksProviderUtils) {
this.context = context; this.context = context;
this.importTrackId = importTrackId;
this.myTracksProviderUtils = myTracksProviderUtils; this.myTracksProviderUtils = myTracksProviderUtils;
this.minRecordingDistance = PreferencesUtils.getInt(context, this.minRecordingDistance = PreferencesUtils.getInt(context,
R.string.min_recording_distance_key, PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT); R.string.min_recording_distance_key, PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT);
@@ -295,20 +290,10 @@ abstract class AbstractImporter extends DefaultHandler {
/** /**
* On track start. * On track start.
*/ */
protected void onTrackStart() throws SAXException { protected void onTrackStart() {
trackData = new TrackData(); trackData = new TrackData();
long trackId; Uri uri = myTracksProviderUtils.insertTrack(trackData.track);
if (importTrackId == -1L) { long trackId = Long.parseLong(uri.getLastPathSegment());
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); trackIds.add(trackId);
trackData.track.setId(trackId); trackData.track.setId(trackId);
} }
@@ -52,15 +52,14 @@ public class GpxImporter extends AbstractImporter {
* Constructor. * Constructor.
* *
* @param context the context * @param context the context
* @param importTrackId track id to import to. -1L to import to a new track.
*/ */
public GpxImporter(Context context, long importTrackId) { public GpxImporter(Context context) {
super(context, importTrackId); super(context);
} }
@VisibleForTesting @VisibleForTesting
public GpxImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) { public GpxImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) {
super(context, -1L, myTracksProviderUtils); super(context, myTracksProviderUtils);
} }
@Override @Override
@@ -118,7 +117,7 @@ public class GpxImporter extends AbstractImporter {
} }
@Override @Override
protected void onTrackStart() throws SAXException { protected void onTrackStart() {
super.onTrackStart(); super.onTrackStart();
name = null; name = null;
description = null; description = null;
@@ -157,9 +157,8 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
*/ */
private boolean importFile(final File file) { private boolean importFile(final File file) {
try { try {
AbstractImporter importer = trackFileFormat == TrackFileFormat.KML ? new KmlImporter( AbstractImporter importer = trackFileFormat == TrackFileFormat.KML ? new KmlImporter(context)
context, -1L) : new GpxImporter(context);
: new GpxImporter(context, -1L);
long trackIds[] = importer.importFile(new FileInputStream(file)); long trackIds[] = importer.importFile(new FileInputStream(file));
int length = trackIds.length; int length = trackIds.length;
if (length > 0) { if (length > 0) {
@@ -71,15 +71,14 @@ public class KmlImporter extends AbstractImporter {
* Constructor. * Constructor.
* *
* @param context the context * @param context the context
* @param importTrackId track id to import to. -1L to import to a new track.
*/ */
public KmlImporter(Context context, long importTrackId) { public KmlImporter(Context context) {
super(context, importTrackId); super(context);
} }
@VisibleForTesting @VisibleForTesting
public KmlImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) { public KmlImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) {
super(context, -1L, myTracksProviderUtils); super(context, myTracksProviderUtils);
} }
@Override @Override
@@ -254,7 +254,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
InputStream inputStream = downloadDriveFile(driveFile); InputStream inputStream = downloadDriveFile(driveFile);
// TODO: should retry if inputStream is null // TODO: should retry if inputStream is null
if (inputStream != null) { if (inputStream != null) {
KmlImporter kmlImporter = new KmlImporter(context, -1L); KmlImporter kmlImporter = new KmlImporter(context);
long[] tracksIds = kmlImporter.importFile(inputStream); long[] tracksIds = kmlImporter.importFile(inputStream);
if (tracksIds.length == 1) { if (tracksIds.length == 1) {
Track track = myTracksProviderUtils.getTrack(tracksIds[0]); Track track = myTracksProviderUtils.getTrack(tracksIds[0]);
@@ -333,20 +333,29 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
track.setModifiedTime(driveModifiedTime); track.setModifiedTime(driveModifiedTime);
myTracksProviderUtils.updateTrack(track); myTracksProviderUtils.updateTrack(track);
} else { } else {
KmlImporter kmlImporter = new KmlImporter(context, track.getId()); KmlImporter kmlImporter = new KmlImporter(context);
long[] tracksIds = kmlImporter.importFile(inputStream); long[] tracksIds = kmlImporter.importFile(inputStream);
if (tracksIds.length == 1) { 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]); Track newTrack = myTracksProviderUtils.getTrack(tracksIds[0]);
newTrack.setDriveId(driveFile.getId()); newTrack.setDriveId(driveFile.getId());
newTrack.setModifiedTime(driveModifiedTime); newTrack.setModifiedTime(driveModifiedTime);
myTracksProviderUtils.updateTrack(newTrack); myTracksProviderUtils.updateTrack(newTrack);
} else { } 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( Log.e(TAG, "Unable to update drive change for track " + track.getName());
TAG, "Unable to update drive change. Imported size is not 1 for " + 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"; 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. * Creates a {@link Track} from a cursor.
* *
@@ -50,14 +50,6 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
this.contentResolver = contentResolver; this.contentResolver = contentResolver;
} }
@Override
public void clearTrack(long trackId) {
deleteTrackPointsAndWaypoints(trackId);
Track track = new Track();
track.setId(trackId);
updateTrack(track);
}
@Override @Override
public Track createTrack(Cursor cursor) { public Track createTrack(Cursor cursor) {
int idIndex = cursor.getColumnIndexOrThrow(TracksColumns._ID); int idIndex = cursor.getColumnIndexOrThrow(TracksColumns._ID);
@@ -184,21 +176,6 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
@Override @Override
public void deleteTrack(long trackId) { 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); Track track = getTrack(trackId);
if (track != null) { if (track != null) {
String where = TrackPointsColumns._ID + ">=? AND " + TrackPointsColumns._ID + "<=?"; String where = TrackPointsColumns._ID + ">=? AND " + TrackPointsColumns._ID + "<=?";
@@ -208,6 +185,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
} }
contentResolver.delete(WaypointsColumns.CONTENT_URI, WaypointsColumns.TRACKID + "=?", contentResolver.delete(WaypointsColumns.CONTENT_URI, WaypointsColumns.TRACKID + "=?",
new String[] { Long.toString(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 @Override