forked from upstream-mirrors/OpenTracks
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.Track;
|
||||||
import de.dennisguse.opentracks.content.data.TracksColumns;
|
import de.dennisguse.opentracks.content.data.TracksColumns;
|
||||||
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
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.TrackFileFormat;
|
||||||
import de.dennisguse.opentracks.io.file.exporter.TrackExporter;
|
import de.dennisguse.opentracks.io.file.exporter.TrackExporter;
|
||||||
import de.dennisguse.opentracks.util.DialogUtils;
|
import de.dennisguse.opentracks.util.DialogUtils;
|
||||||
@@ -209,32 +209,32 @@ public class ExportProgressDialogFragment extends DialogFragment {
|
|||||||
private Boolean exportTrack(Context context, Track track) {
|
private Boolean exportTrack(Context context, Track track) {
|
||||||
TrackExporter trackExporter = trackFileFormat.newTrackExporter(context);
|
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.
|
try (OutputStream outputStream = context.getContentResolver().openOutputStream(exportDocumentFile.getUri())) {
|
||||||
DocumentFile file = directory.findFile(fileName);
|
|
||||||
if (file == null) {
|
|
||||||
file = directory.createFile(trackFileFormat.getMimeType(), fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
try (OutputStream outputStream = context.getContentResolver().openOutputStream(file.getUri())) {
|
|
||||||
if (trackExporter.writeTrack(track, outputStream)) {
|
if (trackExporter.writeTrack(track, outputStream)) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
if (!file.delete()) {
|
if (!exportDocumentFile.delete()) {
|
||||||
Log.e(TAG, "Unable to delete file");
|
Log.e(TAG, "Unable to delete exportDocumentFile");
|
||||||
}
|
}
|
||||||
Log.e(TAG, "Unable to export track");
|
Log.e(TAG, "Unable to export track");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} catch (FileNotFoundException e) {
|
} 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;
|
return false;
|
||||||
} catch (IOException e) {
|
} 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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface DismissCallback {
|
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