Code cleanup: add try finally block for callers of SyncUtils.getFile

This commit is contained in:
Jimmy Shih
2013-10-04 15:04:23 -07:00
parent 6df5e2916f
commit 832397595d
@@ -282,13 +282,15 @@ public class SyncUtils {
public static File insertDriveFile(Drive drive, String folderId, Context context, public static File insertDriveFile(Drive drive, String folderId, Context context,
MyTracksProviderUtils myTracksProviderUtils, Track track, boolean canRetry) MyTracksProviderUtils myTracksProviderUtils, Track track, boolean canRetry)
throws IOException { throws IOException {
java.io.File file = getFile(context, myTracksProviderUtils, track); java.io.File file = null;
if (file == null) {
Log.e(TAG, "Unable to add Drive file. File is null for track " + track.getName());
return null;
}
try { try {
file = getFile(context, myTracksProviderUtils, track);
if (file == null) {
Log.e(TAG, "Unable to add Drive file. File is null for track " + track.getName());
return null;
}
Log.d(TAG, "Add Drive file for track " + track.getName()); Log.d(TAG, "Add Drive file for track " + track.getName());
File uploadedFile = insertDriveFile(drive, folderId, track.getName(), file, canRetry); File uploadedFile = insertDriveFile(drive, folderId, track.getName(), file, canRetry);
if (uploadedFile == null) { if (uploadedFile == null) {
@@ -298,7 +300,9 @@ public class SyncUtils {
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, uploadedFile); SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, uploadedFile);
return uploadedFile; return uploadedFile;
} finally { } finally {
file.delete(); if (file != null) {
file.delete();
}
} }
} }
@@ -354,37 +358,41 @@ public class SyncUtils {
MyTracksProviderUtils myTracksProviderUtils, Track track, boolean canRetry) MyTracksProviderUtils myTracksProviderUtils, Track track, boolean canRetry)
throws IOException { throws IOException {
Log.d(TAG, "Update drive file for track " + track.getName()); Log.d(TAG, "Update drive file for track " + track.getName());
java.io.File file = SyncUtils.getFile(context, myTracksProviderUtils, track); java.io.File file = null;
if (file == null) { try {
Log.e(TAG, "Unable to update drive file. File is null for track " + track.getName()); file = SyncUtils.getFile(context, myTracksProviderUtils, track);
return false;
} else { if (file == null) {
try { Log.e(TAG, "Unable to update drive file. File is null for track " + track.getName());
String title = track.getName() + "." + TrackFileFormat.KML.getExtension(); return false;
File updatedFile; }
String digest = md5(file);
if (digest != null && digest.equals(driveFile.getMd5Checksum())) { String title = track.getName() + "." + TrackFileFormat.KML.getExtension();
if (title.equals(driveFile.getTitle())) { File updatedFile;
updatedFile = driveFile; String digest = md5(file);
} else { if (digest != null && digest.equals(driveFile.getMd5Checksum())) {
updatedFile = updateDriveFile(drive, driveFile, title, null, canRetry); if (title.equals(driveFile.getTitle())) {
} updatedFile = driveFile;
} else { } else {
updatedFile = updateDriveFile(drive, driveFile, title, file, canRetry); updatedFile = updateDriveFile(drive, driveFile, title, null, canRetry);
} }
if (updatedFile == null) { } else {
Log.e(TAG, updatedFile = updateDriveFile(drive, driveFile, title, file, canRetry);
"Unable to update drive file. Updated file is null for track " + track.getName()); }
return false; if (updatedFile == null) {
} Log.e(
long modifiedTime = updatedFile.getModifiedDate().getValue(); TAG, "Unable to update drive file. Updated file is null for track " + track.getName());
if (track.getModifiedTime() != modifiedTime) { return false;
track.setModifiedTime(modifiedTime); }
myTracksProviderUtils.updateTrack(track); long modifiedTime = updatedFile.getModifiedDate().getValue();
} if (track.getModifiedTime() != modifiedTime) {
return true; track.setModifiedTime(modifiedTime);
} finally { myTracksProviderUtils.updateTrack(track);
}
return true;
} finally {
if (file != null) {
file.delete(); file.delete();
} }
} }