From fc32c233b8a4f52e948d6d09c029a73c4a7a7a15 Mon Sep 17 00:00:00 2001 From: Bananeweizen Date: Fri, 29 Mar 2013 10:02:08 +0100 Subject: #1798: refactoring, extract common asynctask with progress --- .../geocaching/utils/AsyncTaskWithProgress.java | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 main/src/cgeo/geocaching/utils/AsyncTaskWithProgress.java (limited to 'main/src/cgeo/geocaching/utils/AsyncTaskWithProgress.java') 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 + * @param + */ +public abstract class AsyncTaskWithProgress extends AsyncTask { + + 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); + } +} -- cgit v1.1