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 3b2948490..6dcbc8786 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 @@ -301,7 +301,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter { } while (cursor.moveToNext()); } - // Handle new drive files + // Insert new tracks from new drive files insertNewTracks(changes.values()); PreferencesUtils.setLong(context, R.string.drive_largest_change_id_key, largestChangeId); } finally { @@ -486,56 +486,100 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter { * @param driveFile the drive file */ private boolean updateTrack(Track track, File driveFile) throws IOException { - boolean useKmz = KmzTrackExporter.KMZ_EXTENSION.equals(driveFile.getFileExtension()); - java.io.File file = null; - try { - file = SyncUtils.getFile(context, myTracksProviderUtils, track, useKmz); - String digest = SyncUtils.md5(file); - if (digest != null && digest.equals(driveFile.getMd5Checksum())) { - track.setModifiedTime(driveFile.getModifiedDate().getValue()); - myTracksProviderUtils.updateTrack(track); - return true; - } - } finally { - if (file != null) { - file.delete(); - } + Track updatedTrack = importDriveFile(driveFile, track.getId()); + if (updatedTrack == null) { + return false; } + File updatedDriveFile; + String trackName = getTrackName(driveFile); + if (!updatedTrack.getName().equals(trackName)) { + updatedTrack.setName(trackName); + + /* + * The drive file title and the track name inside the drive file do not + * match, update the drive file. + */ + java.io.File file = null; + try { + file = SyncUtils.getTempFile(context, myTracksProviderUtils, updatedTrack, true); + updatedDriveFile = SyncUtils.updateDriveFile( + drive, driveFile, trackName + "." + KmzTrackExporter.KMZ_EXTENSION, file, true); + + if (updatedDriveFile == null) { + Log.e(TAG, "Unable to update drive file"); + return false; + } + } finally { + if (file != null) { + file.delete(); + } + } + } else { + updatedDriveFile = driveFile; + } + + SyncUtils.updateTrack(myTracksProviderUtils, updatedTrack, updatedDriveFile); + return true; + } + + /** + * Imports drive file to track. + * + * @param driveFile the drive file + * @param trackId the track id + */ + private Track importDriveFile(File driveFile, long trackId) throws IOException { InputStream inputStream = null; try { inputStream = downloadDriveFile(driveFile, true); if (inputStream == null) { - Log.e(TAG, "Unable to update track. Input stream is null for track " + track.getName()); - return false; + Log.e(TAG, + "Unable to import file. Input stream is null for drive file " + driveFile.getTitle()); + return null; } - + TrackImporter trackImporter; + boolean useKmz = KmzTrackExporter.KMZ_EXTENSION.equals(driveFile.getFileExtension()); if (useKmz) { - trackImporter = new KmzTrackImporter(context, track.getId()); + trackImporter = new KmzTrackImporter(context, trackId); } else { - trackImporter = new KmlFileTrackImporter(context, track.getId()); - } - - long trackId = trackImporter.importFile(inputStream); - if (trackId != -1L) { - Track newTrack = myTracksProviderUtils.getTrack(trackId); - if (newTrack == null) { - Log.e(TAG, "Unable to merge, imported track is null"); - } else { - SyncUtils.updateTrack(myTracksProviderUtils, newTrack, driveFile); - return true; - } + trackImporter = new KmlFileTrackImporter(context, trackId); + } + + long importedId = trackImporter.importFile(inputStream); + if (importedId == -1L) { + Log.e(TAG, "Unable to merge, imported id is -1L"); + return null; + } + Track track = myTracksProviderUtils.getTrack(importedId); + if (track == null) { + Log.e(TAG, "Unable to merge, imported track is null"); + return null; } else { - Log.e(TAG, "Unable to merge, track id is -1L"); + return track; } } catch (IOException e) { Log.e(TAG, "Unable to merge", e); + return null; } finally { if (inputStream != null) { inputStream.close(); } } - return false; + } + + /** + * Gets the track name from the drive file title. + * + * @param driveFile the drive file + */ + private String getTrackName(File driveFile) { + String title = driveFile.getTitle(); + int index = title.lastIndexOf('.'); + if (index == -1) { + return title; + } + return title.substring(0, index); } /** diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java b/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java index 057df7172..2e1111596 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java @@ -101,6 +101,7 @@ public class SyncUtils { private static final String TAG = SyncUtils.class.getSimpleName(); private static final String SYNC_AUTHORITY = "com.google.android.maps.mytracks"; + private static final String TEMP_DIR = "temp"; private SyncUtils() {} @@ -295,7 +296,7 @@ public class SyncUtils { throws IOException { java.io.File file = null; try { - file = getFile(context, myTracksProviderUtils, track, true); + file = getTempFile(context, myTracksProviderUtils, track, true); if (file == null) { Log.e(TAG, "Unable to add Drive file. File is null for track " + track.getName()); @@ -371,7 +372,7 @@ public class SyncUtils { java.io.File file = null; try { - file = SyncUtils.getFile(context, myTracksProviderUtils, track, true); + file = SyncUtils.getTempFile(context, myTracksProviderUtils, track, true); if (file == null) { Log.e(TAG, "Unable to update drive file. File is null for track " + track.getName()); @@ -418,7 +419,7 @@ public class SyncUtils { * @param file the track file. If null, just update the driveFile meta data * @param canRetry true if can retry */ - private static File updateDriveFile( + public static File updateDriveFile( Drive drive, File driveFile, String driveTitle, java.io.File file, boolean canRetry) throws IOException { try { @@ -442,24 +443,28 @@ public class SyncUtils { } /** - * Gets a file from a track. + * Gets a temporary file for a track. * * @param context the context * @param myTracksProviderUtils the myMyTracksProviderUtils * @param track the track * @param useKmz true to output kmz */ - public static java.io.File getFile( + public static java.io.File getTempFile( Context context, MyTracksProviderUtils myTracksProviderUtils, Track track, boolean useKmz) throws FileNotFoundException { String extension = useKmz ? KmzTrackExporter.KMZ_EXTENSION : TrackFileFormat.KML.getExtension(); - java.io.File directory = new java.io.File(context.getCacheDir(), extension); + java.io.File directory = new java.io.File(context.getCacheDir(), TEMP_DIR); if (!FileUtils.ensureDirectoryExists(directory)) { Log.d(TAG, "Unable to create " + directory.getAbsolutePath()); return null; } + for (java.io.File file : directory.listFiles()) { + file.delete(); + } + Track[] tracks = new Track[] { track }; java.io.File file = new java.io.File( directory, FileUtils.buildUniqueFileName(directory, track.getName(), extension));