mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 09:33:06 +02:00
Improve cancelling async task when exporting tracks
This commit is contained in:
@@ -59,21 +59,6 @@ abstract class AbstractTrackExporter implements TrackExporter {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopWriteTrack() {
|
||||
if (writeThread != null && writeThread.isAlive()) {
|
||||
Log.i(TAG, "Attempting to stop write thread");
|
||||
writeThread.interrupt();
|
||||
try {
|
||||
writeThread.join();
|
||||
Log.i(TAG, "Write thread stopped");
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(TAG, "Interrupted while waiting for write thread to stop", e);
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
|
||||
@@ -86,7 +86,7 @@ public class FileTrackExporter extends AbstractTrackExporter {
|
||||
/**
|
||||
* Writes the waypoints.
|
||||
*/
|
||||
private void writeWaypoints(Track track) {
|
||||
private void writeWaypoints(Track track) throws InterruptedException {
|
||||
/*
|
||||
* TODO: Stream through the waypoints in chunks. I am leaving the number of
|
||||
* waypoints very high which should not be a problem because we don't try to
|
||||
@@ -103,6 +103,9 @@ public class FileTrackExporter extends AbstractTrackExporter {
|
||||
* first waypoint holds the stats for the track.
|
||||
*/
|
||||
while (cursor.moveToNext()) {
|
||||
if (Thread.interrupted()) {
|
||||
throw new InterruptedException();
|
||||
}
|
||||
if (!hasWaypoints) {
|
||||
trackWriter.writeBeginWaypoints();
|
||||
hasWaypoints = true;
|
||||
@@ -135,11 +138,12 @@ public class FileTrackExporter extends AbstractTrackExporter {
|
||||
try {
|
||||
int locationNumber = 0;
|
||||
while (iterator.hasNext()) {
|
||||
Location location = iterator.next();
|
||||
setLocationTime(location, offset);
|
||||
if (Thread.interrupted()) {
|
||||
throw new InterruptedException();
|
||||
}
|
||||
Location location = iterator.next();
|
||||
|
||||
setLocationTime(location, offset);
|
||||
locationNumber++;
|
||||
|
||||
boolean isLocationValid = LocationUtils.isValidLocation(location);
|
||||
|
||||
@@ -44,32 +44,26 @@ public class KmzTrackExporter extends AbstractTrackExporter {
|
||||
public static final String KML_FILE_NAME = "doc.kml";
|
||||
|
||||
private final MyTracksProviderUtils myTracksProviderUtils;
|
||||
private final TrackExporter trackExporter;
|
||||
private final FileTrackExporter fileTrackExporter;
|
||||
private final Track[] tracks;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param myTracksProviderUtils the my tracks provider utils
|
||||
* @param trackExporter the track exporter
|
||||
* @param fileTrackExporter the file track exporter
|
||||
* @param tracks the tracks to export
|
||||
*/
|
||||
public KmzTrackExporter(
|
||||
MyTracksProviderUtils myTracksProviderUtils, TrackExporter trackExporter, Track[] tracks) {
|
||||
public KmzTrackExporter(MyTracksProviderUtils myTracksProviderUtils,
|
||||
FileTrackExporter fileTrackExporter, Track[] tracks) {
|
||||
this.myTracksProviderUtils = myTracksProviderUtils;
|
||||
this.trackExporter = trackExporter;
|
||||
this.fileTrackExporter = fileTrackExporter;
|
||||
this.tracks = tracks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSuccess() {
|
||||
return trackExporter.isSuccess() && super.isSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopWriteTrack() {
|
||||
trackExporter.stopWriteTrack();
|
||||
super.stopWriteTrack();
|
||||
return fileTrackExporter.isSuccess() && super.isSuccess();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -83,9 +77,14 @@ public class KmzTrackExporter extends AbstractTrackExporter {
|
||||
zipOutputStream.putNextEntry(zipEntry);
|
||||
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
trackExporter.writeTrack(byteArrayOutputStream);
|
||||
zipOutputStream.write(byteArrayOutputStream.toByteArray());
|
||||
zipOutputStream.closeEntry();
|
||||
fileTrackExporter.writeTrack(byteArrayOutputStream);
|
||||
if (fileTrackExporter.isSuccess()) {
|
||||
zipOutputStream.write(byteArrayOutputStream.toByteArray());
|
||||
zipOutputStream.closeEntry();
|
||||
} else {
|
||||
zipOutputStream.closeEntry();
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
// Add photos
|
||||
addImages(zipOutputStream);
|
||||
@@ -96,7 +95,7 @@ public class KmzTrackExporter extends AbstractTrackExporter {
|
||||
}
|
||||
}
|
||||
|
||||
private void addImages(ZipOutputStream zipOutputStream) throws IOException {
|
||||
private void addImages(ZipOutputStream zipOutputStream) throws InterruptedException, IOException {
|
||||
for (Track track : tracks) {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
@@ -107,6 +106,9 @@ public class KmzTrackExporter extends AbstractTrackExporter {
|
||||
* the first waypoint holds the stats for the track.
|
||||
*/
|
||||
while (cursor.moveToNext()) {
|
||||
if (Thread.interrupted()) {
|
||||
throw new InterruptedException();
|
||||
}
|
||||
Waypoint waypoint = myTracksProviderUtils.createWaypoint(cursor);
|
||||
String photoUrl = waypoint.getPhotoUrl();
|
||||
if (photoUrl != null && !photoUrl.equals("")) {
|
||||
|
||||
@@ -121,7 +121,6 @@ public class SaveActivity extends Activity {
|
||||
public void onCancel(DialogInterface dialog) {
|
||||
saveAsyncTask.cancel(true);
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
}, directoryDisplayName);
|
||||
return progressDialog;
|
||||
|
||||
@@ -165,8 +165,9 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
|
||||
@Override
|
||||
protected void onCancelled() {
|
||||
if (trackExporter != null) {
|
||||
trackExporter.stopWriteTrack();
|
||||
completed = true;
|
||||
if (saveActivity != null) {
|
||||
saveActivity.onAsyncTaskCompleted(successCount, totalCount, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,12 +221,13 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
|
||||
if (trackExporter.isSuccess()) {
|
||||
savedPath = file.getAbsolutePath();
|
||||
return true;
|
||||
} else {
|
||||
if (!file.delete()) {
|
||||
Log.w(TAG, "Failed to delete file " + file.getAbsolutePath());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return trackExporter.isSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -32,11 +32,6 @@ public interface TrackExporter {
|
||||
*/
|
||||
void writeTrack(OutputStream outputStream);
|
||||
|
||||
/**
|
||||
* Stops {@link #writeTrack(OutputStream)}.
|
||||
*/
|
||||
void stopWriteTrack();
|
||||
|
||||
/**
|
||||
* Returns true if {@link #writeTrack(OutputStream)} completed successfully.
|
||||
*/
|
||||
|
||||
@@ -447,9 +447,13 @@ public class SyncUtils {
|
||||
fileTrackExporter.writeTrack(new FileOutputStream(file));
|
||||
if (fileTrackExporter.isSuccess()) {
|
||||
return file;
|
||||
} else {
|
||||
if (!file.delete()) {
|
||||
Log.d(TAG, "Unable to delete file for track " + track.getName());
|
||||
}
|
||||
Log.d(TAG, "Unable to get file for track " + track.getName());
|
||||
return null;
|
||||
}
|
||||
Log.d(TAG, "Unable to get file for track " + track.getName());
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user