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;
try {
file = getFile(context, myTracksProviderUtils, track);
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());
return null; return null;
} }
try {
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,9 +300,11 @@ public class SyncUtils {
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, uploadedFile); SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, uploadedFile);
return uploadedFile; return uploadedFile;
} finally { } finally {
if (file != null) {
file.delete(); file.delete();
} }
} }
}
/** /**
* Inserts a Drive file. * Inserts a Drive file.
@@ -354,13 +358,16 @@ 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;
try {
file = SyncUtils.getFile(context, myTracksProviderUtils, track);
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());
return false; return false;
} else { }
try {
String title = track.getName() + "." + TrackFileFormat.KML.getExtension(); String title = track.getName() + "." + TrackFileFormat.KML.getExtension();
File updatedFile; File updatedFile;
String digest = md5(file); String digest = md5(file);
@@ -374,8 +381,8 @@ public class SyncUtils {
updatedFile = updateDriveFile(drive, driveFile, title, file, canRetry); updatedFile = updateDriveFile(drive, driveFile, title, file, canRetry);
} }
if (updatedFile == null) { if (updatedFile == null) {
Log.e(TAG, Log.e(
"Unable to update drive file. Updated file is null for track " + track.getName()); TAG, "Unable to update drive file. Updated file is null for track " + track.getName());
return false; return false;
} }
long modifiedTime = updatedFile.getModifiedDate().getValue(); long modifiedTime = updatedFile.getModifiedDate().getValue();
@@ -385,6 +392,7 @@ public class SyncUtils {
} }
return true; return true;
} finally { } finally {
if (file != null) {
file.delete(); file.delete();
} }
} }