From 2b633c091c0ffbf15f0cce71982fe41978e029b5 Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Wed, 9 Oct 2013 14:07:38 -0700 Subject: [PATCH] Code cleanup: add getExtension and getName in FileUtils.java --- .../io/file/importer/ImportAsyncTask.java | 14 +++++----- .../apps/mytracks/io/sync/SyncAdapter.java | 17 ++---------- .../apps/mytracks/io/sync/SyncUtils.java | 2 +- .../android/apps/mytracks/util/FileUtils.java | 26 +++++++++++++++++++ 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/importer/ImportAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/importer/ImportAsyncTask.java index 6b0e1a7a9..6f922f1f2 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/importer/ImportAsyncTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/importer/ImportAsyncTask.java @@ -178,8 +178,8 @@ public class ImportAsyncTask extends AsyncTask { try { TrackImporter trackImporter; if (trackFileFormat == TrackFileFormat.KML) { - String name = file.getName(); - if (name.endsWith("." + TrackFileFormat.KML.getExtension())) { + String extension = FileUtils.getExtension(file.getName()); + if (TrackFileFormat.KML.getExtension().equals(extension)) { trackImporter = new KmlFileTrackImporter(context, -1L); } else { MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context); @@ -221,15 +221,15 @@ public class ImportAsyncTask extends AsyncTask { if (candidates != null) { for (File candidate : candidates) { if (!FileUtils.isDirectory(candidate)) { - String name = candidate.getName(); + String extension = FileUtils.getExtension(candidate.getName()); if (trackFileFormat == TrackFileFormat.KML && ( - name.endsWith("." + TrackFileFormat.KML.getExtension()) - || name.endsWith("." + KmzTrackExporter.KMZ_EXTENSION))) { + TrackFileFormat.KML.getExtension().equals(extension) + || KmzTrackExporter.KMZ_EXTENSION.equals(extension))) { files.add(candidate); } else if (trackFileFormat == TrackFileFormat.GPX - && name.endsWith("." + TrackFileFormat.GPX.getExtension())) { + && TrackFileFormat.GPX.getExtension().equals(extension)) { files.add(candidate); - } + } } } } 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 6dcbc8786..2ed2ec221 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 @@ -23,6 +23,7 @@ import com.google.android.apps.mytracks.io.file.importer.KmlFileTrackImporter; import com.google.android.apps.mytracks.io.file.importer.KmzTrackImporter; import com.google.android.apps.mytracks.io.file.importer.TrackImporter; import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils; +import com.google.android.apps.mytracks.util.FileUtils; import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.gms.auth.GoogleAuthException; import com.google.android.gms.auth.UserRecoverableAuthException; @@ -491,7 +492,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter { return false; } File updatedDriveFile; - String trackName = getTrackName(driveFile); + String trackName = FileUtils.getName(driveFile.getTitle()); if (!updatedTrack.getName().equals(trackName)) { updatedTrack.setName(trackName); @@ -568,20 +569,6 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter { } } - /** - * 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); - } - /** * Deletes a drive file. * 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 cac4bc72f..7e110b56f 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 @@ -452,7 +452,6 @@ public class SyncUtils { 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(), FileUtils.TEMP_FILES_DIR); if (!FileUtils.ensureDirectoryExists(directory)) { @@ -465,6 +464,7 @@ public class SyncUtils { } Track[] tracks = new Track[] { track }; + String extension = useKmz ? KmzTrackExporter.KMZ_EXTENSION : TrackFileFormat.KML.getExtension(); java.io.File file = new java.io.File( directory, FileUtils.buildUniqueFileName(directory, track.getName(), extension)); FileTrackExporter fileTrackExporter = new FileTrackExporter( diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/FileUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/FileUtils.java index ef95ade02..41ea7de35 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/FileUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/FileUtils.java @@ -144,6 +144,32 @@ public class FileUtils { return buildUniqueFileName(directory, fileBaseName, extension, 0); } + /** + * Gets the name from a file name, without the extension. + * + * @param fileName the file name + */ + public static String getName(String fileName) { + int index = fileName.lastIndexOf('.'); + if (index == -1) { + return fileName; + } + return fileName.substring(0, index); + } + + /** + * Gets the extension from a file name. Returns null if there is no extension. + * + * @param fileName the file name + */ + public static String getExtension(String fileName) { + int index = fileName.lastIndexOf('.'); + if (index == -1) { + return null; + } + return fileName.substring(index + 1); + } + /** * Builds a filename with the given base and the given extension, possibly * adding a suffix to ensure the file doesn't exist.