forked from upstream-mirrors/OpenTracks
Export: files are now overwritten.
This commit is contained in:
@@ -104,7 +104,7 @@ public class ExportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
if (!isRecording || isPaused) {
|
if (!isRecording || isPaused) {
|
||||||
wakeLock = SystemUtils.acquireWakeLock(exportActivity, wakeLock);
|
wakeLock = SystemUtils.acquireWakeLock(exportActivity, wakeLock);
|
||||||
}
|
}
|
||||||
return saveAllTracks();
|
return exportAllTracks();
|
||||||
} finally {
|
} finally {
|
||||||
if (wakeLock != null && wakeLock.isHeld()) {
|
if (wakeLock != null && wakeLock.isHeld()) {
|
||||||
wakeLock.release();
|
wakeLock.release();
|
||||||
@@ -136,15 +136,11 @@ public class ExportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves tracks to one file (uses first track to determine the filename).
|
* Export the track to one file.
|
||||||
*
|
*
|
||||||
* @param tracks the tracks
|
* @param track the track
|
||||||
*/
|
*/
|
||||||
private Boolean saveTracks(Track[] tracks) {
|
private Boolean exportTrack(Track track) {
|
||||||
if (tracks.length == 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
TrackExporterListener trackExporterListener = new TrackExporterListener() {
|
TrackExporterListener trackExporterListener = new TrackExporterListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onProgressUpdate(int number, int max) {
|
public void onProgressUpdate(int number, int max) {
|
||||||
@@ -155,11 +151,15 @@ public class ExportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
TrackExporter trackExporter = trackFileFormat.newTrackExporter(context, tracks, trackExporterListener);
|
TrackExporter trackExporter = trackFileFormat.newTrackExporter(context, new Track[]{track}, trackExporterListener);
|
||||||
Track track = tracks[0];
|
|
||||||
|
|
||||||
String fileName = track.getId() + "." + trackFileFormat.getExtension();
|
String fileName = track.getId() + "." + trackFileFormat.getExtension();
|
||||||
DocumentFile file = directory.createFile(trackFileFormat.getMimeType(), fileName);
|
|
||||||
|
// 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(file.getUri())) {
|
||||||
if (trackExporter.writeTrack(context, outputStream)) {
|
if (trackExporter.writeTrack(context, outputStream)) {
|
||||||
@@ -183,7 +183,7 @@ public class ExportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
/**
|
/**
|
||||||
* Saves all the tracks.
|
* Saves all the tracks.
|
||||||
*/
|
*/
|
||||||
private Boolean saveAllTracks() {
|
private Boolean exportAllTracks() {
|
||||||
try (Cursor cursor = contentProviderUtils.getTrackCursor(null, null, TracksColumns._ID)) {
|
try (Cursor cursor = contentProviderUtils.getTrackCursor(null, null, TracksColumns._ID)) {
|
||||||
if (cursor == null) {
|
if (cursor == null) {
|
||||||
return false;
|
return false;
|
||||||
@@ -195,7 +195,7 @@ public class ExportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
}
|
}
|
||||||
cursor.moveToPosition(i);
|
cursor.moveToPosition(i);
|
||||||
Track track = contentProviderUtils.createTrack(cursor);
|
Track track = contentProviderUtils.createTrack(cursor);
|
||||||
if (track != null && saveTracks(new Track[]{track})) {
|
if (track != null && exportTrack(track)) {
|
||||||
processedTrackCount++;
|
processedTrackCount++;
|
||||||
}
|
}
|
||||||
publishProgress(i + 1, totalTrackCount);
|
publishProgress(i + 1, totalTrackCount);
|
||||||
|
|||||||
Reference in New Issue
Block a user