Code cleanup: add getExtension and getName in FileUtils.java

This commit is contained in:
Jimmy Shih
2013-10-09 14:07:38 -07:00
parent 120c965e32
commit 2b633c091c
4 changed files with 36 additions and 23 deletions
@@ -178,8 +178,8 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
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<Void, Integer, Boolean> {
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);
}
}
}
}
}
@@ -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.
*
@@ -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(
@@ -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.