forked from upstream-mirrors/OpenTracks
Support renaming files on Google Drive when sync is enabled
This commit is contained in:
@@ -301,7 +301,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
} while (cursor.moveToNext());
|
} while (cursor.moveToNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle new drive files
|
// Insert new tracks from new drive files
|
||||||
insertNewTracks(changes.values());
|
insertNewTracks(changes.values());
|
||||||
PreferencesUtils.setLong(context, R.string.drive_largest_change_id_key, largestChangeId);
|
PreferencesUtils.setLong(context, R.string.drive_largest_change_id_key, largestChangeId);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -486,56 +486,100 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
* @param driveFile the drive file
|
* @param driveFile the drive file
|
||||||
*/
|
*/
|
||||||
private boolean updateTrack(Track track, File driveFile) throws IOException {
|
private boolean updateTrack(Track track, File driveFile) throws IOException {
|
||||||
boolean useKmz = KmzTrackExporter.KMZ_EXTENSION.equals(driveFile.getFileExtension());
|
Track updatedTrack = importDriveFile(driveFile, track.getId());
|
||||||
java.io.File file = null;
|
if (updatedTrack == null) {
|
||||||
try {
|
return false;
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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;
|
InputStream inputStream = null;
|
||||||
try {
|
try {
|
||||||
inputStream = downloadDriveFile(driveFile, true);
|
inputStream = downloadDriveFile(driveFile, true);
|
||||||
if (inputStream == null) {
|
if (inputStream == null) {
|
||||||
Log.e(TAG, "Unable to update track. Input stream is null for track " + track.getName());
|
Log.e(TAG,
|
||||||
return false;
|
"Unable to import file. Input stream is null for drive file " + driveFile.getTitle());
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
TrackImporter trackImporter;
|
TrackImporter trackImporter;
|
||||||
|
boolean useKmz = KmzTrackExporter.KMZ_EXTENSION.equals(driveFile.getFileExtension());
|
||||||
if (useKmz) {
|
if (useKmz) {
|
||||||
trackImporter = new KmzTrackImporter(context, track.getId());
|
trackImporter = new KmzTrackImporter(context, trackId);
|
||||||
} else {
|
} else {
|
||||||
trackImporter = new KmlFileTrackImporter(context, track.getId());
|
trackImporter = new KmlFileTrackImporter(context, trackId);
|
||||||
}
|
}
|
||||||
|
|
||||||
long trackId = trackImporter.importFile(inputStream);
|
long importedId = trackImporter.importFile(inputStream);
|
||||||
if (trackId != -1L) {
|
if (importedId == -1L) {
|
||||||
Track newTrack = myTracksProviderUtils.getTrack(trackId);
|
Log.e(TAG, "Unable to merge, imported id is -1L");
|
||||||
if (newTrack == null) {
|
return null;
|
||||||
Log.e(TAG, "Unable to merge, imported track is null");
|
}
|
||||||
} else {
|
Track track = myTracksProviderUtils.getTrack(importedId);
|
||||||
SyncUtils.updateTrack(myTracksProviderUtils, newTrack, driveFile);
|
if (track == null) {
|
||||||
return true;
|
Log.e(TAG, "Unable to merge, imported track is null");
|
||||||
}
|
return null;
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG, "Unable to merge, track id is -1L");
|
return track;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "Unable to merge", e);
|
Log.e(TAG, "Unable to merge", e);
|
||||||
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
if (inputStream != null) {
|
if (inputStream != null) {
|
||||||
inputStream.close();
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -101,6 +101,7 @@ public class SyncUtils {
|
|||||||
|
|
||||||
private static final String TAG = SyncUtils.class.getSimpleName();
|
private static final String TAG = SyncUtils.class.getSimpleName();
|
||||||
private static final String SYNC_AUTHORITY = "com.google.android.maps.mytracks";
|
private static final String SYNC_AUTHORITY = "com.google.android.maps.mytracks";
|
||||||
|
private static final String TEMP_DIR = "temp";
|
||||||
|
|
||||||
private SyncUtils() {}
|
private SyncUtils() {}
|
||||||
|
|
||||||
@@ -295,7 +296,7 @@ public class SyncUtils {
|
|||||||
throws IOException {
|
throws IOException {
|
||||||
java.io.File file = null;
|
java.io.File file = null;
|
||||||
try {
|
try {
|
||||||
file = getFile(context, myTracksProviderUtils, track, true);
|
file = getTempFile(context, myTracksProviderUtils, track, true);
|
||||||
|
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
Log.e(TAG, "Unable to add Drive file. File is null for track " + track.getName());
|
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;
|
java.io.File file = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
file = SyncUtils.getFile(context, myTracksProviderUtils, track, true);
|
file = SyncUtils.getTempFile(context, myTracksProviderUtils, track, true);
|
||||||
|
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
Log.e(TAG, "Unable to update drive file. File is null for track " + track.getName());
|
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 file the track file. If null, just update the driveFile meta data
|
||||||
* @param canRetry true if can retry
|
* @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)
|
Drive drive, File driveFile, String driveTitle, java.io.File file, boolean canRetry)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
try {
|
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 context the context
|
||||||
* @param myTracksProviderUtils the myMyTracksProviderUtils
|
* @param myTracksProviderUtils the myMyTracksProviderUtils
|
||||||
* @param track the track
|
* @param track the track
|
||||||
* @param useKmz true to output kmz
|
* @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)
|
Context context, MyTracksProviderUtils myTracksProviderUtils, Track track, boolean useKmz)
|
||||||
throws FileNotFoundException {
|
throws FileNotFoundException {
|
||||||
String extension = useKmz ? KmzTrackExporter.KMZ_EXTENSION : TrackFileFormat.KML.getExtension();
|
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)) {
|
if (!FileUtils.ensureDirectoryExists(directory)) {
|
||||||
Log.d(TAG, "Unable to create " + directory.getAbsolutePath());
|
Log.d(TAG, "Unable to create " + directory.getAbsolutePath());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (java.io.File file : directory.listFiles()) {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
|
||||||
Track[] tracks = new Track[] { track };
|
Track[] tracks = new Track[] { track };
|
||||||
java.io.File file = new java.io.File(
|
java.io.File file = new java.io.File(
|
||||||
directory, FileUtils.buildUniqueFileName(directory, track.getName(), extension));
|
directory, FileUtils.buildUniqueFileName(directory, track.getName(), extension));
|
||||||
|
|||||||
Reference in New Issue
Block a user