aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/files/LocalStorage.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/files/LocalStorage.java')
-rw-r--r--main/src/cgeo/geocaching/files/LocalStorage.java94
1 files changed, 73 insertions, 21 deletions
diff --git a/main/src/cgeo/geocaching/files/LocalStorage.java b/main/src/cgeo/geocaching/files/LocalStorage.java
index ec09433..fc82409 100644
--- a/main/src/cgeo/geocaching/files/LocalStorage.java
+++ b/main/src/cgeo/geocaching/files/LocalStorage.java
@@ -2,6 +2,7 @@ package cgeo.geocaching.files;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.utils.CryptUtils;
+import cgeo.geocaching.utils.FileUtils;
import cgeo.geocaching.utils.IOUtils;
import cgeo.geocaching.utils.Log;
@@ -14,6 +15,7 @@ import android.os.Environment;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
@@ -24,12 +26,14 @@ import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
/**
* Handle local storage issues on phone and SD card.
*
*/
-public class LocalStorage {
+public final class LocalStorage {
public static final String HEADER_LAST_MODIFIED = "last-modified";
public static final String HEADER_ETAG = "etag";
@@ -39,6 +43,10 @@ public class LocalStorage {
private static File internalStorageBase;
+ private LocalStorage() {
+ // utility class
+ }
+
/**
* Return the primary storage cache root (external media if mounted, phone otherwise).
*
@@ -159,7 +167,7 @@ public class LocalStorage {
private static File buildFile(final File base, final String fileName, final boolean isUrl, final boolean createDirs) {
if (createDirs) {
- base.mkdirs();
+ FileUtils.mkdirs(base);
}
return new File(base, isUrl ? CryptUtils.md5(fileName) + getExtension(fileName) : fileName);
}
@@ -194,7 +202,7 @@ public class LocalStorage {
final Header header = response != null ? response.getFirstHeader(name) : null;
final File file = filenameForHeader(baseFile, name);
if (header == null) {
- file.delete();
+ FileUtils.deleteIgnoringFailure(file);
} else {
saveToFile(new ByteArrayInputStream(header.getValue().getBytes()), file);
}
@@ -256,15 +264,15 @@ public class LocalStorage {
final boolean written = copy(inputStream, fos);
fos.close();
if (!written) {
- targetFile.delete();
+ FileUtils.deleteIgnoringFailure(targetFile);
}
return written;
} finally {
- inputStream.close();
+ IOUtils.closeQuietly(inputStream);
}
} catch (IOException e) {
Log.e("LocalStorage.saveToFile", e);
- targetFile.delete();
+ FileUtils.deleteIgnoringFailure(targetFile);
}
return false;
}
@@ -279,28 +287,29 @@ public class LocalStorage {
* @return true if the copy happened without error, false otherwise
*/
public static boolean copy(final File source, final File destination) {
- destination.getParentFile().mkdirs();
+ FileUtils.mkdirs(destination.getParentFile());
InputStream input = null;
OutputStream output = null;
+ boolean copyDone = false;
+
try {
input = new BufferedInputStream(new FileInputStream(source));
output = new BufferedOutputStream(new FileOutputStream(destination));
- } catch (FileNotFoundException e) {
- Log.e("LocalStorage.copy: could not open file", e);
- IOUtils.closeQuietly(input);
- IOUtils.closeQuietly(output);
- return false;
- }
-
- boolean copyDone = copy(input, output);
-
- try {
+ copyDone = copy(input, output);
+ // close here already to catch any issue with closing
input.close();
output.close();
+ } catch (FileNotFoundException e) {
+ Log.e("LocalStorage.copy: could not copy file", e);
+ return false;
} catch (IOException e) {
- Log.e("LocalStorage.copy: could not close file", e);
+ Log.e("LocalStorage.copy: could not copy file", e);
return false;
+ } finally {
+ // close here quietly to clean up in all situations
+ IOUtils.closeQuietly(input);
+ IOUtils.closeQuietly(output);
}
return copyDone;
@@ -338,12 +347,12 @@ public class LocalStorage {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
- file.delete();
+ FileUtils.delete(file);
}
}
}
- return path.delete();
+ return FileUtils.delete(path);
}
/**
@@ -361,7 +370,7 @@ public class LocalStorage {
}
for (final File file : filesToDelete) {
try {
- if (!file.delete()) {
+ if (!FileUtils.delete(file)) {
Log.w("LocalStorage.deleteFilesPrefix: Can't delete file " + file.getName());
}
} catch (Exception e) {
@@ -389,4 +398,47 @@ public class LocalStorage {
};
return LocalStorage.getStorageDir(geocode).listFiles(filter);
}
+
+ /**
+ * Get all storages available on the device.
+ * Will include paths like /mnt/sdcard /mnt/usbdisk /mnt/ext_card /mnt/sdcard/ext_card
+ */
+ public static List<File> getStorages() {
+
+ String extStorage = Environment.getExternalStorageDirectory().getAbsolutePath();
+ List<File> storages = new ArrayList<File>();
+ storages.add(new File(extStorage));
+ File file = new File("/system/etc/vold.fstab");
+ if (file.canRead()) {
+ FileReader fr = null;
+ BufferedReader br = null;
+ try {
+ fr = new FileReader(file);
+ br = new BufferedReader(fr);
+ String s = br.readLine();
+ while (s != null) {
+ if (s.startsWith("dev_mount")) {
+ String[] tokens = StringUtils.split(s);
+ if (tokens.length >= 3) {
+ String path = tokens[2]; // mountpoint
+ if (!extStorage.equals(path)) {
+ File directory = new File(path);
+ if (directory.exists() && directory.isDirectory()) {
+ storages.add(directory);
+ }
+ }
+ }
+ }
+ s = br.readLine();
+ }
+ } catch (IOException e) {
+ Log.e("Could not get additional mount points for user content. " +
+ "Proceeding with external storage only (" + extStorage + ")");
+ } finally {
+ IOUtils.closeQuietly(fr);
+ IOUtils.closeQuietly(br);
+ }
+ }
+ return storages;
+ }
}