18034727885
2026-09-16 f8add3ffd28d3d77313c4af58173f9cadadd0746
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
package uni.dcloud.io.uniplugin_richalert;
 
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.TextUtils;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniDestroyableModule;
import io.dcloud.feature.uniapp.utils.UniLogUtils;
import io.dcloud.feature.uniapp.utils.UniResourceUtils;
 
public class RichAlertModule extends UniDestroyableModule {
    public String CONTENT = "content";
    public String CONTENT_COLOR  = "contentColor";
    public String CONTENT_ALIGN  = "contentAlign";
    public String POSITION = "position";
    public String BUTTONS = "buttons";
    public String CHECKBOX = "checkBox";
    public String TITLE_ALIGN = "titleAlign";
    //默认黑色
    public static int defColor = Color.BLACK;
 
    RichAlert alert;
 
    @UniJSMethod(uiThread = true)
    public void show(JSONObject options, UniJSCallback jsCallback) {
        if (mUniSDKInstance.getContext() instanceof Activity) {
            String content = options.getString(CONTENT);
            int contentColor = UniResourceUtils.getColor(options.getString(CONTENT_COLOR), defColor);
            String contentAlign = options.getString(CONTENT_ALIGN);
 
            String title = options.getString(RichAlert.TITLE);
            int titleColor = UniResourceUtils.getColor(options.getString(RichAlert.TITLE_COLOR), defColor);
            String titleAlign = options.getString(TITLE_ALIGN);
 
            String postion = options.getString(POSITION);
 
            RichAlert richAlert = new RichAlert(mUniSDKInstance.getContext());
 
            JSONArray buttons = options.getJSONArray(BUTTONS);
            JSONObject checkBox = options.getJSONObject(CHECKBOX);
 
            if(!TextUtils.isEmpty(title)) {
                richAlert.setTitle(title, titleColor, titleAlign);
            }
            if(!TextUtils.isEmpty(content)) {
                richAlert.setContent(content, contentColor, contentAlign,jsCallback);
            }
            if(checkBox != null) {
                richAlert.setCheckBox(checkBox, jsCallback);
            }
            if(buttons != null) {
                richAlert.setButtons(buttons, jsCallback);
            }
            if(!TextUtils.isEmpty(postion)) {
                richAlert.setPosition(postion);
            }
 
            richAlert.show();
            tracking(richAlert, jsCallback);
        }
    }
 
    private void tracking(RichAlert dialog, final UniJSCallback jsCallback) {
        alert = dialog;
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                JSONObject result = new JSONObject();
                result.put("type", "backCancel");
                jsCallback.invoke(result);
            }
        });
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                alert = null;
            }
        });
    }
 
    @UniJSMethod(uiThread = true)
    public void dismiss() {
        destroy();
    }
 
    @Override
    public void destroy() {
        if (alert != null && alert.isShowing()) {
            UniLogUtils.w("Dismiss the active dialog");
            alert.dismiss();
        }
    }
 
}