diff --git a/common/src/main/java/com/yunbao/common/utils/JavascriptInterfaceUtils.java b/common/src/main/java/com/yunbao/common/utils/JavascriptInterfaceUtils.java index 659daee2b..006eec22f 100644 --- a/common/src/main/java/com/yunbao/common/utils/JavascriptInterfaceUtils.java +++ b/common/src/main/java/com/yunbao/common/utils/JavascriptInterfaceUtils.java @@ -1,16 +1,16 @@ package com.yunbao.common.utils; import static com.yunbao.common.CommonAppConfig.isGetNewWrap; -import static com.yunbao.common.CommonAppContext.logger; -import static com.yunbao.common.CommonAppContext.mFirebaseAnalytics; import static com.yunbao.common.utils.RouteUtil.PATH_COIN; import android.app.Activity; +import android.os.Handler; +import android.os.Looper; +import android.view.View; +import android.view.ViewGroup; import android.webkit.JavascriptInterface; import android.webkit.WebView; -import com.adjust.sdk.Adjust; -import com.adjust.sdk.AdjustEvent; import com.alibaba.android.arouter.launcher.ARouter; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; @@ -37,6 +37,7 @@ public class JavascriptInterfaceUtils { private WebView mWebView; //判断是页面关闭还是网页回退 private boolean pageClose = false; + private boolean dialogClose = false; //同名activity跳转问题 private boolean LiveZhuangBana = true; @@ -45,6 +46,11 @@ public class JavascriptInterfaceUtils { return this; } + public JavascriptInterfaceUtils setDialogClose(boolean dialogClose) { + this.dialogClose = dialogClose; + return this; + } + public JavascriptInterfaceUtils setLiveZhuangBana(boolean liveZhuangBana) { LiveZhuangBana = liveZhuangBana; return this; @@ -68,7 +74,13 @@ public class JavascriptInterfaceUtils { if (pageClose) { mContext.finish(); } else { - mContext.runOnUiThread(() -> mWebView.goBack()); + if (dialogClose) { + Bus.get().post(new JavascriptInterfaceEvent() + .setMethod("androidGoBack")); + } else { + mContext.runOnUiThread(() -> mWebView.goBack()); + } + } } @@ -419,11 +431,39 @@ public class JavascriptInterfaceUtils { } }); } + @JavascriptInterface public void AndroidAdjustPrank() { - AdjustEvent adjustEvent1 = new AdjustEvent("v1nekv"); - Adjust.trackEvent(adjustEvent1); - mFirebaseAnalytics.logEvent("FS_prank_interaction", null); - logger.logEvent("FB_prank_interaction", null); + Bus.get().post(new JavascriptInterfaceEvent() + .setMethod("AndroidAdjustPrank")); + + } + + @JavascriptInterface + public void openWrap() { + Bus.get().post(new JavascriptInterfaceEvent() + .setMethod("openWrap")); + + } + + @JavascriptInterface + public void androidSignGift(String json) { + Bus.get().post(new JavascriptInterfaceEvent() + .setMethod("androidSignGift") + .setData(json)); + + } + @JavascriptInterface + public void setHeight(String height, String width) { + if ("0".equals(height)) { + return; + } + Handler handler = new Handler(Looper.getMainLooper()); + handler.post(() -> { + ViewGroup.LayoutParams params = mWebView.getLayoutParams(); + params.height = DpUtil.dp2px(Integer.parseInt(height)); + mWebView.setLayoutParams(params); + mWebView.setVisibility(View.VISIBLE); + }); } } diff --git a/live/src/main/java/com/yunbao/live/dialog/LiveGameDialogFragment.java b/live/src/main/java/com/yunbao/live/dialog/LiveGameDialogFragment.java index dc35276ce..15b6078a2 100644 --- a/live/src/main/java/com/yunbao/live/dialog/LiveGameDialogFragment.java +++ b/live/src/main/java/com/yunbao/live/dialog/LiveGameDialogFragment.java @@ -2,33 +2,28 @@ package com.yunbao.live.dialog; import static com.yunbao.common.CommonAppContext.logger; import static com.yunbao.common.CommonAppContext.mFirebaseAnalytics; -import static com.yunbao.common.utils.RouteUtil.PATH_COIN; import android.content.DialogInterface; -import android.content.Intent; import android.graphics.Color; import android.os.Build; import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; import android.text.TextUtils; import android.util.Log; import android.view.Display; import android.view.Gravity; -import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowInsets; import android.view.WindowManager; -import android.webkit.JavascriptInterface; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.LinearLayout; +import androidx.annotation.Nullable; + import com.adjust.sdk.Adjust; import com.adjust.sdk.AdjustEvent; -import com.alibaba.android.arouter.launcher.ARouter; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.android.material.tabs.TabLayout; @@ -38,9 +33,12 @@ import com.yunbao.common.bean.ActiveModel; import com.yunbao.common.bean.JsWishBean; import com.yunbao.common.bean.LiveBean; import com.yunbao.common.dialog.AbsDialogFragment; +import com.yunbao.common.event.JavascriptInterfaceEvent; import com.yunbao.common.http.HttpCallback; import com.yunbao.common.http.live.LiveNetManager; +import com.yunbao.common.utils.Bus; import com.yunbao.common.utils.DpUtil; +import com.yunbao.common.utils.JavascriptInterfaceUtils; import com.yunbao.common.utils.L; import com.yunbao.common.utils.RouteUtil; import com.yunbao.common.utils.StringUtil; @@ -49,12 +47,13 @@ import com.yunbao.live.R; import com.yunbao.live.activity.LiveActivity; import com.yunbao.live.activity.LiveAudienceActivity; import com.yunbao.live.activity.LiveRyAnchorActivity; -import com.yunbao.live.activity.ZhuangBanActivity; import com.yunbao.live.event.LiveRoomChangeEvent; import com.yunbao.live.http.LiveHttpUtil; import com.yunbao.live.presenter.LiveRoomCheckLivePresenter; import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; import java.util.List; @@ -142,6 +141,17 @@ public class LiveGameDialogFragment extends AbsDialogFragment { window.setAttributes(params); } + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Bus.getOn(this); + } + + @Override + public void onDetach() { + super.onDetach(); + Bus.getOff(this); + } @Override public void onActivityCreated(Bundle savedInstanceState) { @@ -149,7 +159,10 @@ public class LiveGameDialogFragment extends AbsDialogFragment { mWebView = (WebView) findViewById(R.id.rlWebview); tabLayout = (TabLayout) findViewById(R.id.tabLayout); layout = (LinearLayout) findViewById(R.id.bg_layout); - mWebView.addJavascriptInterface(new JsInteration(), "androidObject"); + mWebView.addJavascriptInterface(new JavascriptInterfaceUtils(getActivity(), mWebView) + .setLiveZhuangBana(true). + setDialogClose(true). + setPageClose(false), "androidObject"); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setDomStorageEnabled(true); String appCachePath = mContext.getCacheDir().getAbsolutePath(); @@ -264,7 +277,7 @@ public class LiveGameDialogFragment extends AbsDialogFragment { url += "?"; } url += "uid=" + CommonAppConfig.getInstance().getUid() + "&token=" - + CommonAppConfig.getInstance().getToken() + "&anchorUid=" + roomId+"&active_id="+model.getActiveId(); + + CommonAppConfig.getInstance().getToken() + "&anchorUid=" + roomId + "&active_id=" + model.getActiveId(); Log.i("debug", "selectTab: " + url); mWebView.loadUrl(url); } else { @@ -280,126 +293,6 @@ public class LiveGameDialogFragment extends AbsDialogFragment { } - //js调用原生 - public class JsInteration { - @JavascriptInterface - public void androidGoTopUp() { - - ARouter.getInstance().build(PATH_COIN).withInt("p", 0).withString("tag", "&first_page=1").navigation(); - - dismiss(); - } - - @JavascriptInterface - public String androidMethod(String data) { - if (data != null && !data.isEmpty()) { - onTrickerySendGiftInto(new JsWishBean(data)); - } - return ""; - } - - @JavascriptInterface - public void AndroidAdjustPrank() { - AdjustEvent adjustEvent1 = new AdjustEvent("v1nekv"); - Adjust.trackEvent(adjustEvent1); - mFirebaseAnalytics.logEvent("FS_prank_interaction", null); - logger.logEvent("FB_prank_interaction", null); - } - - @JavascriptInterface - public String androidMethodClickUser(String userId, String liveId) { - if (mContext instanceof LiveRyAnchorActivity) { - return ""; - } - //点击用户头像 - if (!TextUtils.isEmpty(userId) && !TextUtils.isEmpty(liveId)) { - showUserDialog(userId, liveId); - } - return ""; - } - - @JavascriptInterface - public void onTrickerySendGift(String id) { - if (id != null) { - onTrickerySendGiftInto(new JsWishBean(id)); - } - } - - @JavascriptInterface - public void openWrap() { - dismiss(); - ((LiveAudienceActivity) mContext).openGiftWindow(); - } - - @JavascriptInterface - public void androidGoBack() { - dismiss(); - } - - - @JavascriptInterface - public void androidMethodLookToLive(String liveId) { - if (mContext instanceof LiveRyAnchorActivity) { - return; - } - mWebView.post(new Runnable() { - @Override - public void run() { - if (TextUtils.equals(roomId, liveId)) { - ToastUtil.show(R.string.current_live_room); - dismiss(); - } else { - //看直播 - gotoLive(liveId); - } - - } - }); - - } - - @JavascriptInterface - public void androidClickToNewH5PageView(String url) { - url = CommonAppConfig.HOST + url; - url += "?uid=" + CommonAppConfig.getInstance().getUid() + "&token=" - + CommonAppConfig.getInstance().getToken(); - Log.i("tag", url); - Intent intent = new Intent(mContext, ZhuangBanActivity.class); - intent.putExtra("url", url); - intent.putExtra("isFull", false); - mContext.startActivity(intent); - - } - - @JavascriptInterface - public void androidSignGift(String json) { - JSONObject data = JSONObject.parseObject(json); - if (data != null) { - // 恭喜您第一天簽到成功! - ((LiveAudienceActivity) mContext).openSignWindow(data.getString("icon"), - data.getString("name"), - "恭喜您" + data.getString("desc") + "簽到成功!" - ); - } - - //((LiveAudienceActivity) mContext).openSignWindow(icon, name, desc); - } - - @JavascriptInterface - public void setHeight(String height, String width) { - if ("0".equals(height)) { - return; - } - Handler handler = new Handler(Looper.getMainLooper()); - handler.post(() -> { - ViewGroup.LayoutParams params = mWebView.getLayoutParams(); - params.height = DpUtil.dp2px(Integer.parseInt(height)) - DpUtil.dp2px(30); - mWebView.setLayoutParams(params); - mWebView.setVisibility(View.VISIBLE); - }); - } - } - private LiveRoomCheckLivePresenter mCheckLivePresenter; private void gotoLive(final String live_id) { @@ -466,4 +359,59 @@ public class LiveGameDialogFragment extends AbsDialogFragment { mWebView.destroy(); } + @Subscribe(threadMode = ThreadMode.MAIN) + public void onJavascriptInterfaceEvent(JavascriptInterfaceEvent event) { + if (!TextUtils.isEmpty(event.getMethod())) { + if (TextUtils.equals(event.getMethod(), "androidGoTopUp")) { + dismiss(); + } else if (TextUtils.equals(event.getMethod(), "androidMethod")) { + if (event.getData() != null && !event.getData().isEmpty()) { + onTrickerySendGiftInto(new JsWishBean(event.getData())); + } + } else if (TextUtils.equals(event.getMethod(), "AndroidAdjustPrank")) { + AdjustEvent adjustEvent1 = new AdjustEvent("v1nekv"); + Adjust.trackEvent(adjustEvent1); + mFirebaseAnalytics.logEvent("FS_prank_interaction", null); + logger.logEvent("FB_prank_interaction", null); + } else if (TextUtils.equals(event.getMethod(), "androidMethodClickUser")) { + if (!(mContext instanceof LiveRyAnchorActivity)) { + //点击用户头像 + if (!TextUtils.isEmpty(event.getUserId()) && !TextUtils.isEmpty(event.getLiveId())) { + showUserDialog(event.getUserId(), event.getLiveId()); + } + } + } else if (TextUtils.equals(event.getMethod(), "onTrickerySendGift")) { + if (event.getUserId() != null) { + onTrickerySendGiftInto(new JsWishBean(event.getUserId())); + } + } else if (TextUtils.equals(event.getMethod(), "openWrap")) { + dismiss(); + ((LiveAudienceActivity) mContext).openGiftWindow(); + } else if (TextUtils.equals(event.getMethod(), "androidGoBack")) { + dismiss(); + } else if (TextUtils.equals(event.getMethod(), "androidMethodLookToLive")) { + if (mContext instanceof LiveRyAnchorActivity) { + return; + } + if (TextUtils.equals(roomId, event.getLiveId())) { + ToastUtil.show(R.string.current_live_room); + dismiss(); + } else { + //看直播 + gotoLive(event.getLiveId()); + } + } else if (TextUtils.equals(event.getMethod(), "androidSignGift")) { + JSONObject data = JSONObject.parseObject(event.getData()); + if (data != null) { + // 恭喜您第一天簽到成功! + ((LiveAudienceActivity) mContext).openSignWindow(data.getString("icon"), + data.getString("name"), + "恭喜您" + data.getString("desc") + "簽到成功!" + ); + + } + } + } + } + } diff --git a/live/src/main/java/com/yunbao/live/dialog/LiveHDDialogFragment.java b/live/src/main/java/com/yunbao/live/dialog/LiveHDDialogFragment.java index c446681e8..145794702 100644 --- a/live/src/main/java/com/yunbao/live/dialog/LiveHDDialogFragment.java +++ b/live/src/main/java/com/yunbao/live/dialog/LiveHDDialogFragment.java @@ -4,12 +4,9 @@ import static com.yunbao.common.CommonAppContext.logger; import static com.yunbao.common.CommonAppContext.mFirebaseAnalytics; import android.content.DialogInterface; -import android.content.Intent; import android.graphics.Color; import android.os.Build; import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; import android.text.TextUtils; import android.util.Log; import android.view.Display; @@ -19,7 +16,6 @@ import android.view.ViewGroup; import android.view.Window; import android.view.WindowInsets; import android.view.WindowManager; -import android.webkit.JavascriptInterface; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; @@ -31,7 +27,6 @@ import com.adjust.sdk.Adjust; import com.adjust.sdk.AdjustEvent; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.yunbao.common.CommonAppConfig; import com.yunbao.common.Constants; import com.yunbao.common.bean.JsWishBean; import com.yunbao.common.bean.LiveBean; @@ -39,7 +34,7 @@ import com.yunbao.common.dialog.AbsDialogFragment; import com.yunbao.common.event.JavascriptInterfaceEvent; import com.yunbao.common.http.HttpCallback; import com.yunbao.common.utils.Bus; -import com.yunbao.common.utils.DpUtil; +import com.yunbao.common.utils.JavascriptInterfaceUtils; import com.yunbao.common.utils.L; import com.yunbao.common.utils.RouteUtil; import com.yunbao.common.utils.ToastUtil; @@ -47,7 +42,6 @@ import com.yunbao.live.R; import com.yunbao.live.activity.LiveActivity; import com.yunbao.live.activity.LiveAudienceActivity; import com.yunbao.live.activity.LiveRyAnchorActivity; -import com.yunbao.live.activity.ZhuangBanActivity; import com.yunbao.live.event.LiveRoomChangeEvent; import com.yunbao.live.http.LiveHttpUtil; import com.yunbao.live.presenter.LiveRoomCheckLivePresenter; @@ -145,7 +139,10 @@ public class LiveHDDialogFragment extends AbsDialogFragment { super.onActivityCreated(savedInstanceState); mWebView = (WebView) findViewById(R.id.rlWebview); findViewById(R.id.btn_back).setOnClickListener(v -> dismiss()); - mWebView.addJavascriptInterface(new JsInteration(), "androidObject"); + mWebView.addJavascriptInterface(new JavascriptInterfaceUtils(getActivity(), mWebView) + .setLiveZhuangBana(true). + setDialogClose(true). + setPageClose(false), "androidObject"); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setDomStorageEnabled(true); String appCachePath = mContext.getCacheDir().getAbsolutePath(); @@ -212,113 +209,6 @@ public class LiveHDDialogFragment extends AbsDialogFragment { } - //js调用原生 - public class JsInteration { - - - - @JavascriptInterface - public void AndroidAdjustPrank() { - AdjustEvent adjustEvent1 = new AdjustEvent("v1nekv"); - Adjust.trackEvent(adjustEvent1); - mFirebaseAnalytics.logEvent("FS_prank_interaction", null); - logger.logEvent("FB_prank_interaction", null); - } - - @JavascriptInterface - public String androidMethodClickUser(String userId, String liveId) { - if (mContext instanceof LiveRyAnchorActivity) { - return ""; - } - //点击用户头像 - if (!TextUtils.isEmpty(userId) && !TextUtils.isEmpty(liveId)) { - showUserDialog(userId, liveId); - } - return ""; - } - - @JavascriptInterface - public void onTrickerySendGift(String id) { - if (id != null) { - onTrickerySendGiftInto(new JsWishBean(id)); - } - } - - @JavascriptInterface - public void openWrap() { - dismiss(); - ((LiveAudienceActivity) mContext).openGiftWindow(); - } - - @JavascriptInterface - public void androidGoBack() { - dismiss(); - } - - - @JavascriptInterface - public void androidMethodLookToLive(String liveId) { - if (mContext instanceof LiveRyAnchorActivity) { - return; - } - mWebView.post(new Runnable() { - @Override - public void run() { - if (TextUtils.equals(roomId, liveId)) { - ToastUtil.show(R.string.current_live_room); - dismiss(); - } else { - //看直播 - gotoLive(liveId); - } - - } - }); - - } - - @JavascriptInterface - public void androidClickToNewH5PageView(String url) { - url = CommonAppConfig.HOST + url; - url += "?uid=" + CommonAppConfig.getInstance().getUid() + "&token=" - + CommonAppConfig.getInstance().getToken(); - Log.i("tag", url); - Intent intent = new Intent(mContext, ZhuangBanActivity.class); - intent.putExtra("url", url); - intent.putExtra("isFull", false); - mContext.startActivity(intent); - - } - - @JavascriptInterface - public void androidSignGift(String json) { - JSONObject data = JSONObject.parseObject(json); - if (data != null) { - // 恭喜您第一天簽到成功! - ((LiveAudienceActivity) mContext).openSignWindow(data.getString("icon"), - data.getString("name"), - "恭喜您" + data.getString("desc") + "簽到成功!" - ); - } - - //((LiveAudienceActivity) mContext).openSignWindow(icon, name, desc); - } - - @JavascriptInterface - public void setHeight(String height, String width) { - if ("0".equals(height)) { - return; - } - Handler handler = new Handler(Looper.getMainLooper()); - handler.post(() -> { - ViewGroup.LayoutParams params = mWebView.getLayoutParams(); - params.height = DpUtil.dp2px(Integer.parseInt(height)); - mWebView.setLayoutParams(params); - mWebView.setVisibility(View.VISIBLE); - }); - } - } - private LiveRoomCheckLivePresenter mCheckLivePresenter; private void gotoLive(final String live_id) { @@ -394,8 +284,52 @@ public class LiveHDDialogFragment extends AbsDialogFragment { if (event.getData() != null && !event.getData().isEmpty()) { onTrickerySendGiftInto(new JsWishBean(event.getData())); } + } else if (TextUtils.equals(event.getMethod(), "AndroidAdjustPrank")) { + AdjustEvent adjustEvent1 = new AdjustEvent("v1nekv"); + Adjust.trackEvent(adjustEvent1); + mFirebaseAnalytics.logEvent("FS_prank_interaction", null); + logger.logEvent("FB_prank_interaction", null); + } else if (TextUtils.equals(event.getMethod(), "androidMethodClickUser")) { + //点击用户头像 + if (!(mContext instanceof LiveRyAnchorActivity)) { + //点击用户头像 + if (!TextUtils.isEmpty(event.getUserId()) && !TextUtils.isEmpty(event.getLiveId())) { + showUserDialog(event.getUserId(), event.getLiveId()); + } + } + + } else if (TextUtils.equals(event.getMethod(), "onTrickerySendGift")) { + if (event.getUserId() != null) { + onTrickerySendGiftInto(new JsWishBean(event.getUserId())); + } + } else if (TextUtils.equals(event.getMethod(), "openWrap")) { + dismiss(); + ((LiveAudienceActivity) mContext).openGiftWindow(); + } else if (TextUtils.equals(event.getMethod(), "androidGoBack")) { + dismiss(); + } else if (TextUtils.equals(event.getMethod(), "androidMethodLookToLive")) { + if (mContext instanceof LiveRyAnchorActivity) { + return; + } + if (TextUtils.equals(roomId, event.getLiveId())) { + ToastUtil.show(R.string.current_live_room); + dismiss(); + } else { + //看直播 + gotoLive(event.getLiveId()); + } + } else if (TextUtils.equals(event.getMethod(), "androidSignGift")) { + JSONObject data = JSONObject.parseObject(event.getData()); + if (data != null) { + // 恭喜您第一天簽到成功! + ((LiveAudienceActivity) mContext).openSignWindow(data.getString("icon"), + data.getString("name"), + "恭喜您" + data.getString("desc") + "簽到成功!" + ); + + } } } } - } + diff --git a/main/src/main/java/com/yunbao/main/activity/ZhuangBanActivity.java b/main/src/main/java/com/yunbao/main/activity/ZhuangBanActivity.java index eb2a2d0ad..bc3d116c8 100644 --- a/main/src/main/java/com/yunbao/main/activity/ZhuangBanActivity.java +++ b/main/src/main/java/com/yunbao/main/activity/ZhuangBanActivity.java @@ -1,6 +1,7 @@ package com.yunbao.main.activity; -import static com.yunbao.common.utils.RouteUtil.PATH_COIN; +import static com.yunbao.common.CommonAppContext.logger; +import static com.yunbao.common.CommonAppContext.mFirebaseAnalytics; import android.content.ClipData; import android.content.ClipboardManager; @@ -13,7 +14,6 @@ import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.ViewGroup; -import android.webkit.JavascriptInterface; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebSettings; @@ -25,8 +25,9 @@ import android.widget.RelativeLayout; import androidx.annotation.RequiresApi; +import com.adjust.sdk.Adjust; +import com.adjust.sdk.AdjustEvent; import com.alibaba.android.arouter.facade.annotation.Route; -import com.alibaba.android.arouter.launcher.ARouter; import com.opensource.svgaplayer.SVGACallback; import com.opensource.svgaplayer.SVGADrawable; import com.opensource.svgaplayer.SVGAImageView; @@ -35,12 +36,10 @@ import com.opensource.svgaplayer.SVGAVideoEntity; import com.yunbao.common.CommonAppConfig; import com.yunbao.common.Constants; import com.yunbao.common.activity.AbsActivity; -import com.yunbao.common.bean.IMLoginModel; -import com.yunbao.common.interfaces.CommonCallback; -import com.yunbao.common.manager.IMLoginManager; +import com.yunbao.common.event.JavascriptInterfaceEvent; import com.yunbao.common.utils.Bus; import com.yunbao.common.utils.DpUtil; -import com.yunbao.common.utils.GiftCacheUtil; +import com.yunbao.common.utils.JavascriptInterfaceUtils; import com.yunbao.common.utils.L; import com.yunbao.common.utils.RouteUtil; import com.yunbao.common.utils.SVGAViewUtils; @@ -48,7 +47,9 @@ import com.yunbao.common.utils.ToastUtil; import com.yunbao.common.utils.WordUtil; import com.yunbao.main.R; -import java.io.File; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + import java.net.MalformedURLException; import java.net.URL; @@ -174,174 +175,15 @@ public class ZhuangBanActivity extends AbsActivity { mWebView.getSettings().setJavaScriptEnabled(true); // 是否开启JS支持 mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); // 是否允许JS打开新窗口 mWebView.getSettings().setDomStorageEnabled(true); - mWebView.addJavascriptInterface(new JsInteration(), "androidObject"); + mWebView.addJavascriptInterface(new JavascriptInterfaceUtils(mContext, mWebView) + .setPageClose(true) + .setLiveZhuangBana(false), "androidObject"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } mWebView.loadUrl(url); } - //js调用原生 - public class JsInteration { - @JavascriptInterface - public void onBack() { - finish(); - } - - @JavascriptInterface - public void androidZBan(String svg) { - String[] arr = svg.split("/"); - String id = arr[arr.length - 1]; - String url1; - if (!svg.contains("http")) { - url1 = CommonAppConfig.HOST + svg; - } else { - url1 = svg; - } - GiftCacheUtil.getFile(Constants.GIF_CAR_PREFIX + id, url1, "0", new CommonCallback() { - @Override - public void callback(File bean) { - - } - }); - } - - @JavascriptInterface - public void androidOpeningNoble(String svg) { - try { - new SVGAParser(mContext).parse(new URL(svg), new SVGAParser.ParseCompletion() { - @Override - public void onComplete(SVGAVideoEntity videoItem) { - svga.setVisibility(View.VISIBLE); - SVGADrawable drawable = new SVGADrawable(videoItem); - svga.setImageDrawable(drawable); - svga.setLoops(1); - SVGAViewUtils.playEndClear(svga); - } - - @Override - public void onError() { - } - }); - svga.setCallback(new SVGACallback() { - @Override - public void onPause() { - - } - - @Override - public void onFinished() { - svga.clear(); - svga.setVisibility(View.GONE); - } - - @Override - public void onRepeat() { - - } - - @Override - public void onStep(int i, double v) { - - } - }); - } catch (MalformedURLException e) { - e.printStackTrace(); - } - } - - @JavascriptInterface - public void androidGoBack() { -// Log.e("Zhu", "svg" +svg); - ZhuangBanActivity.this.finish(); - } - - @JavascriptInterface - public void androidGoToRe() { - ARouter.getInstance().build(PATH_COIN).withInt("p", 1).navigation(); - } - - @JavascriptInterface - public void gotoHomePage(String indexStr) { - Log.e("tgasss", "indexStr2" + indexStr); - if (!"".equals(indexStr) && indexStr != null) { - int index = -1; - if ("0".equals(indexStr)) { - index = 0; - } else if ("1".equals(indexStr)) { - index = 1; - } else if ("2".equals(indexStr)) { - index = 2; - } else if ("3".equals(indexStr)) { - index = 3; - } - if (index != -1) { - finish(); - Constants.isShowPage = index; - } - } - } - - @JavascriptInterface - public void openWebView(String url) { - Constants.isTitle = false; - RouteUtil.forwardZhuangBanActivity(url); - } - - @JavascriptInterface - public void openWebViewTitle(String url) { - Constants.isTitle = true; - RouteUtil.forwardZhuangBanActivity(url); - } - - @JavascriptInterface - public void androidClickToNewH5PageView(String url) { - url = CommonAppConfig.HOST + url; - url += "?uid=" + CommonAppConfig.getInstance().getUid() + "&token=" - + CommonAppConfig.getInstance().getToken(); - Log.i("tag", url); - Intent intent = new Intent(mContext, com.yunbao.live.activity.ZhuangBanActivity.class); - intent.putExtra("url", url); - intent.putExtra("isFull", false); - mContext.startActivity(intent); - - } - - /** - * 新跳转 - * - * @param url 跳转的url - * @param title 有标题的展示标题 - */ - @JavascriptInterface - public void androidClickToNewH5PageView(String url, String title) { - url = CommonAppConfig.HOST + url; - url += "?uid=" + CommonAppConfig.getInstance().getUid() + "&token=" - + CommonAppConfig.getInstance().getToken(); - Log.i("tag", url); - Intent intent = new Intent(mContext, com.yunbao.live.activity.ZhuangBanActivity.class); - intent.putExtra("url", url); - intent.putExtra("title", title); - mContext.startActivity(intent); - - } - - /** - * 设置超皇字体颜色 - * - * @param startColor 渐变色 - * @param endColor 渐变色 - * @param contentColor 内容颜色 - */ - @JavascriptInterface - public void androidSetColor(String startColor, String endColor, String contentColor) { - IMLoginModel model = IMLoginManager.get(mContext).getUserInfo(); - model.setStartColor("#" + startColor) - .setEndColor("#" + endColor) - .setContentColor("#" + contentColor); - IMLoginManager.get(mContext).upDataUserInfo(model); - } - } private void openImageChooserActivity(ValueCallback valueCallback) { mValueCallback = valueCallback; @@ -474,4 +316,57 @@ public class ZhuangBanActivity extends AbsActivity { finish(); } } + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onJavascriptInterfaceEvent(JavascriptInterfaceEvent event) { + if (!TextUtils.isEmpty(event.getMethod())) { + if (TextUtils.equals(event.getMethod(), "androidOpeningNoble")) { + try { + new SVGAParser(mContext).parse(new URL(event.getSvg()), new SVGAParser.ParseCompletion() { + @Override + public void onComplete(SVGAVideoEntity videoItem) { + svga.setVisibility(View.VISIBLE); + SVGADrawable drawable = new SVGADrawable(videoItem); + svga.setImageDrawable(drawable); + svga.setLoops(1); + SVGAViewUtils.playEndClear(svga); + } + + @Override + public void onError() { + } + }); + svga.setCallback(new SVGACallback() { + @Override + public void onPause() { + } + + @Override + public void onFinished() { + svga.clear(); + svga.setVisibility(View.GONE); + } + + @Override + public void onRepeat() { + } + + @Override + public void onStep(int i, double v) { + } + }); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } else if (TextUtils.equals(event.getMethod(), "AndroidAdjustConsume")) { + mFirebaseAnalytics.logEvent("FS_event_interaction", null); + logger.logEvent("FB_event_interaction", null); + AdjustEvent adjustEvent1 = new AdjustEvent("rq2c5n"); + Adjust.trackEvent(adjustEvent1); + } + } + + } + + }