wjc
18 小时以前 2f5411b86c59706d31f6ec7de629d8f860725e46
app/src/main/java/com/hdl/photovoltaic/uni/HDLUniMPSDKManager.java
@@ -1,15 +1,24 @@
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.LogoutType;
import com.hdl.photovoltaic.enums.LowerTagType;
import com.hdl.photovoltaic.other.HdlAccountLogic;
import com.hdl.photovoltaic.other.HdlLogLogic;
import com.hdl.photovoltaic.other.HdlThreadLogic;
import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus;
@@ -20,8 +29,10 @@
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;
@@ -34,6 +45,14 @@
public class HDLUniMPSDKManager {
    private static final String TAG = "HDLUniMPSDKManager";
    /**
     * 标记登录界面是否已经存在
     */
    public static boolean isExistsActivity = false;
    /**
     * 小程序是否开启
     */
    public static boolean  uinEnabled = false;
    /**
     * context
     */
@@ -104,9 +123,11 @@
        DCUniMPSDK.getInstance().setUniMPOnCloseCallBack(new IUniMPOnCloseCallBack() {
            @Override
            public void onClose(String appid) {
                if (AppConfigManage.isDebug()) {
                    HdlLogLogic.print("收到小程序通知  被关闭了");
                }
                HDLUniMPSDKManager.uinEnabled=false;
//                if (AppConfigManage.isDebug()) {
//                    HdlLogLogic.print("收到小程序通知  被关闭了");
//                }
                HdlLogLogic.print("收到小程序通知  被关闭了", true);
                //小程序被关闭需要对实例缓存删除操作
                if (mUniMPCaches.containsKey(appid)) {
                    mUniMPCaches.remove(appid);
@@ -118,7 +139,8 @@
                //小程序被关闭了通知给原生
                BaseEventBus baseEventBus = new BaseEventBus();
                baseEventBus.setType(HDLUniMP.UNI_EVENT_REPLY_HOME_CLOSE_HOME_DETAILS_PAGE);
                baseEventBus.setTopic(HDLUniMP.UNI_EVENT_REPLY_HOME_CLOSE_HOME_DETAILS_PAGE);
                baseEventBus.setType(LowerTagType.power_station.toString());
                EventBus.getDefault().post(baseEventBus);
            }
@@ -128,12 +150,14 @@
        DCUniMPSDK.getInstance().setOnUniMPEventCallBack(new IOnUniMPEventCallBack() {
            @Override
            public void onUniMPEventReceive(String appid, String event, Object data, DCUniMPJSCallback callback) {
                HDLUniMPSDKManager.uinEnabled=true;
//                handelUniMPEventReceive(appid, event, data, callback);
                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());
@@ -155,7 +179,14 @@
                    if (onOtherUniMPEventCallBack != null) {
                        onOtherUniMPEventCallBack.onOtherUniMPEventReceive(appid, event, data, callback);
                    } else {
                        HdlLogLogic.print("收到小程序通知,回调事件为null,无法回调事件并执行业务逻辑--->event=" + event + "\r\n" + "data=" + data, true);
                        if (!HDLUniMPSDKManager.isExistsActivity) {
                            HDLUniMPSDKManager.isExistsActivity = true;
                            HdlLogLogic.print("收到小程序通知,回调事件为null,无法回调事件并执行业务逻辑---event=" + event + "\r\n" + "data=" + data, true);
                            //有可能是系统回收掉activity导致回调为null,这样情况下强制返回登录界面;
                            //todo 长时间没响应将退出登录界面重新登录
//                            HDLLinkPMUser.getInstance().logout(0);
                            HdlAccountLogic.getInstance().logout(LogoutType.AbnormalLogout);
                        }
                    }
                }
            }
@@ -188,6 +219,55 @@
     */
    public Boolean isInitialize() {
        return DCUniMPSDK.getInstance().isInitialize();
    }
    /**
     * 检查当前小程序组件是否正在运行
     *
     * @return true表示正在运行,false表示未运行
     */
    public Boolean isRuning() {
        Log.d(TAG,"isRuning");
        if (this.getUniMP() == null) {
            return false;
        }
        return this.getUniMP().isRuning();
    }
    /**
     * 显示已创建的小程序组件
     *
     * @return 通常与 hideUniMP() 配对使用,用于控制组件的可见性
     */
    public Boolean showUniMP() {
        Log.d(TAG,"showUniMP");
        if (this.getUniMP() == null) {
            return false;
        }
        return this.getUniMP().showUniMP();
    }
    /**
     * 隐藏当前显示的小程序组件
     *
     * @return 隐藏后组件仍在后台运行,可以快速重新显示
     */
    public Boolean hideUniMP() {
        Log.d(TAG,"hideUniMP");
        if (this.getUniMP() == null) {
            return false;
        }
        return this.getUniMP().hideUniMP();
    }
    /**
     * 完全关闭并销毁小程序组件实例(与hide的区别,close会释放资源,而hide只是暂时隐藏)
     *
     * @return
     */
    public Boolean closeUniMP() {
        Log.d(TAG,"closeUniMP");
        if (this.getUniMP() == null) {
            return false;
        }
        return this.getUniMP().closeUniMP();
    }
@@ -255,15 +335,6 @@
        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 openUniMP(String uniAppId, String path, JSONObject jsonObject, IOnOtherUniMPEventCallBack callBack) {
        if (TextUtils.isEmpty(uniAppId)) {
            HdlThreadLogic.toast(mContext, "uni Id null");
@@ -279,14 +350,26 @@
                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());
//            initDCUniMPSDK(HDLApp.getInstance());
            HdlLogLogic.print("打开失败===" + e.getMessage());
        }
    }
@@ -295,6 +378,7 @@
     * 初始化UniMPSDK 小程序SDK
     */
    public void initDCUniMPSDK(Context mContext) {
        HdlLogLogic.print("UniMPSDK状态---" + DCUniMPSDK.getInstance().isInitialize(), true);
        if (DCUniMPSDK.getInstance().isInitialize()) {
            return;
        }
@@ -306,7 +390,7 @@
            @Override
            public void onInitFinished(boolean b) {
                HdlLogLogic.print("UniMPSDK 初始化完成----" + b);
                HdlLogLogic.print("UniMPSDK 初始化完成----" + b, true);
            }
        });
@@ -358,12 +442,12 @@
        if (mUniMPCaches.containsKey(uniAppId)) {
            if (mUniMPCaches.get(uniAppId) != null) {
                mUniMPCaches.get(uniAppId).sendUniMPEvent(topic, data);
                HdlLogLogic.print("Android--->通知---uni--->主题:" + topic + "--->发送数据:" + new Gson().toJson(data));
//                HdlLogLogic.print("android---通知---uni---主题:" + topic + "---发送数据:" + new Gson().toJson(data));
            } else {
                HdlLogLogic.print("Android--->通知---uni--->失败--->小程序对象为空:" + uniAppId);
//                HdlLogLogic.print("android---通知---uni---失败---小程序对象为空:" + uniAppId);
            }
        } else {
            HdlLogLogic.print("Android--->通知---uni--->失败--->小程序对象为空:" + uniAppId);
//            HdlLogLogic.print("Android---通知---uni---失败---¬小程序对象为空:" + uniAppId);
        }
    }
@@ -414,9 +498,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);
        }
    }