When inserting a new kml/kmz file in Google Drive, make the the file title and the track name are the same

This commit is contained in:
Jimmy Shih
2013-10-09 15:21:28 -07:00
parent 2b633c091c
commit e94b7768dc
2 changed files with 22 additions and 44 deletions
@@ -26,6 +26,7 @@ import android.util.Log;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
@@ -142,7 +143,9 @@ public class KmzTrackExporter implements TrackExporter {
int byteCount = 0; int byteCount = 0;
while ((byteCount = fileInputStream.read(buffer)) != -1) { while ((byteCount = fileInputStream.read(buffer)) != -1) {
zipOutputStream.write(buffer, 0, byteCount); zipOutputStream.write(buffer, 0, byteCount);
} }
} catch (FileNotFoundException e) {
Log.e(TAG, "Unable to add image", e);
} finally { } finally {
if (fileInputStream != null) { if (fileInputStream != null) {
fileInputStream.close(); fileInputStream.close();
@@ -350,40 +350,15 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
if (driveFile == null) { if (driveFile == null) {
return; return;
} }
boolean success = false;
boolean useKmz = KmzTrackExporter.KMZ_EXTENSION.equals(driveFile.getFileExtension()); long trackId = -1L;
InputStream inputStream = null;
try { try {
inputStream = downloadDriveFile(driveFile, true); Uri uri = myTracksProviderUtils.insertTrack(new Track());
if (inputStream == null) { trackId = Long.parseLong(uri.getLastPathSegment());
continue; success = updateTrack(trackId, driveFile);
}
TrackImporter trackImporter;
if (useKmz) {
Uri uri = myTracksProviderUtils.insertTrack(new Track());
long newId = Long.parseLong(uri.getLastPathSegment());
trackImporter = new KmzTrackImporter(context, newId);
} else {
trackImporter = new KmlFileTrackImporter(context, -1L);
}
long trackId = trackImporter.importFile(inputStream);
if (trackId != -1L) {
Track track = myTracksProviderUtils.getTrack(trackId);
if (track == null) {
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId());
continue;
}
SyncUtils.updateTrack(myTracksProviderUtils, track, driveFile);
Log.d(TAG, "Add from Google Drive " + track.getName());
}
} catch (IOException e) {
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
} finally { } finally {
if (inputStream != null) { if (!success && trackId != -1L) {
inputStream.close(); myTracksProviderUtils.deleteTrack(trackId);
} }
} }
} }
@@ -472,7 +447,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
} else if (modifiedTime < driveModifiedTime) { } else if (modifiedTime < driveModifiedTime) {
Log.d(TAG, "Updating drive change for track " + track.getName() + " and drive file " Log.d(TAG, "Updating drive change for track " + track.getName() + " and drive file "
+ driveFile.getTitle()); + driveFile.getTitle());
if (!updateTrack(track, driveFile)) { if (!updateTrack(track.getId(), driveFile)) {
Log.e(TAG, "Unable to update drive change"); Log.e(TAG, "Unable to update drive change");
track.setModifiedTime(driveModifiedTime); track.setModifiedTime(driveModifiedTime);
myTracksProviderUtils.updateTrack(track); myTracksProviderUtils.updateTrack(track);
@@ -483,18 +458,18 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
/** /**
* Updates a track based on a drive file. Returns true if successful. * Updates a track based on a drive file. Returns true if successful.
* *
* @param track the track * @param trackId the track id
* @param driveFile the drive file * @param driveFile the drive file
*/ */
private boolean updateTrack(Track track, File driveFile) throws IOException { private boolean updateTrack(long trackId, File driveFile) throws IOException {
Track updatedTrack = importDriveFile(driveFile, track.getId()); Track track = importDriveFile(trackId, driveFile);
if (updatedTrack == null) { if (track == null) {
return false; return false;
} }
File updatedDriveFile; File updatedDriveFile;
String trackName = FileUtils.getName(driveFile.getTitle()); String trackName = FileUtils.getName(driveFile.getTitle());
if (!updatedTrack.getName().equals(trackName)) { if (!track.getName().equals(trackName)) {
updatedTrack.setName(trackName); track.setName(trackName);
/* /*
* The drive file title and the track name inside the drive file do not * The drive file title and the track name inside the drive file do not
@@ -502,7 +477,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
*/ */
java.io.File file = null; java.io.File file = null;
try { try {
file = SyncUtils.getTempFile(context, myTracksProviderUtils, updatedTrack, true); file = SyncUtils.getTempFile(context, myTracksProviderUtils, track, true);
updatedDriveFile = SyncUtils.updateDriveFile( updatedDriveFile = SyncUtils.updateDriveFile(
drive, driveFile, trackName + "." + KmzTrackExporter.KMZ_EXTENSION, file, true); drive, driveFile, trackName + "." + KmzTrackExporter.KMZ_EXTENSION, file, true);
@@ -519,17 +494,17 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
updatedDriveFile = driveFile; updatedDriveFile = driveFile;
} }
SyncUtils.updateTrack(myTracksProviderUtils, updatedTrack, updatedDriveFile); SyncUtils.updateTrack(myTracksProviderUtils, track, updatedDriveFile);
return true; return true;
} }
/** /**
* Imports drive file to track. * Imports drive file to track.
* *
* @param driveFile the drive file
* @param trackId the track id * @param trackId the track id
* @param driveFile the drive file
*/ */
private Track importDriveFile(File driveFile, long trackId) throws IOException { private Track importDriveFile(long trackId, File driveFile) throws IOException {
InputStream inputStream = null; InputStream inputStream = null;
try { try {
inputStream = downloadDriveFile(driveFile, true); inputStream = downloadDriveFile(driveFile, true);