Code cleanup: FileUtils.java

This commit is contained in:
Jimmy Shih
2013-10-04 15:37:10 -07:00
parent 4161f8ff56
commit 3827faa0e5
3 changed files with 29 additions and 22 deletions
@@ -20,6 +20,7 @@ import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.io.file.TrackFileFormat; import com.google.android.apps.mytracks.io.file.TrackFileFormat;
import com.google.android.apps.mytracks.io.file.exporter.KmzTrackExporter; import com.google.android.apps.mytracks.io.file.exporter.KmzTrackExporter;
import com.google.android.apps.mytracks.util.FileUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.SystemUtils; import com.google.android.apps.mytracks.util.SystemUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
@@ -219,7 +220,7 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
File[] candidates = file.listFiles(); File[] candidates = file.listFiles();
if (candidates != null) { if (candidates != null) {
for (File candidate : candidates) { for (File candidate : candidates) {
if (!candidate.isDirectory()) { if (!FileUtils.isDirectory(candidate)) {
String name = candidate.getName(); String name = candidate.getName();
if (trackFileFormat == TrackFileFormat.KML && ( if (trackFileFormat == TrackFileFormat.KML && (
name.endsWith("." + TrackFileFormat.KML.getExtension()) name.endsWith("." + TrackFileFormat.KML.getExtension())
@@ -117,7 +117,7 @@ public class KmzTrackImporter implements TrackImporter {
if (importTrackId != -1L) { if (importTrackId != -1L) {
File dir = new File( File dir = new File(
FileUtils.getPath(FileUtils.PICTURES_DIR, Long.toString(importTrackId))); FileUtils.getPath(FileUtils.PICTURES_DIR, Long.toString(importTrackId)));
if (dir.exists() && dir.isDirectory()) { if (FileUtils.isDirectory(dir)) {
for (File file : dir.listFiles()) { for (File file : dir.listFiles()) {
file.delete(); file.delete();
} }
@@ -23,7 +23,7 @@ import java.io.File;
/** /**
* Utilities for dealing with files. * Utilities for dealing with files.
* *
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public class FileUtils { public class FileUtils {
@@ -31,22 +31,28 @@ public class FileUtils {
private FileUtils() {} private FileUtils() {}
/** /**
* Name of the top-level directory inside the SD card where our files will be * The backups directory under the My Tracks external storage directory.
* read from/written to.
*/ */
protected static final String SDCARD_TOP_DIR = "MyTracks";
public static final String BACKUPS_DIR = "backups"; public static final String BACKUPS_DIR = "backups";
/**
* The pictures directory under the My Tracks external storage directory.
*/
public static final String PICTURES_DIR = "pictures"; public static final String PICTURES_DIR = "pictures";
/**
* The My Tracks external storage directory.
*/
@VisibleForTesting
static final String SDCARD_TOP_DIR = "MyTracks";
/** /**
* The maximum FAT32 path length. See the FAT32 spec at * The maximum FAT32 path length. See the FAT32 spec at
* http://msdn.microsoft.com/en-us/windows/hardware/gg463080 * http://msdn.microsoft.com/en-us/windows/hardware/gg463080
*/ */
@VisibleForTesting @VisibleForTesting
static final int MAX_FAT32_PATH_LENGTH = 260; static final int MAX_FAT32_PATH_LENGTH = 260;
/** /**
* Returns true if the external storage is available. * Returns true if the external storage is available.
*/ */
@@ -55,7 +61,7 @@ public class FileUtils {
return Environment.MEDIA_MOUNTED.equals(state) return Environment.MEDIA_MOUNTED.equals(state)
|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state); || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
} }
/** /**
* Returns true if the external storage is writable. * Returns true if the external storage is writable.
*/ */
@@ -63,7 +69,7 @@ public class FileUtils {
String state = Environment.getExternalStorageState(); String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state); return Environment.MEDIA_MOUNTED.equals(state);
} }
/** /**
* Returns true if the directory exists. * Returns true if the directory exists.
* *
@@ -74,9 +80,8 @@ public class FileUtils {
} }
/** /**
* Ensures the directory exists by creating it and its parents if * Ensures the directory exists by creating it and its parents if necessary.
* necessary. *
*
* @return whether the directory exists (either already existed or was * @return whether the directory exists (either already existed or was
* successfully created) * successfully created)
*/ */
@@ -84,7 +89,7 @@ public class FileUtils {
if (isDirectory(dir)) { if (isDirectory(dir)) {
return true; return true;
} }
return dir.mkdirs(); return dir.mkdirs();
} }
/** /**
@@ -102,9 +107,10 @@ public class FileUtils {
} }
return dirNameBuilder.toString(); return dirNameBuilder.toString();
} }
/** /**
* Gets a path on the external storage. * Gets a path on the external storage.
* *
* @param components the components * @param components the components
*/ */
public static String getPath(String... components) { public static String getPath(String... components) {
@@ -117,7 +123,7 @@ public class FileUtils {
/** /**
* Builds a filename with the given base name (prefix) and the given * Builds a filename with the given base name (prefix) and the given
* extension, possibly adding a suffix to ensure the file doesn't exist. * extension, possibly adding a suffix to ensure the file doesn't exist.
* *
* @param directory the directory the file will live in * @param directory the directory the file will live in
* @param fileBaseName the prefix for the file name * @param fileBaseName the prefix for the file name
* @param extension the file's extension * @param extension the file's extension
@@ -131,7 +137,7 @@ public class FileUtils {
/** /**
* Builds a filename with the given base and the given extension, possibly * Builds a filename with the given base and the given extension, possibly
* adding a suffix to ensure the file doesn't exist. * adding a suffix to ensure the file doesn't exist.
* *
* @param directory the directory the filename will be located in * @param directory the directory the filename will be located in
* @param base the base for the filename * @param base the base for the filename
* @param extension the extension for the filename * @param extension the extension for the filename
@@ -161,7 +167,7 @@ public class FileUtils {
* filename characters may be any combination of letters, digits, or * filename characters may be any combination of letters, digits, or
* characters with code point values greater than 127. Replaces the invalid * characters with code point values greater than 127. Replaces the invalid
* characters with "_" and collapses multiple "_" together. * characters with "_" and collapses multiple "_" together.
* *
* @param name name * @param name name
*/ */
@VisibleForTesting @VisibleForTesting
@@ -182,7 +188,7 @@ public class FileUtils {
/** /**
* Returns true if it is a special FAT32 character. * Returns true if it is a special FAT32 character.
* *
* @param character the character * @param character the character
*/ */
private static boolean isSpecialFat32(char character) { private static boolean isSpecialFat32(char character) {
@@ -219,7 +225,7 @@ public class FileUtils {
/** /**
* Truncates the name if necessary so the filename path length (directory + * Truncates the name if necessary so the filename path length (directory +
* name + suffix) meets the Fat32 path limit. * name + suffix) meets the Fat32 path limit.
* *
* @param directory directory * @param directory directory
* @param name name * @param name name
* @param suffix suffix * @param suffix suffix