session-android/src/org/thoughtcrime/securesms/util/task/ProgressDialogAsyncTask.java

45 lines
1.3 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.util.task;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
2014-08-12 12:11:23 -07:00
import java.lang.ref.WeakReference;
public abstract class ProgressDialogAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
2014-08-12 12:11:23 -07:00
private final WeakReference<Context> contextReference;
private ProgressDialog progress;
private final String title;
private final String message;
public ProgressDialogAsyncTask(@NonNull Context context, @NonNull String title, @NonNull String message) {
super();
2015-10-19 11:23:12 -07:00
this.contextReference = new WeakReference<>(context);
2014-08-12 12:11:23 -07:00
this.title = title;
this.message = message;
}
public ProgressDialogAsyncTask(@NonNull Context context, int title, int message) {
this(context, context.getString(title), context.getString(message));
}
@Override
protected void onPreExecute() {
2014-08-12 12:11:23 -07:00
final Context context = contextReference.get();
if (context != null) progress = ProgressDialog.show(context, title, message, true);
}
@Override
protected void onPostExecute(Result result) {
if (progress != null) progress.dismiss();
}
protected @NonNull Context getContext() {
return contextReference.get();
}
}