AliPayAuthLoadingDialog.java
2.43 KB
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
package com.wd.common.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Window;
import android.view.WindowManager;
/**
* Time:2022/10/10
* Author:ypf
* Description:进度条
*/
public class AliPayAuthLoadingDialog {
private static AliPayAuthLoadingDialog instance = null;
private Dialog mLoadingDialog;
private AliPayAuthLoadingDialog() {
}
public static AliPayAuthLoadingDialog getInstance() {
if (instance == null) {
instance = new AliPayAuthLoadingDialog();
}
return instance;
}
/**
* 创建dialog 对象
*/
private void createLoadingDialog(Context context, boolean allowCancel) {
mLoadingDialog = DialogUtils.createRequestDialog(context, allowCancel);
}
public void startLoading(Activity activity, boolean allowCancel) {
runOnUiThreadStartLoading(activity, allowCancel);
}
/**
* 设置对话框透明度
*/
public void setAlpha(float alpha){
if (mLoadingDialog != null) {
Window window = mLoadingDialog.getWindow();
if (window != null) {
WindowManager.LayoutParams attributes = window.getAttributes();
if (attributes != null) {
attributes.alpha = alpha;
}
}
}
}
public void stopLoading() {
//异常处理,not attached to window manager
try {
if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
mLoadingDialog.dismiss();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void runOnUiThreadStartLoading(Activity activity, boolean allowCancel) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
startLoadingDialog(activity, allowCancel);
}
});
}
private void startLoadingDialog(Activity activity, boolean allowCancel) {
if (activity == null || activity.isDestroyed()) {
return;
}
createLoadingDialog(activity, allowCancel);
if (mLoadingDialog != null && !mLoadingDialog.isShowing()) {
mLoadingDialog.show();
}
}
public void cancelDialog() {
if (mLoadingDialog != null) {
mLoadingDialog.cancel();
mLoadingDialog = null;
}
}
}