wjc
2025-05-23 971a24a9e58a21bc306897fd3ad63012a399f7db
app/src/main/java/com/hdl/photovoltaic/uni/HDLUniMPSDKManager.java
@@ -1,25 +1,36 @@
package com.hdl.photovoltaic.uni;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.Process;
import android.text.TextUtils;
import android.util.Log;
import com.google.gson.Gson;
import com.hdl.linkpm.sdk.user.HDLLinkPMUser;
import com.hdl.photovoltaic.HDLApp;
import com.hdl.photovoltaic.R;
import com.hdl.photovoltaic.config.AppConfigManage;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.enums.LowerTagType;
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 java.util.List;
import io.dcloud.common.DHInterface.ICallBack;
import io.dcloud.common.util.RuningAcitvityUtil;
import io.dcloud.feature.sdk.DCSDKInitConfig;
import io.dcloud.feature.sdk.DCUniMPSDK;
import io.dcloud.feature.sdk.Interface.IDCUniMPPreInitCallback;
@@ -27,10 +38,15 @@
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";
    /**
     * 标记登录界面是否已经存在
     */
    public static boolean isExistsActivity = false;
    /**
     * context
     */
@@ -89,19 +105,22 @@
    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(TAG, "收到小程序通知  被关闭了");
                }
//                if (AppConfigManage.isDebug()) {
//                    HdlLogLogic.print("收到小程序通知  被关闭了");
//                }
                HdlLogLogic.print("收到小程序通知  被关闭了", true);
                //小程序被关闭需要对实例缓存删除操作
                if (mUniMPCaches.containsKey(appid)) {
                    mUniMPCaches.remove(appid);
@@ -110,6 +129,13 @@
                if (appid.equals(mCurrentAppId)) {
                    mCurrentAppId = "";
                }
                //小程序被关闭了通知给原生
                BaseEventBus baseEventBus = new BaseEventBus();
                baseEventBus.setTopic(HDLUniMP.UNI_EVENT_REPLY_HOME_CLOSE_HOME_DETAILS_PAGE);
                baseEventBus.setType(LowerTagType.power_station.toString());
                EventBus.getDefault().post(baseEventBus);
            }
        });
@@ -118,9 +144,12 @@
            @Override
            public void onUniMPEventReceive(String appid, String event, Object data, DCUniMPJSCallback callback) {
//                handelUniMPEventReceive(appid, event, data, callback);
                HdlLogLogic.print(TAG, "收到小程序通知    event=" + event);
                HdlLogLogic.print("收到小程序通知    event=" + event);
//                String processMessage = Process.myPid() + " ";
//                HdlLogLogic.print("进程信息:" + processMessage);
                //暂时通过这个处理小程序同时多条相同的请求
                if (data == null) {
                    HdlLogLogic.print("收到小程序通知,data数据为null。", true);
                    return;
                }
//                String key= MD5Utils.getMd5(event+data.toString());
@@ -136,11 +165,19 @@
                    //回传数据给小程序
                    JSONObject params = getAppParams();
                    callback.invoke(params);
                    HdlLogLogic.print(TAG, "uni_GetAppParams callback:" + params.toString());
                    HdlLogLogic.print("uni_GetAppParams callback:" + params.toString());
                } else {
                    //除了统一处理,剩余其它特定场景的事件通知
                    if (onOtherUniMPEventCallBack != null) {
                        onOtherUniMPEventCallBack.onOtherUniMPEventReceive(appid, event, data, callback);
                    } else {
                        if (!HDLUniMPSDKManager.isExistsActivity) {
                            HDLUniMPSDKManager.isExistsActivity = true;
                            HdlLogLogic.print("收到小程序通知,回调事件为null,无法回调事件并执行业务逻辑---event=" + event + "\r\n" + "data=" + data, true);
                            //有可能是系统回收掉activity导致回调为null,这样情况下强制返回登录界面;
                            //todo 长时间没响应将退出登录界面重新登录
                            HDLLinkPMUser.getInstance().logout(0);
                        }
                    }
                }
            }
@@ -184,12 +221,12 @@
    public void checkUniMPResource(String uniAppId) {
        //2.不是第一次启动了,先判断wgt包之前有么有部署过
        if (!DCUniMPSDK.getInstance().isExistsApp(uniAppId)) {
            HdlLogLogic.print(TAG, "DCUni wgt之前还没部署过");
            HdlLogLogic.print("DCUni wgt之前还没部署过");
            //之前没部署过重新部署
            hdlReleaseAppResourceToRunPathWithAppid(uniAppId);
//            [self hdlReleaseAppResourceToRunPathWithAppid:uniAppId];
        } else {
            HdlLogLogic.print(TAG, "DCUni wgt已经部署过");
            HdlLogLogic.print("DCUni wgt已经部署过");
        }
    }
@@ -202,7 +239,7 @@
        String wgtPath = mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/" + uniAppId + ".wgt";
        boolean b = fileIsExists(wgtPath);
        if (b) {
            HdlLogLogic.print(TAG, "wgt包本地存在");
            HdlLogLogic.print("wgt包本地存在");
            DCUniMPSDK.getInstance().releaseWgtToRunPathFromePath(uniAppId, wgtPath, new ICallBack() {
                @Override
                public Object onCallBack(int code, Object pArgs) {
@@ -211,13 +248,13 @@
                    } else {
                        //释放wgt失败
                        HdlLogLogic.print(TAG, "资源释放失败");
                        HdlLogLogic.print("资源释放失败");
                    }
                    return null;
                }
            });
        } else {
            HdlLogLogic.print(TAG, "wgt包本地不存在");
            HdlLogLogic.print("wgt包本地不存在");
        }
    }
@@ -240,18 +277,10 @@
        return true;
    }
    /**
     * 打开小程序
     *
     * @param uniAppId 小程序ID
     * @param path     指定路径
     *                 例:pages/component/scroll-view/scroll-view?a=1&b=2&c=3
     * @param callBack 监听小程序OtherUniMPEvent事件
     */
    public void openUniMP(String uniAppId, String path, IOnOtherUniMPEventCallBack callBack) {
    public void openUniMP(String uniAppId, String path, JSONObject jsonObject, IOnOtherUniMPEventCallBack callBack) {
        if (TextUtils.isEmpty(uniAppId)) {
            HdlThreadLogic.toast(mContext, "uni Id null");
            HdlLogLogic.print(TAG, "小程序ID 不能为空");
            HdlLogLogic.print("小程序ID 不能为空");
            return;
        }
        try {
@@ -260,17 +289,30 @@
            if (TextUtils.isEmpty(path)) {
                uniMP = DCUniMPSDK.getInstance().openUniMP(mContext, uniAppId);
            } else {
//                UniMPOpenConfiguration uniMPOpenConfiguration = new UniMPOpenConfiguration();
//                uniMPOpenConfiguration.path = path;
                uniMP = DCUniMPSDK.getInstance().openUniMP(mContext, uniAppId, path);
                UniMPOpenConfiguration uniMPOpenConfiguration = new UniMPOpenConfiguration();
                uniMPOpenConfiguration.path = path;
                uniMPOpenConfiguration.extraData = jsonObject;
                if (UserConfigManage.getInstance().getUniBottomSafeDistanceBackgroundColor() == 0) {
                    uniMPOpenConfiguration.splashClass = CSplashView.class;
                } else {
                    uniMPOpenConfiguration.splashClass = BSplashView.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(TAG, "打开失败" + e.getMessage());
//            initDCUniMPSDK(HDLApp.getInstance());
            HdlLogLogic.print("打开失败===" + e.getMessage());
        }
    }
@@ -278,6 +320,7 @@
     * 初始化UniMPSDK 小程序SDK
     */
    public void initDCUniMPSDK(Context mContext) {
        HdlLogLogic.print("UniMPSDK状态---" + DCUniMPSDK.getInstance().isInitialize(), true);
        if (DCUniMPSDK.getInstance().isInitialize()) {
            return;
        }
@@ -289,7 +332,7 @@
            @Override
            public void onInitFinished(boolean b) {
                HdlLogLogic.print("unimp", "UniMPSDK 初始化完成----" + b);
                HdlLogLogic.print("UniMPSDK 初始化完成----" + b);
            }
        });
@@ -312,19 +355,19 @@
     */
    public void openUniMP(String uniAppId, JSONObject arguments, IOnOtherUniMPEventCallBack callBack) {
        if (TextUtils.isEmpty(uniAppId)) {
            HdlLogLogic.print(TAG, "小程序ID 不能为空");
            HdlLogLogic.print("小程序ID 不能为空");
            return;
        }
        try {
            setOnOtherUniMPEventCallBack(callBack);
//            UniMPOpenConfiguration uniMPOpenConfiguration = new UniMPOpenConfiguration();
//            uniMPOpenConfiguration.extraData = arguments;
            uniMP = DCUniMPSDK.getInstance().openUniMP(mContext, uniAppId, arguments);
            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(TAG, "打开失败" + e.getMessage());
            HdlLogLogic.print("打开失败===" + e.getMessage());
        }
    }
@@ -333,20 +376,20 @@
     * 注意:需要提前小程序在运行才可成功
     *
     * @param uniAppId 小程序指定
     * @param event    小程序指定主题
     * @param topic    小程序指定主题
     * @param data     String或JSON
     */
    public void sendUniMPEvent(String uniAppId, String event, Object data) {
    public void sendUniMPEvent(String uniAppId, String topic, Object data) {
        if (mUniMPCaches.containsKey(uniAppId)) {
            if (mUniMPCaches.get(uniAppId) != null) {
                mUniMPCaches.get(uniAppId).sendUniMPEvent(event, data);
                HdlLogLogic.print(TAG, "发送通知成功:event:" + event + " data:" + new Gson().toJson(data));
                mUniMPCaches.get(uniAppId).sendUniMPEvent(topic, data);
//                HdlLogLogic.print("Android---通知---uni---主题:" + topic + "---发送数据:" + new Gson().toJson(data));
            } else {
                HdlLogLogic.print(TAG, "发送失败,小程序对象为空:" + uniAppId);
//                HdlLogLogic.print("Android---通知---uni---失败---小程序对象为空:" + uniAppId);
            }
        } else {
            HdlLogLogic.print(TAG, "发送失败,找不到对应的小程序:" + uniAppId);
//            HdlLogLogic.print("Android---通知---uni---失败---¬小程序对象为空:" + uniAppId);
        }
    }
@@ -360,7 +403,7 @@
     */
    public void sendUniMPEventToCurrentOpenAppId(String event, Object data) {
        if (TextUtils.isEmpty(mCurrentAppId)) {
            HdlLogLogic.print(TAG, "当前没打开的小程序");
            HdlLogLogic.print("当前没打开的小程序");
            return;
        }
        sendUniMPEvent(mCurrentAppId, event, data);
@@ -388,7 +431,7 @@
    public void setOnOtherUniMPEventCallBack(IOnOtherUniMPEventCallBack onOtherUniMPEventCallBack) {
        this.onOtherUniMPEventCallBack = onOtherUniMPEventCallBack;
        HdlLogLogic.print("onOtherUniMPEventCallBack", "set callback");
        HdlLogLogic.print("onOtherUniMPEventCallBack set callback");
    }
    /**
@@ -397,9 +440,10 @@
     * @param onOtherUniMPEventCallBack -
     */
    public void checkRemoveOtherUniMPEventCallBack(IOnOtherUniMPEventCallBack onOtherUniMPEventCallBack) {
        HdlLogLogic.print("onOtherUniMPEventCallBack", true);
        if (this.onOtherUniMPEventCallBack == onOtherUniMPEventCallBack) {
            this.onOtherUniMPEventCallBack = null;
            HdlLogLogic.print("onOtherUniMPEventCallBack", "remove callback");
            HdlLogLogic.print("onOtherUniMPEventCallBack remove callback", true);
        }
    }