aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/utils/AsyncTaskWithProgress.java
blob: 29aea63f854ae7e8cbb344f74626780ec67553ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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. Override {@link #doInBackgroundInternal(Object[])} and related methods.
 * <p>
 * If no style is given, the progress dialog uses "determinate" style with known maximum. The progress maximum is
 * automatically derived from the number of {@code Params} given to the task in {@link #execute(Object...)}.
 * </p>
 *
 */
public abstract class AsyncTaskWithProgress<Params, Result> extends AsyncTask<Params, Integer, Result> {

    private final Progress progress = new Progress();
    private final Activity activity;
    private final String progressTitle;
    private final String progressMessage;
    private boolean indeterminate = false;

    /**
     * Creates an AsyncTask with progress dialog.
     *
     */
    public AsyncTaskWithProgress(final Activity activity, final String progressTitle, final String progressMessage) {
        this(activity, progressTitle, progressMessage, false);
    }

    /**
     * Creates an AsyncTask with progress dialog.
     *
     */
    public AsyncTaskWithProgress(final Activity activity, final String progressTitle) {
        this(activity, progressTitle, null);
    }

    /**
     * Creates an AsyncTask with progress dialog.
     *
     */
    public AsyncTaskWithProgress(final Activity activity, final String progressTitle, final String progressMessage, final boolean indeterminate) {
        this.activity = activity;
        this.progressTitle = progressTitle;
        this.progressMessage = progressMessage;
        this.indeterminate = indeterminate;
    }

    /**
     * Creates an AsyncTask with progress dialog.
     *
     */
    public AsyncTaskWithProgress(final Activity activity, final String progressTitle, final boolean indeterminate) {
        this(activity, progressTitle, null, indeterminate);
    }

    @Override
    protected final void onPreExecute() {
        if (null != activity) {
            if (indeterminate) {
                progress.show(activity, progressTitle, progressMessage, true, null);
            }
            else {
                progress.show(activity, progressTitle, progressMessage, ProgressDialog.STYLE_HORIZONTAL, null);
            }
        }
        onPreExecuteInternal();
    }

    /**
     * This method should typically be overridden by sub classes instead of {@link #onPreExecute()}.
     */
    protected void onPreExecuteInternal() {
        // empty by default
    }

    @Override
    protected final void onPostExecute(final Result result) {
        onPostExecuteInternal(result);
        if (null != activity) {
            progress.dismiss();
        }
    }

    /**
     * This method should typically be overridden by sub classes instead of {@link #onPostExecute(Object)}.
     *
     */
    protected void onPostExecuteInternal(final Result result) {
        // empty by default
    }

    @Override
    protected final void onProgressUpdate(final Integer... status) {
        final int progressValue = status[0];
        if (null != activity && progressValue >= 0) {
            progress.setProgress(progressValue);
        }
        onProgressUpdateInternal(progressValue);
    }

    /**
     * This method should by overridden by sub classes instead of {@link #onProgressUpdate(Integer...)}.
     */
    protected void onProgressUpdateInternal(@SuppressWarnings("unused") final int progress) {
        // empty by default
    }

    protected void setMessage(final String message) {
        progress.setMessage(message);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected final Result doInBackground(final Params... params) {
        if (params != null) {
            progress.setMaxProgressAndReset(params.length);
        }
        return doInBackgroundInternal(params);
    }

    protected abstract Result doInBackgroundInternal(Params[] params);
}