mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 09:33:06 +02:00
extract logic for creating DocumentFile for export to align naming
This commit is contained in:
+13
-13
@@ -38,7 +38,7 @@ import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.content.data.Track;
|
||||
import de.dennisguse.opentracks.content.data.TracksColumns;
|
||||
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
||||
import de.dennisguse.opentracks.io.file.ExportFileName;
|
||||
import de.dennisguse.opentracks.io.file.ExportFile;
|
||||
import de.dennisguse.opentracks.io.file.TrackFileFormat;
|
||||
import de.dennisguse.opentracks.io.file.exporter.TrackExporter;
|
||||
import de.dennisguse.opentracks.util.DialogUtils;
|
||||
@@ -209,32 +209,32 @@ public class ExportProgressDialogFragment extends DialogFragment {
|
||||
private Boolean exportTrack(Context context, Track track) {
|
||||
TrackExporter trackExporter = trackFileFormat.newTrackExporter(context);
|
||||
|
||||
String fileName = ExportFileName.getExportFileNameByTrackId(track.getId(), trackFileFormat.getExtension());
|
||||
DocumentFile exportDocumentFile = ExportFile.getExportDocumentFile(
|
||||
track.getId(),
|
||||
trackFileFormat.getExtension(),
|
||||
directory,
|
||||
trackFileFormat.getMimeType()
|
||||
);
|
||||
|
||||
// Overwrite a file if it exists; DocumentFile.createFile() creates a new file appending a suffix if the displayname already exists.
|
||||
DocumentFile file = directory.findFile(fileName);
|
||||
if (file == null) {
|
||||
file = directory.createFile(trackFileFormat.getMimeType(), fileName);
|
||||
}
|
||||
|
||||
try (OutputStream outputStream = context.getContentResolver().openOutputStream(file.getUri())) {
|
||||
try (OutputStream outputStream = context.getContentResolver().openOutputStream(exportDocumentFile.getUri())) {
|
||||
if (trackExporter.writeTrack(track, outputStream)) {
|
||||
return true;
|
||||
} else {
|
||||
if (!file.delete()) {
|
||||
Log.e(TAG, "Unable to delete file");
|
||||
if (!exportDocumentFile.delete()) {
|
||||
Log.e(TAG, "Unable to delete exportDocumentFile");
|
||||
}
|
||||
Log.e(TAG, "Unable to export track");
|
||||
return false;
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e(TAG, "Unable to open file " + file.getName(), e);
|
||||
Log.e(TAG, "Unable to open exportDocumentFile " + exportDocumentFile.getName(), e);
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Unable to close file output stream", e);
|
||||
Log.e(TAG, "Unable to close exportDocumentFile output stream", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public interface DismissCallback {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package de.dennisguse.opentracks.io.file;
|
||||
|
||||
import androidx.documentfile.provider.DocumentFile;
|
||||
|
||||
import de.dennisguse.opentracks.content.data.Track;
|
||||
|
||||
public class ExportFile {
|
||||
|
||||
public static DocumentFile getExportDocumentFile(Track.Id trackId, String trackFileFormatExtension, DocumentFile directory, String mimeType) {
|
||||
String fileName = getExportFileNameByTrackId(trackId, trackFileFormatExtension);
|
||||
DocumentFile file = directory.findFile(fileName);
|
||||
if (file == null) {
|
||||
file = directory.createFile(mimeType, fileName);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
private static String getExportFileNameByTrackId(Track.Id trackId, String trackFileFormatExtension) {
|
||||
return trackId.getId() + "." + trackFileFormatExtension;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package de.dennisguse.opentracks.io.file;
|
||||
|
||||
import de.dennisguse.opentracks.content.data.Track;
|
||||
|
||||
public class ExportFileName {
|
||||
|
||||
public static String getExportFileNameByTrackId(Track.Id trackId, String trackFileFormatExtension) {
|
||||
return trackId.getId() + "." + trackFileFormatExtension;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user