aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2011-11-16 19:27:19 +0100
committerSamuel Tardieu <sam@rfc1149.net>2011-11-16 19:32:54 +0100
commit31da7083b1e708034d0220e454afe8d992adedfd (patch)
treeadcb1645820835022ea000ddd0d0f0edc484fcc1 /main/src/cgeo/geocaching
parent21bf94937ab7e021536a2b07d42a637230cd4d15 (diff)
downloadcgeo-31da7083b1e708034d0220e454afe8d992adedfd.zip
cgeo-31da7083b1e708034d0220e454afe8d992adedfd.tar.gz
cgeo-31da7083b1e708034d0220e454afe8d992adedfd.tar.bz2
Create directories only when we will write a file
The directory structure should not be created when we are trying to read a file, especially if this file is located into the secondary storage area where we look "just in case". This fixes #802.
Diffstat (limited to 'main/src/cgeo/geocaching')
-rw-r--r--main/src/cgeo/geocaching/StaticMapsProvider.java6
-rw-r--r--main/src/cgeo/geocaching/cgCacheListAdapter.java2
-rw-r--r--main/src/cgeo/geocaching/cgDirectionImg.java6
-rw-r--r--main/src/cgeo/geocaching/cgeoimages.java2
-rw-r--r--main/src/cgeo/geocaching/cgeosmaps.java4
-rw-r--r--main/src/cgeo/geocaching/files/LocalStorage.java35
-rw-r--r--main/src/cgeo/geocaching/network/HtmlImage.java4
7 files changed, 30 insertions, 29 deletions
diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java
index 34ebd3d..7f4587d 100644
--- a/main/src/cgeo/geocaching/StaticMapsProvider.java
+++ b/main/src/cgeo/geocaching/StaticMapsProvider.java
@@ -21,8 +21,8 @@ public class StaticMapsProvider {
*/
private static final int MIN_MAP_IMAGE_BYTES = 6000;
- public static File getMapFile(final String geocode, final int level) {
- return LocalStorage.getStorageFile(geocode, "map_" + level, false);
+ public static File getMapFile(final String geocode, final int level, final boolean createDirs) {
+ return LocalStorage.getStorageFile(geocode, "map_" + level, false, createDirs);
}
private static void downloadMapsInThread(final cgCache cache, String latlonMap, int edge, String waypoints) {
@@ -39,7 +39,7 @@ public class StaticMapsProvider {
final String url = mapUrl + "&zoom=" + zoom + "&size=" + edge + "x" + edge + "&maptype=" + mapType + "&markers=icon%3A" + markerUrl + "%7C" + latlonMap + waypoints + "&sensor=false";
- final File file = getMapFile(cache.getGeocode(), level);
+ final File file = getMapFile(cache.getGeocode(), level, true);
final HttpResponse httpResponse = cgBase.request(url, null, false);
if (httpResponse != null) {
diff --git a/main/src/cgeo/geocaching/cgCacheListAdapter.java b/main/src/cgeo/geocaching/cgCacheListAdapter.java
index 6516c25..1b0082b 100644
--- a/main/src/cgeo/geocaching/cgCacheListAdapter.java
+++ b/main/src/cgeo/geocaching/cgCacheListAdapter.java
@@ -483,7 +483,7 @@ public class cgCacheListAdapter extends ArrayAdapter<cgCache> {
holder.directionLayout.setVisibility(View.GONE);
holder.distance.clear();
- final Bitmap dirImgPre = BitmapFactory.decodeFile(cgDirectionImg.getDirectionFile(cache.getGeocode()).getPath());
+ final Bitmap dirImgPre = BitmapFactory.decodeFile(cgDirectionImg.getDirectionFile(cache.getGeocode(), false).getPath());
final Bitmap dirImg;
if (dirImgPre != null) { // null happens for invalid caches (not yet released)
dirImg = dirImgPre.copy(Bitmap.Config.ARGB_8888, true);
diff --git a/main/src/cgeo/geocaching/cgDirectionImg.java b/main/src/cgeo/geocaching/cgDirectionImg.java
index 964af5d..7b28bf2 100644
--- a/main/src/cgeo/geocaching/cgDirectionImg.java
+++ b/main/src/cgeo/geocaching/cgDirectionImg.java
@@ -17,12 +17,12 @@ public class cgDirectionImg {
final HttpResponse httpResponse =
cgBase.request("http://www.geocaching.com/ImgGen/seek/CacheDir.ashx", new Parameters("k", code), false);
if (httpResponse != null) {
- LocalStorage.saveEntityToFile(httpResponse.getEntity(), getDirectionFile(geocode));
+ LocalStorage.saveEntityToFile(httpResponse.getEntity(), getDirectionFile(geocode, true));
}
}
- public static File getDirectionFile(final String geocode) {
- return LocalStorage.getStorageFile(geocode, "direction.png", false);
+ public static File getDirectionFile(final String geocode, final boolean createDirs) {
+ return LocalStorage.getStorageFile(geocode, "direction.png", false, createDirs);
}
} \ No newline at end of file
diff --git a/main/src/cgeo/geocaching/cgeoimages.java b/main/src/cgeo/geocaching/cgeoimages.java
index a6f1740..32ba3ba 100644
--- a/main/src/cgeo/geocaching/cgeoimages.java
+++ b/main/src/cgeo/geocaching/cgeoimages.java
@@ -198,7 +198,7 @@ public class cgeoimages extends AbstractActivity {
}
private void viewImageInStandardApp(final BitmapDrawable image) {
- final File file = LocalStorage.getStorageFile(null, "temp.jpg", false);
+ final File file = LocalStorage.getStorageFile(null, "temp.jpg", false, true);
try {
final FileOutputStream fos = new FileOutputStream(file);
image.getBitmap().compress(CompressFormat.JPEG, 100, fos);
diff --git a/main/src/cgeo/geocaching/cgeosmaps.java b/main/src/cgeo/geocaching/cgeosmaps.java
index cee43e5..965542f 100644
--- a/main/src/cgeo/geocaching/cgeosmaps.java
+++ b/main/src/cgeo/geocaching/cgeosmaps.java
@@ -116,7 +116,7 @@ public class cgeosmaps extends AbstractActivity {
for (int level = 1; level <= 5; level++) {
try {
- final Bitmap image = BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, level).getPath());
+ final Bitmap image = BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, level, false).getPath());
if (image != null) {
maps.add(image);
}
@@ -128,7 +128,7 @@ public class cgeosmaps extends AbstractActivity {
if (maps.isEmpty()) {
for (int level = 1; level <= 5; level++) {
try {
- final Bitmap image = BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, level).getPath());
+ final Bitmap image = BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, level, false).getPath());
if (image != null) {
maps.add(image);
}
diff --git a/main/src/cgeo/geocaching/files/LocalStorage.java b/main/src/cgeo/geocaching/files/LocalStorage.java
index 1b98913..edfb93a 100644
--- a/main/src/cgeo/geocaching/files/LocalStorage.java
+++ b/main/src/cgeo/geocaching/files/LocalStorage.java
@@ -36,7 +36,7 @@ public class LocalStorage {
}
/**
- * Return the primary storage cache root (phone if external media is mounted, external media otherwise).
+ * Return the secondary storage cache root (phone if external media is mounted, external media otherwise).
*
* @return the root of the cache directory
*/
@@ -68,33 +68,31 @@ public class LocalStorage {
}
/**
- * Get the primary storage cache directory for a geocode. The directory and its parents will be created if
- * necessary. A null or empty geocode will be replaced by a default value.
+ * Get the primary storage cache directory for a geocode. A null or empty geocode will be replaced by a default
+ * value.
*
* @param geocode
* the geocode
* @return the cache directory
*/
public static File getStorageDir(final String geocode) {
- return buildStorageDir(getStorage(), geocode);
+ return storageDir(getStorage(), geocode);
}
/**
- * Get the secondary storage cache directory for a geocode. The directory and its parents will be created if
- * necessary. A null or empty geocode will be replaced by a default value.
+ * Get the secondary storage cache directory for a geocode. A null or empty geocode will be replaced by a default
+ * value.
*
* @param geocode
* the geocode
* @return the cache directory
*/
- public static File getStorageSecDir(final String geocode) {
- return buildStorageDir(getStorageSec(), geocode);
+ private static File getStorageSecDir(final String geocode) {
+ return storageDir(getStorageSec(), geocode);
}
- private static File buildStorageDir(final File base, final String geocode) {
- final File dir = new File(base, StringUtils.defaultIfEmpty(geocode, "_others"));
- dir.mkdirs();
- return dir;
+ private static File storageDir(final File base, final String geocode) {
+ return new File(base, StringUtils.defaultIfEmpty(geocode, "_others"));
}
/**
@@ -110,13 +108,13 @@ public class LocalStorage {
* true if an url was given, false if a file name was given
* @return the file
*/
- public static File getStorageFile(final String geocode, final String fileNameOrUrl, final boolean isUrl) {
- return buildFile(getStorageDir(geocode), fileNameOrUrl, isUrl);
+ public static File getStorageFile(final String geocode, final String fileNameOrUrl, final boolean isUrl, final boolean createDirs) {
+ return buildFile(getStorageDir(geocode), fileNameOrUrl, isUrl, createDirs);
}
/**
* Get the secondary file corresponding to a geocode and a file name or an url. If it is an url, an appropriate
- * filename will be built by hashing it. The directory structure will be created if needed.
+ * filename will be built by hashing it. The directory structure will not be created automatically.
* A null or empty geocode will be replaced by a default value.
*
* @param geocode
@@ -128,10 +126,13 @@ public class LocalStorage {
* @return the file
*/
public static File getStorageSecFile(final String geocode, final String fileNameOrUrl, final boolean isUrl) {
- return buildFile(getStorageSecDir(geocode), fileNameOrUrl, isUrl);
+ return buildFile(getStorageSecDir(geocode), fileNameOrUrl, isUrl, false);
}
- private static File buildFile(final File base, final String fileName, final boolean isUrl) {
+ private static File buildFile(final File base, final String fileName, final boolean isUrl, final boolean createDirs) {
+ if (createDirs) {
+ base.mkdirs();
+ }
return new File(base, isUrl ? CryptUtils.md5(fileName) + getExtension(fileName) : fileName);
}
diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java
index 7f4bc1b..6dd5238 100644
--- a/main/src/cgeo/geocaching/network/HtmlImage.java
+++ b/main/src/cgeo/geocaching/network/HtmlImage.java
@@ -92,7 +92,7 @@ public class HtmlImage implements Html.ImageGetter {
}
}
- final File file = LocalStorage.getStorageFile(geocode, url, true);
+ final File file = LocalStorage.getStorageFile(geocode, url, true, true);
LocalStorage.saveEntityToFile(bufferedEntity, file);
}
@@ -146,7 +146,7 @@ public class HtmlImage implements Html.ImageGetter {
private Bitmap loadImageFromStorage(final String url) {
try {
- final File file = LocalStorage.getStorageFile(geocode, url, true);
+ final File file = LocalStorage.getStorageFile(geocode, url, true, false);
final Bitmap image = loadCachedImage(file);
if (image != null) {
return image;