aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
authorBananeweizen <Bananeweizen@gmx.de>2013-07-28 17:46:09 +0200
committerBananeweizen <Bananeweizen@gmx.de>2013-07-28 17:49:09 +0200
commit51e61f7b36a731ce7ed4087fbe38642687be0321 (patch)
tree418cc9b91f0b66df0141bd0a68a370afaee03abc /main/src
parent77e7e33e8956fa00b6e7f43be6c51177f4b6cf5b (diff)
downloadcgeo-51e61f7b36a731ce7ed4087fbe38642687be0321.zip
cgeo-51e61f7b36a731ce7ed4087fbe38642687be0321.tar.gz
cgeo-51e61f7b36a731ce7ed4087fbe38642687be0321.tar.bz2
change: make gpx export name more readable
* use only date instead of date and time * format date using dashes * append _2, _3, ... with further exports
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/export/GpxExport.java12
-rw-r--r--main/src/cgeo/geocaching/utils/FileUtils.java26
2 files changed, 35 insertions, 3 deletions
diff --git a/main/src/cgeo/geocaching/export/GpxExport.java b/main/src/cgeo/geocaching/export/GpxExport.java
index 61be3c5..c31b1ae 100644
--- a/main/src/cgeo/geocaching/export/GpxExport.java
+++ b/main/src/cgeo/geocaching/export/GpxExport.java
@@ -1,11 +1,12 @@
package cgeo.geocaching.export;
import cgeo.geocaching.Geocache;
-import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.R;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.activity.ActivityMixin;
+import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.utils.AsyncTaskWithProgress;
+import cgeo.geocaching.utils.FileUtils;
import cgeo.geocaching.utils.Log;
import android.app.Activity;
@@ -106,6 +107,12 @@ class GpxExport extends AbstractExport {
this.activity = activity;
}
+ private File getExportFile() {
+ final SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
+ final Date now = new Date();
+ return FileUtils.getUniqueNamedFile(Settings.getGpxExportDir() + File.separatorChar + "export_" + fileNameDateFormat.format(now) + ".gpx");
+ }
+
@Override
protected File doInBackgroundInternal(String[] geocodes) {
// quick check for being able to write the GPX file
@@ -117,8 +124,7 @@ class GpxExport extends AbstractExport {
setMessage(cgeoapplication.getInstance().getResources().getQuantityString(R.plurals.cache_counts, allGeocodes.size(), allGeocodes.size()));
- final SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
- final File exportFile = new File(Settings.getGpxExportDir() + File.separatorChar + "export_" + fileNameDateFormat.format(new Date()) + ".gpx");
+ final File exportFile = getExportFile();
BufferedWriter writer = null;
try {
final File exportLocation = new File(Settings.getGpxExportDir());
diff --git a/main/src/cgeo/geocaching/utils/FileUtils.java b/main/src/cgeo/geocaching/utils/FileUtils.java
index 5ab8fcc..9951af1 100644
--- a/main/src/cgeo/geocaching/utils/FileUtils.java
+++ b/main/src/cgeo/geocaching/utils/FileUtils.java
@@ -1,6 +1,7 @@
package cgeo.geocaching.utils;
import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.StringUtils;
import android.os.Handler;
import android.os.Message;
@@ -65,4 +66,29 @@ public final class FileUtils {
public abstract boolean shouldEnd();
}
+
+ /**
+ * Create a unique non existing file named like the given file name. If a file with the given name already exists,
+ * add a number as suffix to the file name.<br>
+ * Example: For the file name "file.ext" this will return the first file of the list
+ * <ul>
+ * <li>file.ext</li>
+ * <li>file_2.ext</li>
+ * <li>file_3.ext</li>
+ * </ul>
+ * which does not yet exist.
+ */
+ public static File getUniqueNamedFile(final String baseNameAndPath) {
+ String extension = StringUtils.substringAfterLast(baseNameAndPath, ".");
+ String pathName = StringUtils.substringBeforeLast(baseNameAndPath, ".");
+ int number = 1;
+ while (new File(getNumberedFileName(pathName, extension, number)).exists()) {
+ number++;
+ }
+ return new File(getNumberedFileName(pathName, extension, number));
+ }
+
+ private static String getNumberedFileName(String pathName, String extension, int number) {
+ return pathName + (number > 1 ? "_" + Integer.toString(number) : "") + "." + extension;
+ }
}