mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 17:43:06 +02:00
Improve ExportAllAsyncTask
- if an error occurs, the toast should display the error messageId from trackWriter.getErrorMessage() if available - when user cancels export, onPostExecute is not invoked, thus need to release the wake lock in doInBackground
This commit is contained in:
@@ -90,14 +90,11 @@ public class ExportAllActivity extends Activity {
|
||||
* Invokes when the associated AsyncTask completes.
|
||||
*
|
||||
* @param success true if the AsyncTask is successful
|
||||
* @param messageId message id to display to user
|
||||
*/
|
||||
public void onAsyncTaskCompleted(boolean success) {
|
||||
public void onAsyncTaskCompleted(boolean success, int messageId) {
|
||||
removeDialog(DIALOG_PROGRESS_ID);
|
||||
if (success) {
|
||||
Toast.makeText(this, R.string.export_success, Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
Toast.makeText(this, R.string.export_error, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
Toast.makeText(this, messageId, success ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG).show();
|
||||
finish();
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +50,9 @@ public class ExportAllAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
// true if the AsyncTask has completed
|
||||
private boolean completed;
|
||||
|
||||
// message id to return to the activity
|
||||
private int messageId;
|
||||
|
||||
/**
|
||||
* Creates an AsyncTask.
|
||||
*
|
||||
@@ -74,6 +77,7 @@ public class ExportAllAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
}
|
||||
success = false;
|
||||
completed = false;
|
||||
messageId = R.string.export_error;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,7 +88,7 @@ public class ExportAllAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
public void setActivity(ExportAllActivity exportAllActivity) {
|
||||
this.exportAllActivity = exportAllActivity;
|
||||
if (completed && exportAllActivity != null) {
|
||||
exportAllActivity.onAsyncTaskCompleted(success);
|
||||
exportAllActivity.onAsyncTaskCompleted(success, messageId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +105,7 @@ public class ExportAllAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
try {
|
||||
cursor = myTracksProviderUtils.getTracksCursor(null, null, TracksColumns._ID);
|
||||
if (cursor == null) {
|
||||
messageId = R.string.export_success;
|
||||
return true;
|
||||
}
|
||||
int count = cursor.getCount();
|
||||
@@ -119,15 +124,21 @@ public class ExportAllAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
trackWriter.writeTrack();
|
||||
|
||||
if (!trackWriter.wasSuccess()) {
|
||||
messageId = trackWriter.getErrorMessage();
|
||||
return false;
|
||||
}
|
||||
publishProgress(i + 1, count);
|
||||
}
|
||||
messageId = R.string.export_success;
|
||||
return true;
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
// Release the wake lock if obtained
|
||||
if (wakeLock != null && wakeLock.isHeld()) {
|
||||
wakeLock.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,14 +151,10 @@ public class ExportAllAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result) {
|
||||
// Release the wake lock if obtained
|
||||
if (wakeLock != null && wakeLock.isHeld()) {
|
||||
wakeLock.release();
|
||||
}
|
||||
success = result;
|
||||
completed = true;
|
||||
if (exportAllActivity != null) {
|
||||
exportAllActivity.onAsyncTaskCompleted(success);
|
||||
exportAllActivity.onAsyncTaskCompleted(success, messageId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user