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:
Jimmy Shih
2012-03-16 10:44:08 -07:00
parent 8ee1066b9d
commit ddd06ea9f6
2 changed files with 16 additions and 12 deletions
@@ -90,14 +90,11 @@ public class ExportAllActivity extends Activity {
* Invokes when the associated AsyncTask completes. * Invokes when the associated AsyncTask completes.
* *
* @param success true if the AsyncTask is successful * @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); removeDialog(DIALOG_PROGRESS_ID);
if (success) { Toast.makeText(this, messageId, success ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG).show();
Toast.makeText(this, R.string.export_success, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, R.string.export_error, Toast.LENGTH_LONG).show();
}
finish(); finish();
} }
@@ -50,6 +50,9 @@ public class ExportAllAsyncTask extends AsyncTask<Void, Integer, Boolean> {
// true if the AsyncTask has completed // true if the AsyncTask has completed
private boolean completed; private boolean completed;
// message id to return to the activity
private int messageId;
/** /**
* Creates an AsyncTask. * Creates an AsyncTask.
* *
@@ -74,6 +77,7 @@ public class ExportAllAsyncTask extends AsyncTask<Void, Integer, Boolean> {
} }
success = false; success = false;
completed = 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) { public void setActivity(ExportAllActivity exportAllActivity) {
this.exportAllActivity = exportAllActivity; this.exportAllActivity = exportAllActivity;
if (completed && exportAllActivity != null) { if (completed && exportAllActivity != null) {
exportAllActivity.onAsyncTaskCompleted(success); exportAllActivity.onAsyncTaskCompleted(success, messageId);
} }
} }
@@ -101,6 +105,7 @@ public class ExportAllAsyncTask extends AsyncTask<Void, Integer, Boolean> {
try { try {
cursor = myTracksProviderUtils.getTracksCursor(null, null, TracksColumns._ID); cursor = myTracksProviderUtils.getTracksCursor(null, null, TracksColumns._ID);
if (cursor == null) { if (cursor == null) {
messageId = R.string.export_success;
return true; return true;
} }
int count = cursor.getCount(); int count = cursor.getCount();
@@ -119,15 +124,21 @@ public class ExportAllAsyncTask extends AsyncTask<Void, Integer, Boolean> {
trackWriter.writeTrack(); trackWriter.writeTrack();
if (!trackWriter.wasSuccess()) { if (!trackWriter.wasSuccess()) {
messageId = trackWriter.getErrorMessage();
return false; return false;
} }
publishProgress(i + 1, count); publishProgress(i + 1, count);
} }
messageId = R.string.export_success;
return true; return true;
} finally { } finally {
if (cursor != null) { if (cursor != null) {
cursor.close(); 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 @Override
protected void onPostExecute(Boolean result) { protected void onPostExecute(Boolean result) {
// Release the wake lock if obtained
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
}
success = result; success = result;
completed = true; completed = true;
if (exportAllActivity != null) { if (exportAllActivity != null) {
exportAllActivity.onAsyncTaskCompleted(success); exportAllActivity.onAsyncTaskCompleted(success, messageId);
} }
} }