From ddd06ea9f6a8b9507d6e67a2b398f6318c8ae747 Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Fri, 16 Mar 2012 10:44:08 -0700 Subject: [PATCH] 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 --- .../apps/mytracks/ExportAllActivity.java | 9 +++------ .../apps/mytracks/ExportAllAsyncTask.java | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/ExportAllActivity.java b/MyTracks/src/com/google/android/apps/mytracks/ExportAllActivity.java index 548da9328..8ff2ebb5b 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/ExportAllActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/ExportAllActivity.java @@ -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(); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/ExportAllAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/ExportAllAsyncTask.java index 96028cd4c..06bf1e1f0 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/ExportAllAsyncTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/ExportAllAsyncTask.java @@ -50,6 +50,9 @@ public class ExportAllAsyncTask extends AsyncTask { // 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 { } success = false; completed = false; + messageId = R.string.export_error; } /** @@ -84,7 +88,7 @@ public class ExportAllAsyncTask extends AsyncTask { 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 { 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 { 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 { @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); } }