forked from upstream-mirrors/OpenTracks
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
|
@Override
|
||||||
public boolean isSuccess() {
|
public boolean isSuccess() {
|
||||||
return success;
|
return success;
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ public class FileTrackExporter extends AbstractTrackExporter {
|
|||||||
/**
|
/**
|
||||||
* Writes the waypoints.
|
* 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
|
* 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
|
* 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.
|
* first waypoint holds the stats for the track.
|
||||||
*/
|
*/
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
|
if (Thread.interrupted()) {
|
||||||
|
throw new InterruptedException();
|
||||||
|
}
|
||||||
if (!hasWaypoints) {
|
if (!hasWaypoints) {
|
||||||
trackWriter.writeBeginWaypoints();
|
trackWriter.writeBeginWaypoints();
|
||||||
hasWaypoints = true;
|
hasWaypoints = true;
|
||||||
@@ -135,11 +138,12 @@ public class FileTrackExporter extends AbstractTrackExporter {
|
|||||||
try {
|
try {
|
||||||
int locationNumber = 0;
|
int locationNumber = 0;
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
Location location = iterator.next();
|
|
||||||
setLocationTime(location, offset);
|
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
throw new InterruptedException();
|
throw new InterruptedException();
|
||||||
}
|
}
|
||||||
|
Location location = iterator.next();
|
||||||
|
|
||||||
|
setLocationTime(location, offset);
|
||||||
locationNumber++;
|
locationNumber++;
|
||||||
|
|
||||||
boolean isLocationValid = LocationUtils.isValidLocation(location);
|
boolean isLocationValid = LocationUtils.isValidLocation(location);
|
||||||
|
|||||||
@@ -44,32 +44,26 @@ public class KmzTrackExporter extends AbstractTrackExporter {
|
|||||||
public static final String KML_FILE_NAME = "doc.kml";
|
public static final String KML_FILE_NAME = "doc.kml";
|
||||||
|
|
||||||
private final MyTracksProviderUtils myTracksProviderUtils;
|
private final MyTracksProviderUtils myTracksProviderUtils;
|
||||||
private final TrackExporter trackExporter;
|
private final FileTrackExporter fileTrackExporter;
|
||||||
private final Track[] tracks;
|
private final Track[] tracks;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param myTracksProviderUtils the my tracks provider utils
|
* @param myTracksProviderUtils the my tracks provider utils
|
||||||
* @param trackExporter the track exporter
|
* @param fileTrackExporter the file track exporter
|
||||||
* @param tracks the tracks to export
|
* @param tracks the tracks to export
|
||||||
*/
|
*/
|
||||||
public KmzTrackExporter(
|
public KmzTrackExporter(MyTracksProviderUtils myTracksProviderUtils,
|
||||||
MyTracksProviderUtils myTracksProviderUtils, TrackExporter trackExporter, Track[] tracks) {
|
FileTrackExporter fileTrackExporter, Track[] tracks) {
|
||||||
this.myTracksProviderUtils = myTracksProviderUtils;
|
this.myTracksProviderUtils = myTracksProviderUtils;
|
||||||
this.trackExporter = trackExporter;
|
this.fileTrackExporter = fileTrackExporter;
|
||||||
this.tracks = tracks;
|
this.tracks = tracks;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isSuccess() {
|
public boolean isSuccess() {
|
||||||
return trackExporter.isSuccess() && super.isSuccess();
|
return fileTrackExporter.isSuccess() && super.isSuccess();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void stopWriteTrack() {
|
|
||||||
trackExporter.stopWriteTrack();
|
|
||||||
super.stopWriteTrack();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -83,9 +77,14 @@ public class KmzTrackExporter extends AbstractTrackExporter {
|
|||||||
zipOutputStream.putNextEntry(zipEntry);
|
zipOutputStream.putNextEntry(zipEntry);
|
||||||
|
|
||||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
trackExporter.writeTrack(byteArrayOutputStream);
|
fileTrackExporter.writeTrack(byteArrayOutputStream);
|
||||||
zipOutputStream.write(byteArrayOutputStream.toByteArray());
|
if (fileTrackExporter.isSuccess()) {
|
||||||
zipOutputStream.closeEntry();
|
zipOutputStream.write(byteArrayOutputStream.toByteArray());
|
||||||
|
zipOutputStream.closeEntry();
|
||||||
|
} else {
|
||||||
|
zipOutputStream.closeEntry();
|
||||||
|
throw new IOException();
|
||||||
|
}
|
||||||
|
|
||||||
// Add photos
|
// Add photos
|
||||||
addImages(zipOutputStream);
|
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) {
|
for (Track track : tracks) {
|
||||||
Cursor cursor = null;
|
Cursor cursor = null;
|
||||||
try {
|
try {
|
||||||
@@ -107,6 +106,9 @@ public class KmzTrackExporter extends AbstractTrackExporter {
|
|||||||
* the first waypoint holds the stats for the track.
|
* the first waypoint holds the stats for the track.
|
||||||
*/
|
*/
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
|
if (Thread.interrupted()) {
|
||||||
|
throw new InterruptedException();
|
||||||
|
}
|
||||||
Waypoint waypoint = myTracksProviderUtils.createWaypoint(cursor);
|
Waypoint waypoint = myTracksProviderUtils.createWaypoint(cursor);
|
||||||
String photoUrl = waypoint.getPhotoUrl();
|
String photoUrl = waypoint.getPhotoUrl();
|
||||||
if (photoUrl != null && !photoUrl.equals("")) {
|
if (photoUrl != null && !photoUrl.equals("")) {
|
||||||
|
|||||||
@@ -121,7 +121,6 @@ public class SaveActivity extends Activity {
|
|||||||
public void onCancel(DialogInterface dialog) {
|
public void onCancel(DialogInterface dialog) {
|
||||||
saveAsyncTask.cancel(true);
|
saveAsyncTask.cancel(true);
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
finish();
|
|
||||||
}
|
}
|
||||||
}, directoryDisplayName);
|
}, directoryDisplayName);
|
||||||
return progressDialog;
|
return progressDialog;
|
||||||
|
|||||||
@@ -165,8 +165,9 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCancelled() {
|
protected void onCancelled() {
|
||||||
if (trackExporter != null) {
|
completed = true;
|
||||||
trackExporter.stopWriteTrack();
|
if (saveActivity != null) {
|
||||||
|
saveActivity.onAsyncTaskCompleted(successCount, totalCount, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,12 +221,13 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
|
|
||||||
if (trackExporter.isSuccess()) {
|
if (trackExporter.isSuccess()) {
|
||||||
savedPath = file.getAbsolutePath();
|
savedPath = file.getAbsolutePath();
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
if (!file.delete()) {
|
if (!file.delete()) {
|
||||||
Log.w(TAG, "Failed to delete file " + file.getAbsolutePath());
|
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);
|
void writeTrack(OutputStream outputStream);
|
||||||
|
|
||||||
/**
|
|
||||||
* Stops {@link #writeTrack(OutputStream)}.
|
|
||||||
*/
|
|
||||||
void stopWriteTrack();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if {@link #writeTrack(OutputStream)} completed successfully.
|
* Returns true if {@link #writeTrack(OutputStream)} completed successfully.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -447,9 +447,13 @@ public class SyncUtils {
|
|||||||
fileTrackExporter.writeTrack(new FileOutputStream(file));
|
fileTrackExporter.writeTrack(new FileOutputStream(file));
|
||||||
if (fileTrackExporter.isSuccess()) {
|
if (fileTrackExporter.isSuccess()) {
|
||||||
return file;
|
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