aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBananeweizen <Bananeweizen@gmx.de>2013-03-29 10:02:08 +0100
committerBananeweizen <Bananeweizen@gmx.de>2013-03-29 10:02:08 +0100
commitfc32c233b8a4f52e948d6d09c029a73c4a7a7a15 (patch)
treef0f0df5966ea155c48285d20e12f4b57e2fd2d6c
parentb3c74231b10ad15ed00b4107cf9f456c74978d51 (diff)
downloadcgeo-fc32c233b8a4f52e948d6d09c029a73c4a7a7a15.zip
cgeo-fc32c233b8a4f52e948d6d09c029a73c4a7a7a15.tar.gz
cgeo-fc32c233b8a4f52e948d6d09c029a73c4a7a7a15.tar.bz2
#1798: refactoring, extract common asynctask with progress
-rw-r--r--main/src/cgeo/geocaching/export/AbstractExport.java7
-rw-r--r--main/src/cgeo/geocaching/export/FieldnoteExport.java23
-rw-r--r--main/src/cgeo/geocaching/export/GpxExport.java25
-rw-r--r--main/src/cgeo/geocaching/utils/AsyncTaskWithProgress.java80
4 files changed, 98 insertions, 37 deletions
diff --git a/main/src/cgeo/geocaching/export/AbstractExport.java b/main/src/cgeo/geocaching/export/AbstractExport.java
index 72ea544..e4ba5f0 100644
--- a/main/src/cgeo/geocaching/export/AbstractExport.java
+++ b/main/src/cgeo/geocaching/export/AbstractExport.java
@@ -1,5 +1,6 @@
package cgeo.geocaching.export;
+import cgeo.geocaching.R;
import cgeo.geocaching.cgeoapplication;
abstract class AbstractExport implements Export {
@@ -27,7 +28,7 @@ abstract class AbstractExport implements Export {
/**
* Generates a localized string from a resource id.
- *
+ *
* @param resourceId
* the resource id of the string
* @param params
@@ -43,4 +44,8 @@ abstract class AbstractExport implements Export {
// used in the array adapter of the dialog showing the exports
return getName();
}
+
+ protected String getProgressTitle() {
+ return getString(R.string.export) + ": " + getName();
+ }
}
diff --git a/main/src/cgeo/geocaching/export/FieldnoteExport.java b/main/src/cgeo/geocaching/export/FieldnoteExport.java
index 5e1805a..eadcb77 100644
--- a/main/src/cgeo/geocaching/export/FieldnoteExport.java
+++ b/main/src/cgeo/geocaching/export/FieldnoteExport.java
@@ -5,11 +5,11 @@ import cgeo.geocaching.LogEntry;
import cgeo.geocaching.R;
import cgeo.geocaching.cgData;
import cgeo.geocaching.activity.ActivityMixin;
-import cgeo.geocaching.activity.Progress;
import cgeo.geocaching.connector.gc.Login;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
+import cgeo.geocaching.utils.AsyncTaskWithProgress;
import cgeo.geocaching.utils.IOUtils;
import cgeo.geocaching.utils.Log;
@@ -20,7 +20,6 @@ import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
-import android.os.AsyncTask;
import android.os.Environment;
import android.view.ContextThemeWrapper;
import android.view.View;
@@ -102,18 +101,17 @@ class FieldnoteExport extends AbstractExport {
return builder.create();
}
- private class ExportTask extends AsyncTask<Void, Integer, Boolean> {
+ private class ExportTask extends AsyncTaskWithProgress<Void, Boolean> {
private final List<Geocache> caches;
private final Activity activity;
private final boolean upload;
private final boolean onlyNew;
- private final Progress progress = new Progress();
private File exportFile;
private static final int STATUS_UPLOAD = -1;
/**
- * Instantiates and configurates the task for exporting field notes.
+ * Instantiates and configures the task for exporting field notes.
*
* @param caches
* The {@link List} of {@link cgeo.geocaching.Geocache} to be exported
@@ -125,6 +123,7 @@ class FieldnoteExport extends AbstractExport {
* Upload/export only new logs since last export
*/
public ExportTask(final List<Geocache> caches, final Activity activity, final boolean upload, final boolean onlyNew) {
+ super(activity, getProgressTitle(), getString(R.string.export_fieldnotes_creating));
this.caches = caches;
this.activity = activity;
this.upload = upload;
@@ -132,13 +131,6 @@ class FieldnoteExport extends AbstractExport {
}
@Override
- protected void onPreExecute() {
- if (null != activity) {
- progress.show(activity, getString(R.string.export) + ": " + getName(), getString(R.string.export_fieldnotes_creating), true, null);
- }
- }
-
- @Override
protected Boolean doInBackground(Void... params) {
final StringBuilder fieldNoteBuffer = new StringBuilder();
try {
@@ -228,9 +220,8 @@ class FieldnoteExport extends AbstractExport {
@Override
protected void onPostExecute(Boolean result) {
+ super.onPostExecute(result);
if (null != activity) {
- progress.dismiss();
-
if (result) {
// if (onlyNew) {
// // update last export time in settings when doing it ourself (currently we use the date check from gc.com)
@@ -251,9 +242,9 @@ class FieldnoteExport extends AbstractExport {
protected void onProgressUpdate(Integer... status) {
if (null != activity) {
if (STATUS_UPLOAD == status[0]) {
- progress.setMessage(getString(R.string.export_fieldnotes_uploading));
+ setMessage(getString(R.string.export_fieldnotes_uploading));
} else {
- progress.setMessage(getString(R.string.export_fieldnotes_creating) + " (" + status[0] + ')');
+ setMessage(getString(R.string.export_fieldnotes_creating) + " (" + status[0] + ')');
}
}
}
diff --git a/main/src/cgeo/geocaching/export/GpxExport.java b/main/src/cgeo/geocaching/export/GpxExport.java
index c4060fe..0ae9037 100644
--- a/main/src/cgeo/geocaching/export/GpxExport.java
+++ b/main/src/cgeo/geocaching/export/GpxExport.java
@@ -6,11 +6,12 @@ import cgeo.geocaching.R;
import cgeo.geocaching.Settings;
import cgeo.geocaching.Waypoint;
import cgeo.geocaching.cgData;
+import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.activity.ActivityMixin;
-import cgeo.geocaching.activity.Progress;
import cgeo.geocaching.enumerations.CacheAttribute;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.geopoint.Geopoint;
+import cgeo.geocaching.utils.AsyncTaskWithProgress;
import cgeo.geocaching.utils.BaseUtils;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.XmlUtils;
@@ -21,11 +22,9 @@ import org.xmlpull.v1.XmlSerializer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
-import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
-import android.os.AsyncTask;
import android.os.Environment;
import android.util.Xml;
import android.view.ContextThemeWrapper;
@@ -98,10 +97,9 @@ class GpxExport extends AbstractExport {
return builder.create();
}
- private class ExportTask extends AsyncTask<Void, Integer, File> {
+ private class ExportTask extends AsyncTaskWithProgress<Void, File> {
private final List<Geocache> caches;
private final Activity activity;
- private final Progress progress = new Progress();
/**
* Instantiates and configures the task for exporting field notes.
@@ -112,19 +110,12 @@ class GpxExport extends AbstractExport {
* optional: Show a progress bar and toasts
*/
public ExportTask(final List<Geocache> caches, final Activity activity) {
+ super(activity, caches.size(), getProgressTitle(), cgeoapplication.getInstance().getResources().getQuantityString(R.plurals.cache_counts, caches.size(), caches.size()));
this.caches = caches;
this.activity = activity;
}
@Override
- protected void onPreExecute() {
- if (null != activity) {
- progress.show(activity, null, getString(R.string.export) + ": " + getName(), ProgressDialog.STYLE_HORIZONTAL, null);
- progress.setMaxProgressAndReset(caches.size());
- }
- }
-
- @Override
protected File doInBackground(Void... params) {
// quick check for being able to write the GPX file
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
@@ -344,8 +335,8 @@ class GpxExport extends AbstractExport {
@Override
protected void onPostExecute(final File exportFile) {
+ super.onPostExecute(exportFile);
if (null != activity) {
- progress.dismiss();
if (exportFile != null) {
ActivityMixin.showToast(activity, getName() + ' ' + getString(R.string.export_exportedto) + ": " + exportFile.toString());
if (Settings.getShareAfterExport()) {
@@ -361,11 +352,5 @@ class GpxExport extends AbstractExport {
}
}
- @Override
- protected void onProgressUpdate(Integer... status) {
- if (null != activity) {
- progress.setProgress(status[0]);
- }
- }
}
}
diff --git a/main/src/cgeo/geocaching/utils/AsyncTaskWithProgress.java b/main/src/cgeo/geocaching/utils/AsyncTaskWithProgress.java
new file mode 100644
index 0000000..b23fa9d
--- /dev/null
+++ b/main/src/cgeo/geocaching/utils/AsyncTaskWithProgress.java
@@ -0,0 +1,80 @@
+package cgeo.geocaching.utils;
+
+import cgeo.geocaching.activity.Progress;
+
+import android.app.Activity;
+import android.app.ProgressDialog;
+import android.os.AsyncTask;
+
+/**
+ * AsyncTask which automatically shows a progress dialog. Use it like the {@code AsyncTask} class, but leave away the
+ * middle template parameter.
+ *
+ * @param <Params>
+ * @param <Result>
+ */
+public abstract class AsyncTaskWithProgress<Params, Result> extends AsyncTask<Params, Integer, Result> {
+
+ private final Progress progress = new Progress();
+ private final Activity activity;
+ private final int maxProgress;
+ private final String progressTitle;
+ private final String progressMessage;
+
+ /**
+ * Creates an AsyncTask with progress dialog, where the maximum is set to the given maxProgress.
+ *
+ * @param activity
+ * @param maxProgress
+ * @param progressTitle
+ * @param progressMessage
+ */
+ public AsyncTaskWithProgress(final Activity activity, final int maxProgress, final String progressTitle, final String progressMessage) {
+ this.activity = activity;
+ this.maxProgress = maxProgress;
+ this.progressTitle = progressTitle;
+ this.progressMessage = progressMessage;
+ }
+
+ /**
+ * Creates an AsyncTask with progress dialog, where the maximum is set to indeterminate.
+ *
+ * @param activity
+ * @param progressTitle
+ * @param progressMessage
+ */
+ public AsyncTaskWithProgress(final Activity activity, final String progressTitle, final String progressMessage) {
+ this(activity, 0, progressTitle, progressMessage);
+ }
+
+ @Override
+ protected void onPreExecute() {
+ if (null != activity) {
+ if (maxProgress <= 0) {
+ progress.show(activity, progressTitle, progressMessage, true, null);
+ }
+ else {
+ progress.show(activity, progressTitle, progressMessage, ProgressDialog.STYLE_HORIZONTAL, null);
+ }
+ progress.setMaxProgressAndReset(maxProgress);
+ }
+ }
+
+ @Override
+ protected void onPostExecute(Result result) {
+ if (null != activity) {
+ progress.dismiss();
+ }
+ }
+
+ @Override
+ protected void onProgressUpdate(Integer... status) {
+ if (null != activity) {
+ progress.setProgress(status[0]);
+ }
+ }
+
+ protected void setMessage(final String message) {
+ progress.setMessage(message);
+ }
+}