mac
2024-06-12 12d6db5780d8a2121a3bef2d58bf897b24ff552a
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package com.hdl.photovoltaic.uni;
 
import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.os.SystemClock;
import android.text.TextUtils;
 
 
import com.google.gson.Gson;
import com.hdl.photovoltaic.HDLApp;
import com.hdl.photovoltaic.R;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.other.HdlLogLogic;
import com.hdl.photovoltaic.other.HdlThreadLogic;
import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus;
 
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
 
import java.io.File;
import java.util.HashMap;
 
import io.dcloud.common.DHInterface.ICallBack;
import io.dcloud.feature.sdk.DCSDKInitConfig;
import io.dcloud.feature.sdk.DCUniMPSDK;
import io.dcloud.feature.sdk.Interface.IDCUniMPPreInitCallback;
import io.dcloud.feature.sdk.Interface.IOnUniMPEventCallBack;
import io.dcloud.feature.sdk.Interface.IUniMP;
import io.dcloud.feature.sdk.Interface.IUniMPOnCloseCallBack;
import io.dcloud.feature.unimp.DCUniMPJSCallback;
import io.dcloud.feature.unimp.config.UniMPOpenConfiguration;
 
 
public class HDLUniMPSDKManager {
    private static final String TAG = "HDLUniMPSDKManager";
    /**
     * context
     */
    private Application mContext;
    private IUniMP uniMP;
    /**
     * unimp小程序实例缓存
     **/
    HashMap<String, IUniMP> mUniMPCaches = new HashMap<>();
    /**
     * 当前打开的小程序APPId
     */
    private String mCurrentAppId;
    /**
     * instance
     */
    private volatile static HDLUniMPSDKManager instance;
 
    /**
     * 获取当前对象
     *
     * @return HDLUniMPSDKManager
     */
    public static synchronized HDLUniMPSDKManager getInstance() {
        if (instance == null) {
            synchronized (HDLUniMPSDKManager.class) {
                if (instance == null) {
                    instance = new HDLUniMPSDKManager();
                }
            }
        }
        return instance;
    }
 
    /**
     * 反初始化
     */
    public void unInit() {
        mUniMPCaches.clear();
        this.mContext = null;
    }
 
    /**
     * 初始化
     *
     * @param ctx -
     */
    public void init(Application ctx) {
        this.mContext = ctx;
        registerCallBack();
    }
 
    /**
     * 移除相关回调监听
     */
    private void removeCallBack() {
        DCUniMPSDK.getInstance().setUniMPOnCloseCallBack(null);
        DCUniMPSDK.getInstance().setOnUniMPEventCallBack(null);
 
    }
 
    /**
     * 监听相关回调
     */
    private void registerCallBack() {
        DCUniMPSDK.getInstance();
        //1.设置小程序被关闭事件监听
        DCUniMPSDK.getInstance().setUniMPOnCloseCallBack(new IUniMPOnCloseCallBack() {
            @Override
            public void onClose(String appid) {
                if (AppConfigManage.isDebug()) {
                    HdlLogLogic.print("收到小程序通知  被关闭了");
                }
                //小程序被关闭需要对实例缓存删除操作
                if (mUniMPCaches.containsKey(appid)) {
                    mUniMPCaches.remove(appid);
                }
                //清空当前打开小程序记录
                if (appid.equals(mCurrentAppId)) {
                    mCurrentAppId = "";
                }
 
                //小程序被关闭了通知给原生
                BaseEventBus baseEventBus = new BaseEventBus();
                baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_HOME_CLOSE_HOME_DETAILS_PAGE);
                EventBus.getDefault().post(baseEventBus);
 
            }
        });
 
        //2.设置监听小程序发送给宿主的事件
        DCUniMPSDK.getInstance().setOnUniMPEventCallBack(new IOnUniMPEventCallBack() {
            @Override
            public void onUniMPEventReceive(String appid, String event, Object data, DCUniMPJSCallback callback) {
//                handelUniMPEventReceive(appid, event, data, callback);
                HdlLogLogic.print("收到小程序通知    event=" + event);
                //暂时通过这个处理小程序同时多条相同的请求
                if (data == null) {
                    HdlLogLogic.print("收到小程序通知,data数据为null。", true);
 
                    return;
                }
//                String key= MD5Utils.getMd5(event+data.toString());
//                if(UniAppMessageCacheUtil.getInstance().isExist(key))
//                {
//                    LogUtils.i("当前小程序数据短时间发送了多次");
//                    return;
//                }
//                UniAppMessageCacheUtil.getInstance().put(key,System.currentTimeMillis());
 
                //只处理一下统一处理的事件
                if (HDLUniMP.UNI_EVENT_GetAppParams.equals(event)) {
                    //回传数据给小程序
                    JSONObject params = getAppParams();
                    callback.invoke(params);
                    HdlLogLogic.print("uni_GetAppParams callback:" + params.toString());
                } else {
                    //除了统一处理,剩余其它特定场景的事件通知
                    if (onOtherUniMPEventCallBack != null) {
                        onOtherUniMPEventCallBack.onOtherUniMPEventReceive(appid, event, data, callback);
                    } else {
                        HdlLogLogic.print("收到小程序通知,回调事件为null,无法回调事件并执行业务逻辑--->event=" + event + "\r\n" + "data=" + data, true);
                    }
                }
            }
        });
    }
 
    /**
     * 小程序向APP获取所需的配置参数
     *
     * @return -
     */
    private JSONObject getAppParams() {
        JSONObject mParams = new JSONObject();
        try {
            //回调告诉小程序要使用的语言
            mParams.put("language", "zh-Hans");
//            mParams.put("language", "en");//回调告诉小程序要使用的语言
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return mParams;
    }
 
 
    /**
     * 小程序是否初始化完成
     * 启动uni小程序,确保已初始化完毕再调用
     *
     * @return -
     */
    public Boolean isInitialize() {
        return DCUniMPSDK.getInstance().isInitialize();
    }
 
 
    /**
     * 检查是否存在应用资源,不存在则部署
     *
     * @param uniAppId -
     */
    public void checkUniMPResource(String uniAppId) {
        //2.不是第一次启动了,先判断wgt包之前有么有部署过
        if (!DCUniMPSDK.getInstance().isExistsApp(uniAppId)) {
            HdlLogLogic.print("DCUni wgt之前还没部署过");
            //之前没部署过重新部署
            hdlReleaseAppResourceToRunPathWithAppid(uniAppId);
//            [self hdlReleaseAppResourceToRunPathWithAppid:uniAppId];
        } else {
            HdlLogLogic.print("DCUni wgt已经部署过");
        }
    }
 
    /**
     * 将wgt应用资源包部署到运行路径中
     *
     * @param uniAppId -
     */
    public void hdlReleaseAppResourceToRunPathWithAppid(String uniAppId) {
        String wgtPath = mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/" + uniAppId + ".wgt";
        boolean b = fileIsExists(wgtPath);
        if (b) {
            HdlLogLogic.print("wgt包本地存在");
            DCUniMPSDK.getInstance().releaseWgtToRunPathFromePath(uniAppId, wgtPath, new ICallBack() {
                @Override
                public Object onCallBack(int code, Object pArgs) {
                    if (code == 1) {
                        //释放wgt完成
 
                    } else {
                        //释放wgt失败
                        HdlLogLogic.print("资源释放失败");
                    }
                    return null;
                }
            });
        } else {
            HdlLogLogic.print("wgt包本地不存在");
        }
    }
 
    /**
     * 判断文件是否存在
     *
     * @param strFile -
     * @return -
     */
    public static boolean fileIsExists(String strFile) {
        try {
            File f = new File(strFile);
            if (!f.exists()) {
                return false;
            }
 
        } catch (Exception e) {
            return false;
        }
        return true;
    }
 
    /**
     * 打开小程序
     *
     * @param uniAppId   小程序ID
     * @param path       指定路径
     *                   例:pages/component/scroll-view/scroll-view?a=1&b=2&c=3
     * @param jsonObject 附加数据
     * @param callBack   监听小程序OtherUniMPEvent事件
     */
    public void openUniMPDelay(String uniAppId, String path, JSONObject jsonObject, IOnOtherUniMPEventCallBack callBack) {
        if (TextUtils.isEmpty(uniAppId)) {
            HdlThreadLogic.toast(mContext, "uni Id null");
            HdlLogLogic.print("小程序ID 不能为空");
            return;
        }
        try {
            setOnOtherUniMPEventCallBack(callBack);
            uniMP = null;
            if (TextUtils.isEmpty(path)) {
                uniMP = DCUniMPSDK.getInstance().openUniMP(mContext, uniAppId);
            } else {
                UniMPOpenConfiguration uniMPOpenConfiguration = new UniMPOpenConfiguration();
                uniMPOpenConfiguration.path = path;
                uniMPOpenConfiguration.extraData = jsonObject;
                uniMPOpenConfiguration.splashClass=MySplashView.class;
                uniMP = DCUniMPSDK.getInstance().openUniMP(mContext, uniAppId, uniMPOpenConfiguration);
            }
            //android14有些设备有问题,需要等待
            if (android.os.Build.VERSION.SDK_INT >= 34) {
                if (TextUtils.isEmpty(mCurrentAppId)) {
                    //第一次用要休眠
                    SystemClock.sleep(1000);
                }
            }
            mCurrentAppId = uniAppId;//记录当前小程序
            mUniMPCaches.put(uniMP.getAppid(), uniMP);
        } catch (Exception e) {
            e.printStackTrace();
            HdlThreadLogic.toast(HDLApp.getInstance(), R.string.uni_open_error);
//            initDCUniMPSDK(HDLApp.getInstance());
            HdlLogLogic.print("打开失败===" + e.getMessage());
        }
    }
 
    public void openUniMP(String uniAppId, String path, JSONObject jsonObject, IOnOtherUniMPEventCallBack callBack) {
        if (TextUtils.isEmpty(uniAppId)) {
            HdlThreadLogic.toast(mContext, "uni Id null");
            HdlLogLogic.print("小程序ID 不能为空");
            return;
        }
        try {
            setOnOtherUniMPEventCallBack(callBack);
            uniMP = null;
            if (TextUtils.isEmpty(path)) {
                uniMP = DCUniMPSDK.getInstance().openUniMP(mContext, uniAppId);
            } else {
                UniMPOpenConfiguration uniMPOpenConfiguration = new UniMPOpenConfiguration();
                uniMPOpenConfiguration.path = path;
                uniMPOpenConfiguration.extraData = jsonObject;
                uniMPOpenConfiguration.splashClass=MySplashView.class;
                uniMP = DCUniMPSDK.getInstance().openUniMP(mContext, uniAppId, uniMPOpenConfiguration);
            }
            mCurrentAppId = uniAppId;//记录当前小程序
            mUniMPCaches.put(uniMP.getAppid(), uniMP);
        } catch (Exception e) {
            e.printStackTrace();
            HdlThreadLogic.toast(HDLApp.getInstance(), R.string.uni_open_error);
//            initDCUniMPSDK(HDLApp.getInstance());
            HdlLogLogic.print("打开失败===" + e.getMessage());
        }
    }
 
    /**
     * 初始化UniMPSDK 小程序SDK
     */
    public void initDCUniMPSDK(Context mContext) {
        if (DCUniMPSDK.getInstance().isInitialize()) {
            return;
        }
        DCSDKInitConfig config = new DCSDKInitConfig.Builder()
                .setCapsule(false)//设置是否使用胶囊按钮
                .setEnableBackground(false)//开启后台运行
                .build();
        DCUniMPSDK.getInstance().initialize(mContext, config, new IDCUniMPPreInitCallback() {
            @Override
            public void onInitFinished(boolean b) {
 
                HdlLogLogic.print("UniMPSDK 初始化完成----" + b);
            }
        });
 
        //初始化UniMPSDKManager
        HDLUniMPSDKManager.getInstance().init(HDLApp.getInstance());
 
    }
 
    public IUniMP getUniMP() {
        return uniMP;
    }
 
    /**
     * 打开小程序并传入参数
     *
     * @param uniAppId  小程序ID
     * @param arguments JSONObject arguments = new JSONObject();
     *                  arguments.put("MSG","Hello HDL");
     * @param callBack  监听小程序OtherUniMPEvent事件
     */
    public void openUniMP(String uniAppId, JSONObject arguments, IOnOtherUniMPEventCallBack callBack) {
        if (TextUtils.isEmpty(uniAppId)) {
            HdlLogLogic.print("小程序ID 不能为空");
            return;
        }
        try {
            setOnOtherUniMPEventCallBack(callBack);
            UniMPOpenConfiguration uniMPOpenConfiguration = new UniMPOpenConfiguration();
            uniMPOpenConfiguration.extraData = arguments;
            uniMP = DCUniMPSDK.getInstance().openUniMP(mContext, uniAppId, uniMPOpenConfiguration);
            mCurrentAppId = uniAppId;//记录当前小程序
            mUniMPCaches.put(uniMP.getAppid(), uniMP);
        } catch (Exception e) {
            e.printStackTrace();
            HdlLogLogic.print("打开失败===" + e.getMessage());
        }
    }
 
    /**
     * 原生向小程序发送通知事件
     * 注意:需要提前小程序在运行才可成功
     *
     * @param uniAppId 小程序指定
     * @param topic    小程序指定主题
     * @param data     String或JSON
     */
    public void sendUniMPEvent(String uniAppId, String topic, Object data) {
 
        if (mUniMPCaches.containsKey(uniAppId)) {
            if (mUniMPCaches.get(uniAppId) != null) {
                mUniMPCaches.get(uniAppId).sendUniMPEvent(topic, data);
                HdlLogLogic.print("Android--->通知---uni--->主题:" + topic + "--->发送数据:" + new Gson().toJson(data));
            } else {
                HdlLogLogic.print("Android--->通知---uni--->失败--->小程序对象为空:" + uniAppId);
            }
        } else {
            HdlLogLogic.print("Android--->通知---uni--->失败--->小程序对象为空:" + uniAppId);
        }
 
    }
 
    /**
     * 原生向当前打开的小程序发送通知事件
     * 注意:需要提前小程序在运行才可成功
     *
     * @param event -
     * @param data  String或JSON
     */
    public void sendUniMPEventToCurrentOpenAppId(String event, Object data) {
        if (TextUtils.isEmpty(mCurrentAppId)) {
            HdlLogLogic.print("当前没打开的小程序");
            return;
        }
        sendUniMPEvent(mCurrentAppId, event, data);
    }
 
    /**
     * 除了公共处理的其他Event
     */
    private IOnOtherUniMPEventCallBack onOtherUniMPEventCallBack;
 
    /**
     * IOnOtherUniMPEventCallBack
     */
    public interface IOnOtherUniMPEventCallBack {
        /**
         * 除了统一处理,剩余其它特定场景的事件通知
         *
         * @param appid    -
         * @param event    -
         * @param data     JSONObject com.alibaba.fastjson解析处理
         * @param callback callback.invoke(JSONObject) 用com.alibaba.fastjson
         */
        void onOtherUniMPEventReceive(String appid, String event, Object data, DCUniMPJSCallback callback);
    }
 
    public void setOnOtherUniMPEventCallBack(IOnOtherUniMPEventCallBack onOtherUniMPEventCallBack) {
        this.onOtherUniMPEventCallBack = onOtherUniMPEventCallBack;
        HdlLogLogic.print("onOtherUniMPEventCallBack set callback");
    }
 
    /**
     * 检测是否当前页面注册的callback,是的话则移除
     *
     * @param onOtherUniMPEventCallBack -
     */
    public void checkRemoveOtherUniMPEventCallBack(IOnOtherUniMPEventCallBack onOtherUniMPEventCallBack) {
        if (this.onOtherUniMPEventCallBack == onOtherUniMPEventCallBack) {
            this.onOtherUniMPEventCallBack = null;
            HdlLogLogic.print("onOtherUniMPEventCallBack remove callback");
        }
    }
 
}