Skip to content

Commit c8774f1

Browse files
authored
Merge pull request #2 from Andyporras/share_zip
Code refactor
2 parents f82a6f3 + a2f5d59 commit c8774f1

4 files changed

Lines changed: 34 additions & 90 deletions

File tree

app/src/main/java/net/osmtracker/activity/TrackManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ private static void prepareAndShareTrack(final long trackId, Context context) {
582582
new ExportToTempFileTask(context, trackId){
583583
@Override
584584
protected void executionCompleted(){
585-
// Create zip file or an archive in case there are no files associated with the trace
585+
// Creates a zip file with the trace and its multimedia files
586586
File zipFile = ZipHelper.zipCacheFiles(context, trackId, this.getTmpFile());
587587
shareFile(zipFile, context);
588588
}

app/src/main/java/net/osmtracker/db/DataHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public class DataHelper {
4848
*/
4949
public static final String EXTENSION_JPG = ".jpg";
5050

51+
/**
52+
* ZIP file extension
53+
*/
54+
public static final String EXTENSION_ZIP = ".zip";
55+
5156
/**
5257
* GPX Files MIME standard for sharing
5358
*/

app/src/main/java/net/osmtracker/gpx/ExportToTempFileTask.java

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
package net.osmtracker.gpx;
22

3-
import android.content.ContentResolver;
4-
import android.content.ContentUris;
53
import android.content.Context;
64
import android.database.Cursor;
75
import android.preference.PreferenceManager;
86
import android.util.Log;
97

10-
import androidx.annotation.NonNull;
11-
128
import net.osmtracker.OSMTracker;
139
import net.osmtracker.db.DataHelper;
14-
import net.osmtracker.db.TrackContentProvider;
1510
import net.osmtracker.exception.ExportTrackException;
1611

1712
import java.io.File;
18-
import java.io.IOException;
1913
import java.util.Date;
2014

2115
/**
@@ -26,73 +20,30 @@
2620
public abstract class ExportToTempFileTask extends ExportTrackTask {
2721

2822
private static final String TAG = ExportToTempFileTask.class.getSimpleName();
29-
23+
3024
private final File tmpFile;
3125
private String filename;
32-
26+
3327
public ExportToTempFileTask(Context context, long trackId) {
3428
super(context, trackId);
29+
String desiredOutputFormat = PreferenceManager.getDefaultSharedPreferences(context).getString(
30+
OSMTracker.Preferences.KEY_OUTPUT_FILENAME,
31+
OSMTracker.Preferences.VAL_OUTPUT_FILENAME);
3532
try {
36-
String exportLabelName = PreferenceManager.getDefaultSharedPreferences(context).getString(
37-
OSMTracker.Preferences.KEY_OUTPUT_FILENAME_LABEL, OSMTracker.Preferences.VAL_OUTPUT_FILENAME_LABEL);
3833
String trackName = new DataHelper(context).getTrackById(trackId).getName();
39-
long date = new DataHelper(context).getTrackById(trackId).getTrackDate();
4034

41-
String formattedTrackStartDate = DataHelper.FILENAME_FORMATTER.format(new Date(date));
35+
long startDate = new DataHelper(context).getTrackById(trackId).getTrackDate();
36+
String formattedTrackStartDate = DataHelper.FILENAME_FORMATTER.format(new Date(startDate));
4237

4338
// Create temporary file
44-
String namefinal = createFile(trackName, formattedTrackStartDate, exportLabelName);
45-
tmpFile = new File(context.getCacheDir(),namefinal+".gpx");
39+
String tmpFilename = super.formatGpxFilename(desiredOutputFormat, trackName, formattedTrackStartDate);
40+
tmpFile = new File(context.getCacheDir(),tmpFilename + DataHelper.EXTENSION_GPX);
4641
Log.d(TAG, "Temporary file: "+ tmpFile.getAbsolutePath());
4742
} catch (Exception ioe) {
4843
Log.e(TAG, "Could not create temporary file", ioe);
4944
throw new IllegalStateException("Could not create temporary file", ioe);
5045
}
5146
}
52-
//create temporary file
53-
private String createFile(String sanitizedTrackName, String formattedTrackStartDate, String exportLabelName) throws IOException{
54-
String result = "";
55-
String desiredOutputFormat = PreferenceManager.getDefaultSharedPreferences(context).getString(
56-
OSMTracker.Preferences.KEY_OUTPUT_FILENAME,
57-
OSMTracker.Preferences.VAL_OUTPUT_FILENAME);
58-
59-
boolean thereIsTrackName = sanitizedTrackName != null && sanitizedTrackName.length() >= 1;
60-
switch(desiredOutputFormat){
61-
case OSMTracker.Preferences.VAL_OUTPUT_FILENAME_NAME:
62-
if(thereIsTrackName)
63-
result += sanitizedTrackName;
64-
else
65-
result += formattedTrackStartDate; // fallback case
66-
break;
67-
case OSMTracker.Preferences.VAL_OUTPUT_FILENAME_NAME_DATE:
68-
if(thereIsTrackName)
69-
if(sanitizedTrackName.equals(formattedTrackStartDate)) {
70-
result += sanitizedTrackName;
71-
}else{
72-
result += sanitizedTrackName + "_" + formattedTrackStartDate; // name is not equal
73-
}
74-
else
75-
result += formattedTrackStartDate;
76-
break;
77-
case OSMTracker.Preferences.VAL_OUTPUT_FILENAME_DATE_NAME:
78-
if(thereIsTrackName){
79-
if(sanitizedTrackName.equals(formattedTrackStartDate)){
80-
result += formattedTrackStartDate;
81-
}else{
82-
result += formattedTrackStartDate + "_" + sanitizedTrackName;
83-
}
84-
}else{
85-
result += formattedTrackStartDate;
86-
}
87-
break;
88-
case OSMTracker.Preferences.VAL_OUTPUT_FILENAME_DATE:
89-
result += formattedTrackStartDate;
90-
break;
91-
}
92-
if(!(exportLabelName.equals("")))
93-
result += "_"+ exportLabelName;
94-
return result;
95-
}
9647

9748
@Override
9849
protected File getExportDirectory(Date startDate) throws ExportTrackException {

app/src/main/java/net/osmtracker/gpx/ZipHelper.java

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
import java.io.File;
77
import java.io.FileInputStream;
8-
import java.io.FileNotFoundException;
98
import java.io.FileOutputStream;
109
import java.io.IOException;
10+
import java.util.Objects;
1111
import java.util.zip.ZipEntry;
1212
import java.util.zip.ZipOutputStream;
1313

@@ -26,46 +26,34 @@ public class ZipHelper {
2626
* @return The created ZIP file or null if an error occurred.
2727
*/
2828
public static File zipCacheFiles(Context context, long trackId, File fileGPX) {
29-
File directory = DataHelper.getTrackDirectory(trackId,context);
30-
if (!directory.exists() || !directory.isDirectory()) {
31-
String name = fileGPX.getName();
32-
File zipFile = new File(context.getCacheDir(), name.substring(0, name.length() - 3)+"zip");
33-
try (FileOutputStream fos = new FileOutputStream(zipFile);
34-
ZipOutputStream zos = new ZipOutputStream(fos)) {
35-
// Adds the original gpx file
36-
addFileToZip(fileGPX, zos);
37-
return zipFile;
38-
} catch (FileNotFoundException e) {
39-
throw new RuntimeException(e);
40-
} catch (IOException e) {
41-
throw new RuntimeException(e);
42-
}
43-
}
4429

45-
File[] files = directory.listFiles();
46-
if (files == null || files.length == 0) {
47-
Log.e(TAG, "There are no files to compress in: " + directory.getAbsolutePath());
48-
return null;
49-
}
5030
String name = fileGPX.getName();
31+
File zipFile = new File(context.getCacheDir(),
32+
name.substring(0, name.length() - 3) + DataHelper.EXTENSION_ZIP);
5133

52-
File zipFile = new File(context.getCacheDir(), name.substring(0, name.length() - 3)+"zip");
34+
File traceFilesDirectory = DataHelper.getTrackDirectory(trackId, context);
5335

5436
try (FileOutputStream fos = new FileOutputStream(zipFile);
5537
ZipOutputStream zos = new ZipOutputStream(fos)) {
5638

57-
for (File file : files) {
58-
if (!file.isDirectory()) { // Avoid adding empty folders
39+
// Add gpx file
40+
addFileToZip(fileGPX, zos);
41+
42+
if(!traceFilesDirectory.exists()){
43+
return zipFile;
44+
}
45+
for (File multimediaFile : Objects.requireNonNull(traceFilesDirectory.listFiles())) {
46+
if (!multimediaFile.isDirectory()) { // Avoid adding empty folders
5947
// only add files that are not .zip files
60-
if (!file.getName().endsWith(".zip")) {
61-
addFileToZip(file, zos);
62-
}else {
63-
Log.d(TAG, "No file is added: " + file.getAbsolutePath());
48+
if (!multimediaFile.getName().endsWith(DataHelper.EXTENSION_ZIP)) {
49+
addFileToZip(multimediaFile, zos);
50+
} else {
51+
Log.d(TAG, "Multimedia file: " + multimediaFile.getAbsolutePath() + " ignored. ");
6452
}
53+
} else {
54+
Log.d(TAG, "Folder " + multimediaFile.getAbsolutePath() + " ignored. ");
6555
}
6656
}
67-
// Add gpx file
68-
addFileToZip(fileGPX, zos);
6957

7058
Log.d(TAG, "ZIP file created: " + zipFile.getAbsolutePath());
7159
return zipFile;
@@ -122,4 +110,4 @@ private static void addFileToZip(File file, ZipOutputStream zos) throws IOExcept
122110
zos.closeEntry();
123111
}
124112
}
125-
}
113+
}

0 commit comments

Comments
 (0)