Skip to content

Commit 079985d

Browse files
authored
Refactor LoadingDialog to run tasks in a new thread
Fixed NetworkOnMainThreadException when downloading web page content in Dev Tools.
1 parent 0a53764 commit 079985d

1 file changed

Lines changed: 47 additions & 45 deletions

File tree

utils/src/main/java/org/xedox/utils/dialog/LoadingDialog.java

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Context;
44
import android.os.Handler;
55
import android.os.Looper;
6-
import android.view.View;
76
import android.widget.ProgressBar;
87
import android.widget.TextView;
98
import org.xedox.utils.R;
@@ -21,17 +20,14 @@ public class LoadingDialog {
2120
public LoadingDialog(Context context, String title, Runnable run) {
2221
this.context = context;
2322
this.runnable = run;
23+
2424
builder = new DialogBuilder(context);
2525
builder.setView(R.layout.dialog_loading_layout);
2626
builder.setTitle(title);
2727
builder.setCancelable(false);
2828

2929
infoText = builder.findViewById(R.id.loading_info);
3030
progressBar = builder.findViewById(R.id.progress);
31-
32-
if (runnable != null) {
33-
startBackgroundTask();
34-
}
3531
}
3632

3733
public LoadingDialog(Context context, int title, Runnable run) {
@@ -40,61 +36,67 @@ public LoadingDialog(Context context, int title, Runnable run) {
4036

4137
public void setRunnable(Runnable runnable) {
4238
this.runnable = runnable;
43-
if (isShowing && runnable != null) {
44-
startBackgroundTask();
45-
}
4639
}
4740

4841
private void startBackgroundTask() {
49-
runnable.run();
50-
dismiss();
42+
new Thread(() -> {
43+
try {
44+
if (runnable != null) {
45+
runnable.run();
46+
}
47+
} finally {
48+
dismiss();
49+
}
50+
}).start();
5151
}
5252

5353
public void show() {
54-
handler.post(
55-
() -> {
56-
if (!isShowing) {
57-
builder.show();
58-
isShowing = true;
59-
if (runnable != null) {
60-
startBackgroundTask();
61-
}
62-
}
63-
});
54+
handler.post(() -> {
55+
if (!isShowing) {
56+
builder.show();
57+
isShowing = true;
58+
59+
if (runnable != null) {
60+
startBackgroundTask();
61+
}
62+
}
63+
});
6464
}
6565

6666
public void dismiss() {
67-
handler.post(
68-
() -> {
69-
if (isShowing) {
70-
builder.dialog.dismiss();
71-
isShowing = false;
72-
}
73-
});
67+
handler.post(() -> {
68+
if (isShowing) {
69+
if (builder.dialog != null && builder.dialog.isShowing()) {
70+
builder.dialog.dismiss();
71+
}
72+
isShowing = false;
73+
}
74+
});
7475
}
7576

7677
public void setMaxProgress(int max) {
77-
handler.post(
78-
() -> {
79-
if (max <= 0) {
80-
progressBar.setIndeterminate(true);
81-
} else {
82-
progressBar.setIndeterminate(false);
83-
progressBar.setMax(max);
84-
}
85-
});
78+
handler.post(() -> {
79+
if (max <= 0) {
80+
progressBar.setIndeterminate(true);
81+
} else {
82+
progressBar.setIndeterminate(false);
83+
progressBar.setMax(max);
84+
}
85+
});
8686
}
8787

8888
public void updateProgress(String message, int progress) {
89-
handler.post(
90-
() -> {
91-
if (isShowing) {
92-
if (message != null) infoText.setText(message);
93-
if (!progressBar.isIndeterminate()) {
94-
progressBar.setProgress(progress);
95-
}
96-
}
97-
});
89+
handler.post(() -> {
90+
if (isShowing) {
91+
if (message != null) {
92+
infoText.setText(message);
93+
}
94+
95+
if (!progressBar.isIndeterminate()) {
96+
progressBar.setProgress(progress);
97+
}
98+
}
99+
});
98100
}
99101

100102
public Handler getHandler() {

0 commit comments

Comments
 (0)