Skip to content

Improved GPX Sharing: Share as ZIP #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/src/main/java/net/osmtracker/activity/TrackManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import net.osmtracker.exception.CreateTrackException;
import net.osmtracker.gpx.ExportToStorageTask;
import net.osmtracker.gpx.ExportToTempFileTask;
import net.osmtracker.gpx.ZipHelper;
import net.osmtracker.util.FileSystemUtils;

import java.io.File;
Expand Down Expand Up @@ -581,7 +582,9 @@ private static void prepareAndShareTrack(final long trackId, Context context) {
new ExportToTempFileTask(context, trackId){
@Override
protected void executionCompleted(){
shareFile(this.getTmpFile(), context);
// Creates a zip file with the trace and its multimedia files
File zipFile = ZipHelper.zipCacheFiles(context, trackId, this.getTmpFile());
shareFile(zipFile, context);
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/net/osmtracker/db/DataHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public class DataHelper {
*/
public static final String EXTENSION_JPG = ".jpg";

/**
* ZIP file extension
*/
public static final String EXTENSION_ZIP = ".zip";

/**
* GPX Files MIME standard for sharing
*/
Expand Down
64 changes: 8 additions & 56 deletions app/src/main/java/net/osmtracker/gpx/ExportToTempFileTask.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package net.osmtracker.gpx;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.util.Log;

import androidx.annotation.NonNull;

import net.osmtracker.OSMTracker;
import net.osmtracker.db.DataHelper;
import net.osmtracker.db.TrackContentProvider;
import net.osmtracker.exception.ExportTrackException;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
Expand All @@ -32,67 +26,25 @@ public abstract class ExportToTempFileTask extends ExportTrackTask {

public ExportToTempFileTask(Context context, long trackId) {
super(context, trackId);
String desiredOutputFormat = PreferenceManager.getDefaultSharedPreferences(context).getString(
OSMTracker.Preferences.KEY_OUTPUT_FILENAME,
OSMTracker.Preferences.VAL_OUTPUT_FILENAME);

try {
String exportLabelName = PreferenceManager.getDefaultSharedPreferences(context).getString(
OSMTracker.Preferences.KEY_OUTPUT_FILENAME_LABEL, OSMTracker.Preferences.VAL_OUTPUT_FILENAME_LABEL);
String trackName = new DataHelper(context).getTrackById(trackId).getName();
long date = new DataHelper(context).getTrackById(trackId).getStartDate();

String formattedTrackStartDate = DataHelper.FILENAME_FORMATTER.format(new Date(date));
long startDate = new DataHelper(context).getTrackById(trackId).getTrackDate();
String formattedTrackStartDate = DataHelper.FILENAME_FORMATTER.format(new Date(startDate));

// Create temporary file
String namefinal = createFile(trackName, formattedTrackStartDate, exportLabelName);
tmpFile = new File(context.getCacheDir(),namefinal+".gpx");
String tmpFilename = super.formatGpxFilename(desiredOutputFormat, trackName, formattedTrackStartDate);
tmpFile = new File(context.getCacheDir(),tmpFilename + DataHelper.EXTENSION_GPX);
Log.d(TAG, "Temporary file: "+ tmpFile.getAbsolutePath());
} catch (Exception ioe) {
Log.e(TAG, "Could not create temporary file", ioe);
throw new IllegalStateException("Could not create temporary file", ioe);
}
}
//create temporary file
private String createFile(String sanitizedTrackName, String formattedTrackStartDate, String exportLabelName) throws IOException{
String result = "";
String desiredOutputFormat = PreferenceManager.getDefaultSharedPreferences(context).getString(
OSMTracker.Preferences.KEY_OUTPUT_FILENAME,
OSMTracker.Preferences.VAL_OUTPUT_FILENAME);

boolean thereIsTrackName = sanitizedTrackName != null && sanitizedTrackName.length() >= 1;
switch(desiredOutputFormat){
case OSMTracker.Preferences.VAL_OUTPUT_FILENAME_NAME:
if(thereIsTrackName)
result += sanitizedTrackName;
else
result += formattedTrackStartDate; // fallback case
break;
case OSMTracker.Preferences.VAL_OUTPUT_FILENAME_NAME_DATE:
if(thereIsTrackName)
if(sanitizedTrackName.equals(formattedTrackStartDate)) {
result += sanitizedTrackName;
}else{
result += sanitizedTrackName + "_" + formattedTrackStartDate; // name is not equal
}
else
result += formattedTrackStartDate;
break;
case OSMTracker.Preferences.VAL_OUTPUT_FILENAME_DATE_NAME:
if(thereIsTrackName){
if(sanitizedTrackName.equals(formattedTrackStartDate)){
result += formattedTrackStartDate;
}else{
result += formattedTrackStartDate + "_" + sanitizedTrackName;
}
}else{
result += formattedTrackStartDate;
}
break;
case OSMTracker.Preferences.VAL_OUTPUT_FILENAME_DATE:
result += formattedTrackStartDate;
break;
}
if(!(exportLabelName.equals("")))
result += "_"+ exportLabelName;
return result;
}

@Override
protected File getExportDirectory(Date startDate) throws ExportTrackException {
Expand Down
94 changes: 94 additions & 0 deletions app/src/main/java/net/osmtracker/gpx/ZipHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package net.osmtracker.gpx;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import net.osmtracker.db.DataHelper;

public class ZipHelper {

private static final String TAG = "ZipHelper";

/**
* Compresses all files associated with a track into a ZIP file.
*
* @param context Application context.
* @param trackId Track ID.
* @param fileGPX GPX file.
* @return The created ZIP file or null if an error occurred.
*/
public static File zipCacheFiles(Context context, long trackId, File fileGPX) {

String name = fileGPX.getName();
File zipFile = new File(context.getCacheDir(),
name.substring(0, name.length() - 3) + DataHelper.EXTENSION_ZIP);

File traceFilesDirectory = DataHelper.getTrackDirectory(trackId, context);

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

// Add gpx file
addFileToZip(fileGPX, zos);

if(!traceFilesDirectory.exists()){
return zipFile;
}

for (File multimediaFile : Objects.requireNonNull(traceFilesDirectory.listFiles())) {
if (!multimediaFile.isDirectory()) { // Avoid adding empty folders
// only add files that are not .zip files
if (!multimediaFile.getName().endsWith(DataHelper.EXTENSION_ZIP)) {
addFileToZip(multimediaFile, zos);
} else {
Log.d(TAG, "Multimedia file: " + multimediaFile.getAbsolutePath() + " ignored. ");
}
} else {
Log.d(TAG, "Folder " + multimediaFile.getAbsolutePath() + " ignored. ");
}
}

Log.d(TAG, "ZIP file created: " + zipFile.getAbsolutePath());
return zipFile;

} catch (IOException e) {
Log.e(TAG, "Error creating ZIP file", e);
return null;
}
}


/**
* Adds a file to the ZIP archive.
*
* @param file The file to add.
* @param zos The ZipOutputStream to which the file will be added.
*/
private static void addFileToZip(File file, ZipOutputStream zos) throws IOException {
if (!file.exists()) {
Log.e(TAG, "File does not exist: " + file.getAbsolutePath());
return;
}

try (FileInputStream fis = new FileInputStream(file)) {
ZipEntry zipEntry = new ZipEntry(file.getName());
zos.putNextEntry(zipEntry);

byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}

zos.closeEntry();
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
<string name="trackmgr_contextmenu_stop">Stop tracking</string>
<string name="trackmgr_contextmenu_resume">Resume tracking</string>
<string name="trackmgr_contextmenu_delete">Delete</string>
<string name="trackmgr_contextmenu_export">Export as GPX</string>
<string name="trackmgr_contextmenu_share">Share GPX</string>
<string name="trackmgr_contextmenu_export">Export</string>
<string name="trackmgr_contextmenu_share">Share</string>
<string name="trackmgr_contextmenu_osm_upload">Upload to OpenStreetMap</string>
<string name="trackmgr_contextmenu_display">Display</string>
<string name="trackmgr_contextmenu_details">Details</string>
Expand Down