diff options
| author | Bananeweizen <Bananeweizen@gmx.de> | 2013-03-29 10:02:08 +0100 |
|---|---|---|
| committer | Bananeweizen <Bananeweizen@gmx.de> | 2013-03-29 10:02:08 +0100 |
| commit | fc32c233b8a4f52e948d6d09c029a73c4a7a7a15 (patch) | |
| tree | f0f0df5966ea155c48285d20e12f4b57e2fd2d6c /main/src/cgeo/geocaching/utils/AsyncTaskWithProgress.java | |
| parent | b3c74231b10ad15ed00b4107cf9f456c74978d51 (diff) | |
| download | cgeo-fc32c233b8a4f52e948d6d09c029a73c4a7a7a15.zip cgeo-fc32c233b8a4f52e948d6d09c029a73c4a7a7a15.tar.gz cgeo-fc32c233b8a4f52e948d6d09c029a73c4a7a7a15.tar.bz2 | |
#1798: refactoring, extract common asynctask with progress
Diffstat (limited to 'main/src/cgeo/geocaching/utils/AsyncTaskWithProgress.java')
| -rw-r--r-- | main/src/cgeo/geocaching/utils/AsyncTaskWithProgress.java | 80 |
1 files changed, 80 insertions, 0 deletions
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); + } +} |
