New file |
| | |
| | | package com.hdl.photovoltaic.uni; |
| | | |
| | | import android.app.Application; |
| | | import android.content.Context; |
| | | import android.os.Environment; |
| | | 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 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() { |
| | | //1.设置小程序被关闭事件监听 |
| | | DCUniMPSDK.getInstance().setUniMPOnCloseCallBack(new IUniMPOnCloseCallBack() { |
| | | @Override |
| | | public void onClose(String appid) { |
| | | if (AppConfigManage.isDebug()) { |
| | | HdlLogLogic.print(TAG, "收到小程序通知 被关闭了"); |
| | | } |
| | | //小程序被关闭需要对实例缓存删除操作 |
| | | if (mUniMPCaches.containsKey(appid)) { |
| | | mUniMPCaches.remove(appid); |
| | | } |
| | | //清空当前打开小程序记录 |
| | | if (appid.equals(mCurrentAppId)) { |
| | | mCurrentAppId = ""; |
| | | } |
| | | } |
| | | }); |
| | | |
| | | //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(TAG, "收到小程序通知 event=" + event); |
| | | //暂时通过这个处理小程序同时多条相同的请求 |
| | | if (data == null) { |
| | | 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(TAG, "uni_GetAppParams callback:" + params.toString()); |
| | | } else { |
| | | //除了统一处理,剩余其它特定场景的事件通知 |
| | | if (onOtherUniMPEventCallBack != null) { |
| | | onOtherUniMPEventCallBack.onOtherUniMPEventReceive(appid, event, data, callback); |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 小程序向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(TAG, "DCUni wgt之前还没部署过"); |
| | | //之前没部署过重新部署 |
| | | hdlReleaseAppResourceToRunPathWithAppid(uniAppId); |
| | | // [self hdlReleaseAppResourceToRunPathWithAppid:uniAppId]; |
| | | } else { |
| | | HdlLogLogic.print(TAG, "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(TAG, "wgt包本地存在"); |
| | | DCUniMPSDK.getInstance().releaseWgtToRunPathFromePath(uniAppId, wgtPath, new ICallBack() { |
| | | @Override |
| | | public Object onCallBack(int code, Object pArgs) { |
| | | if (code == 1) { |
| | | //释放wgt完成 |
| | | |
| | | } else { |
| | | //释放wgt失败 |
| | | HdlLogLogic.print(TAG, "资源释放失败"); |
| | | } |
| | | return null; |
| | | } |
| | | }); |
| | | } else { |
| | | HdlLogLogic.print(TAG, "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 openUniMP(String uniAppId, String path, JSONObject jsonObject, IOnOtherUniMPEventCallBack callBack) { |
| | | if (TextUtils.isEmpty(uniAppId)) { |
| | | HdlThreadLogic.toast(mContext, "uni Id null"); |
| | | HdlLogLogic.print(TAG, "小程序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; |
| | | 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(TAG, "打开失败" + 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("unimp", "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(TAG, "小程序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(TAG, "打开失败" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 原生向小程序发送通知事件 |
| | | * 注意:需要提前小程序在运行才可成功 |
| | | * |
| | | * @param uniAppId 小程序指定 |
| | | * @param event 小程序指定主题 |
| | | * @param data String或JSON |
| | | */ |
| | | public void sendUniMPEvent(String uniAppId, String event, 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)); |
| | | } else { |
| | | HdlLogLogic.print(TAG, "发送失败,小程序对象为空:" + uniAppId); |
| | | } |
| | | } else { |
| | | HdlLogLogic.print(TAG, "发送失败,找不到对应的小程序:" + uniAppId); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 原生向当前打开的小程序发送通知事件 |
| | | * 注意:需要提前小程序在运行才可成功 |
| | | * |
| | | * @param event - |
| | | * @param data String或JSON |
| | | */ |
| | | public void sendUniMPEventToCurrentOpenAppId(String event, Object data) { |
| | | if (TextUtils.isEmpty(mCurrentAppId)) { |
| | | HdlLogLogic.print(TAG, "当前没打开的小程序"); |
| | | 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"); |
| | | } |
| | | } |
| | | |
| | | } |