aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main/src/cgeo/geocaching/StaticMapsProvider.java10
-rw-r--r--main/src/cgeo/geocaching/cgData.java9
-rw-r--r--main/src/cgeo/geocaching/cgeoapplication.java6
-rw-r--r--main/src/cgeo/geocaching/connector/gc/GCParser.java2
-rw-r--r--main/src/cgeo/geocaching/export/GpxExport.java4
-rw-r--r--main/src/cgeo/geocaching/files/LocalStorage.java21
-rw-r--r--main/src/cgeo/geocaching/network/HtmlImage.java11
-rw-r--r--main/src/cgeo/geocaching/ui/GPXListAdapter.java3
-rw-r--r--main/src/cgeo/geocaching/utils/FileUtils.java22
-rw-r--r--main/src/cgeo/geocaching/utils/Log.java18
-rw-r--r--main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java8
11 files changed, 79 insertions, 35 deletions
diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java
index 2555b80..eb59bcb 100644
--- a/main/src/cgeo/geocaching/StaticMapsProvider.java
+++ b/main/src/cgeo/geocaching/StaticMapsProvider.java
@@ -7,6 +7,7 @@ import cgeo.geocaching.geopoint.GeopointFormatter.Format;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.settings.Settings;
+import cgeo.geocaching.utils.FileUtils;
import cgeo.geocaching.utils.Log;
import ch.boye.httpclientandroidlib.HttpResponse;
@@ -82,7 +83,7 @@ public final class StaticMapsProvider {
// Delete image if it has no contents
final long fileSize = file.length();
if (fileSize < MIN_MAP_IMAGE_BYTES) {
- file.delete();
+ FileUtils.deleteIgnoringFailure(file);
}
}
}
@@ -229,10 +230,9 @@ public final class StaticMapsProvider {
int waypointId = waypoint.getId();
int waypointMapHash = waypoint.getStaticMapsHashcode();
for (int level = 1; level <= MAPS_LEVEL_MAX; level++) {
- try {
- StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + waypointMapHash + '_' + level, false).delete();
- } catch (Exception e) {
- Log.e("StaticMapsProvider.removeWpStaticMaps", e);
+ final File mapFile = StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + waypointMapHash + '_' + level, false);
+ if (!FileUtils.delete(mapFile)) {
+ Log.e("StaticMapsProvider.removeWpStaticMaps failed for " + mapFile.getAbsolutePath());
}
}
}
diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java
index f7b52e0..fb8908f 100644
--- a/main/src/cgeo/geocaching/cgData.java
+++ b/main/src/cgeo/geocaching/cgData.java
@@ -14,6 +14,7 @@ import cgeo.geocaching.files.LocalStorage;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.Viewport;
import cgeo.geocaching.settings.Settings;
+import cgeo.geocaching.utils.FileUtils;
import cgeo.geocaching.utils.Log;
import org.apache.commons.collections.CollectionUtils;
@@ -351,7 +352,9 @@ public class cgData {
return false;
}
- source.delete();
+ if (!FileUtils.delete(source)) {
+ Log.e("Original database could not be deleted during move");
+ }
Settings.setDbOnSDCard(!Settings.isDbOnSDCard());
Log.i("Database was moved to " + target);
init();
@@ -773,7 +776,7 @@ public class cgData {
final File[] wrongFiles = dir.listFiles(filter);
if (wrongFiles != null) {
for (final File wrongFile : wrongFiles) {
- wrongFile.delete();
+ FileUtils.deleteIgnoringFailure(wrongFile);
}
}
}
@@ -823,7 +826,7 @@ public class cgData {
for (final File file : LocalStorage.getStorageSec().listFiles()) {
if (file.isDirectory()) {
// This will silently fail if the directory is not empty.
- file.delete();
+ FileUtils.deleteIgnoringFailure(file);
}
}
}
diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java
index 700dea8..5a793f5 100644
--- a/main/src/cgeo/geocaching/cgeoapplication.java
+++ b/main/src/cgeo/geocaching/cgeoapplication.java
@@ -25,7 +25,11 @@ public class cgeoapplication extends Application {
private static cgeoapplication instance;
public cgeoapplication() {
- instance = this;
+ setInstance(this);
+ }
+
+ private static void setInstance(final cgeoapplication application) {
+ instance = application;
}
public static cgeoapplication getInstance() {
diff --git a/main/src/cgeo/geocaching/connector/gc/GCParser.java b/main/src/cgeo/geocaching/connector/gc/GCParser.java
index af6764f..0dc4242 100644
--- a/main/src/cgeo/geocaching/connector/gc/GCParser.java
+++ b/main/src/cgeo/geocaching/connector/gc/GCParser.java
@@ -764,7 +764,7 @@ public abstract class GCParser {
public static SearchResult searchByNextPage(final SearchResult search, boolean showCaptcha, RecaptchaReceiver recaptchaReceiver) {
if (search == null) {
- return search;
+ return null;
}
final String[] viewstates = search.getViewstates();
diff --git a/main/src/cgeo/geocaching/export/GpxExport.java b/main/src/cgeo/geocaching/export/GpxExport.java
index 4f5e063..b7b122e 100644
--- a/main/src/cgeo/geocaching/export/GpxExport.java
+++ b/main/src/cgeo/geocaching/export/GpxExport.java
@@ -151,9 +151,9 @@ class GpxExport extends AbstractExport {
// Ignore double error
}
}
- // delete partial gpx file on error
+ // delete partial GPX file on error
if (exportFile.exists()) {
- exportFile.delete();
+ FileUtils.deleteIgnoringFailure(exportFile);
}
return null;
diff --git a/main/src/cgeo/geocaching/files/LocalStorage.java b/main/src/cgeo/geocaching/files/LocalStorage.java
index d10f12b..7657e1e 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;
@@ -32,7 +33,7 @@ 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";
@@ -42,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).
*
@@ -197,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);
}
@@ -259,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;
}
@@ -341,12 +346,12 @@ public class LocalStorage {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
- file.delete();
+ FileUtils.delete(file);
}
}
}
- return path.delete();
+ return FileUtils.delete(path);
}
/**
@@ -364,7 +369,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) {
diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java
index 0649e12..797e67d 100644
--- a/main/src/cgeo/geocaching/network/HtmlImage.java
+++ b/main/src/cgeo/geocaching/network/HtmlImage.java
@@ -6,6 +6,7 @@ import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.compatibility.Compatibility;
import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.files.LocalStorage;
+import cgeo.geocaching.utils.FileUtils;
import cgeo.geocaching.utils.IOUtils;
import cgeo.geocaching.utils.ImageUtils;
import cgeo.geocaching.utils.Log;
@@ -146,9 +147,13 @@ public class HtmlImage implements Html.ImageGetter {
*/
private static void makeFreshCopy(final File file) {
final File tempFile = new File(file.getParentFile(), file.getName() + "-temp");
- file.renameTo(tempFile);
- LocalStorage.copy(tempFile, file);
- tempFile.delete();
+ if (file.renameTo(tempFile)) {
+ LocalStorage.copy(tempFile, file);
+ FileUtils.deleteIgnoringFailure(tempFile);
+ }
+ else {
+ Log.e("Could not reset timestamp of file " + file.getAbsolutePath());
+ }
}
private Bitmap getTransparent1x1Image() {
diff --git a/main/src/cgeo/geocaching/ui/GPXListAdapter.java b/main/src/cgeo/geocaching/ui/GPXListAdapter.java
index 7f3c33f..988bb81 100644
--- a/main/src/cgeo/geocaching/ui/GPXListAdapter.java
+++ b/main/src/cgeo/geocaching/ui/GPXListAdapter.java
@@ -5,6 +5,7 @@ import butterknife.InjectView;
import cgeo.geocaching.GpxFileListActivity;
import cgeo.geocaching.R;
import cgeo.geocaching.files.GPXImporter;
+import cgeo.geocaching.utils.FileUtils;
import cgeo.geocaching.utils.Log;
import android.app.Activity;
@@ -80,7 +81,7 @@ public class GPXListAdapter extends ArrayAdapter<File> {
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
- file.delete();
+ FileUtils.deleteIgnoringFailure(file);
GPXListAdapter.this.remove(file);
}
})
diff --git a/main/src/cgeo/geocaching/utils/FileUtils.java b/main/src/cgeo/geocaching/utils/FileUtils.java
index 0b358d2..fec2518 100644
--- a/main/src/cgeo/geocaching/utils/FileUtils.java
+++ b/main/src/cgeo/geocaching/utils/FileUtils.java
@@ -89,4 +89,26 @@ public final class FileUtils {
private static String getNumberedFileName(String pathName, String extension, int number) {
return pathName + (number > 1 ? "_" + Integer.toString(number) : "") + "." + extension;
}
+
+ /**
+ * This usage of this method indicates that the return value of File.delete() can safely be ignored.
+ */
+ public static void deleteIgnoringFailure(final File file) {
+ if (!file.delete()) {
+ Log.i("Could not delete " + file.getAbsolutePath());
+ }
+ }
+
+ /**
+ * Deletes a file and logs deletion failures.
+ *
+ * @return <code> true</code> if this file was deleted, <code>false</code> otherwise.
+ */
+ public static boolean delete(final File file) {
+ final boolean success = file.delete();
+ if (!success) {
+ Log.e("Could not delete " + file.getAbsolutePath());
+ }
+ return success;
+ }
}
diff --git a/main/src/cgeo/geocaching/utils/Log.java b/main/src/cgeo/geocaching/utils/Log.java
index f7f33d9..aa8dbd1 100644
--- a/main/src/cgeo/geocaching/utils/Log.java
+++ b/main/src/cgeo/geocaching/utils/Log.java
@@ -8,26 +8,30 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
-final public class Log {
+public final class Log {
private static final String TAG = "cgeo";
/**
- * the debug flag is cached here so that we don't need to access the settings every time we have to evaluate it
+ * The debug flag is cached here so that we don't need to access the settings every time we have to evaluate it.
*/
private static boolean isDebug = true;
private static boolean first = true;
+ private Log() {
+ // utility class
+ }
+
public static boolean isDebug() {
return isDebug;
}
/**
- * save a copy of the debug flag from the settings for performance reasons
- *
+ * Save a copy of the debug flag from the settings for performance reasons.
+ *
* @param isDebug
*/
- public static void setDebug(boolean isDebug) {
+ public static void setDebug(final boolean isDebug) {
Log.isDebug = isDebug;
}
@@ -95,11 +99,11 @@ final public class Log {
*
* @param msg the message to log, or to add to the log if other messages have been stored in the same run
*/
- public synchronized static void logToFile(final String msg) {
+ public static synchronized void logToFile(final String msg) {
final File file = new File(Environment.getExternalStorageDirectory(), "cgeo-debug.log");
if (first) {
first = false;
- file.delete();
+ FileUtils.delete(file);
}
Writer writer = null;
try {
diff --git a/main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java b/main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java
index 75c10ab..9207c74 100644
--- a/main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java
+++ b/main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java
@@ -20,7 +20,7 @@ public class SimpleCancellableHandler extends CancellableHandler {
}
@Override
- public void handleRegularMessage(final Message msg) {
+ protected void handleRegularMessage(final Message msg) {
AbstractActivity activity = activityRef.get();
if (activity != null && msg.getData() != null && msg.getData().getString(MESSAGE_TEXT) != null) {
activity.showToast(msg.getData().getString(MESSAGE_TEXT));
@@ -30,7 +30,7 @@ public class SimpleCancellableHandler extends CancellableHandler {
}
@Override
- public void handleCancel(final Object extra) {
+ protected void handleCancel(final Object extra) {
AbstractActivity activity = activityRef.get();
if (activity != null) {
activity.showToast((String) extra);
@@ -38,7 +38,7 @@ public class SimpleCancellableHandler extends CancellableHandler {
dismissProgress();
}
- public final void showToast(int resId) {
+ protected final void showToast(int resId) {
AbstractActivity activity = activityRef.get();
if (activity != null) {
Resources res = activity.getResources();
@@ -46,7 +46,7 @@ public class SimpleCancellableHandler extends CancellableHandler {
}
}
- public final void dismissProgress() {
+ protected final void dismissProgress() {
Progress progressDialog = progressDialogRef.get();
if (progressDialog != null) {
progressDialog.dismiss();