From a1ac20d9cbe90b566bffe3ed39a6e07700c3248f Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期一, 19 六月 2023 10:30:06 +0800 Subject: [PATCH] Merge branch 'wjc' --- app/src/main/java/com/hdl/photovoltaic/uni/HDLUniMPSDKManager.java | 409 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 409 insertions(+), 0 deletions(-) diff --git a/app/src/main/java/com/hdl/photovoltaic/uni/HDLUniMPSDKManager.java b/app/src/main/java/com/hdl/photovoltaic/uni/HDLUniMPSDKManager.java new file mode 100644 index 0000000..521c6d6 --- /dev/null +++ b/app/src/main/java/com/hdl/photovoltaic/uni/HDLUniMPSDKManager.java @@ -0,0 +1,409 @@ +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宸茬粡閮ㄧ讲杩�"); + } + } + + /** + * 灏唚gt搴旂敤璧勬簮鍖呴儴缃插埌杩愯璺緞涓� + * + * @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 灏忕▼搴廔D + * @param path 鎸囧畾璺緞 + * 渚嬶細pages/component/scroll-view/scroll-view?a=1&b=2&c=3 + * @param jsonObject 闄勫姞鏁版嵁 + * @param callBack 鐩戝惉灏忕▼搴廜therUniMPEvent浜嬩欢 + */ + public void openUniMP(String uniAppId, String path, JSONObject jsonObject, IOnOtherUniMPEventCallBack callBack) { + if (TextUtils.isEmpty(uniAppId)) { + HdlThreadLogic.toast(mContext, "uni Id null"); + HdlLogLogic.print(TAG, "灏忕▼搴廔D 涓嶈兘涓虹┖"); + 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()); + } + } + + /** + * 鍒濆鍖朥niMPSDK 灏忕▼搴廠DK + */ + 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); + } + }); + + //鍒濆鍖朥niMPSDKManager + HDLUniMPSDKManager.getInstance().init(HDLApp.getInstance()); + + } + + public IUniMP getUniMP() { + return uniMP; + } + + /** + * 鎵撳紑灏忕▼搴忓苟浼犲叆鍙傛暟 + * + * @param uniAppId 灏忕▼搴廔D + * @param arguments JSONObject arguments = new JSONObject(); + * arguments.put("MSG","Hello HDL"); + * @param callBack 鐩戝惉灏忕▼搴廜therUniMPEvent浜嬩欢 + */ + public void openUniMP(String uniAppId, JSONObject arguments, IOnOtherUniMPEventCallBack callBack) { + if (TextUtils.isEmpty(uniAppId)) { + HdlLogLogic.print(TAG, "灏忕▼搴廔D 涓嶈兘涓虹┖"); + 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鎴朖SON + */ + 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, "鍙戦�侀�氱煡鎴愬姛锛歟vent锛�" + event + " data锛�" + new Gson().toJson(data)); + } else { + HdlLogLogic.print(TAG, "鍙戦�佸け璐ワ紝灏忕▼搴忓璞′负绌猴細" + uniAppId); + } + } else { + HdlLogLogic.print(TAG, "鍙戦�佸け璐ワ紝鎵句笉鍒板搴旂殑灏忕▼搴忥細" + uniAppId); + } + + } + + /** + * 鍘熺敓鍚戝綋鍓嶆墦寮�鐨勫皬绋嬪簭鍙戦�侀�氱煡浜嬩欢 + * 娉ㄦ剰锛氶渶瑕佹彁鍓嶅皬绋嬪簭鍦ㄨ繍琛屾墠鍙垚鍔� + * + * @param event - + * @param data String鎴朖SON + */ + public void sendUniMPEventToCurrentOpenAppId(String event, Object data) { + if (TextUtils.isEmpty(mCurrentAppId)) { + HdlLogLogic.print(TAG, "褰撳墠娌℃墦寮�鐨勫皬绋嬪簭"); + return; + } + sendUniMPEvent(mCurrentAppId, event, data); + } + + /** + * 闄や簡鍏叡澶勭悊鐨勫叾浠朎vent + */ + private IOnOtherUniMPEventCallBack onOtherUniMPEventCallBack; + + /** + * IOnOtherUniMPEventCallBack + */ + public interface IOnOtherUniMPEventCallBack { + /** + * 闄や簡缁熶竴澶勭悊锛屽墿浣欏叾瀹冪壒瀹氬満鏅殑浜嬩欢閫氱煡 + * + * @param appid - + * @param event - + * @param data JSONObject com.alibaba.fastjson瑙f瀽澶勭悊 + * @param callback callback.invoke(JSONObject) 鐢╟om.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"); + } + } + +} -- Gitblit v1.8.0