Js调用原生整改
This commit is contained in:
@@ -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") + "簽到成功!"
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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") + "簽到成功!"
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user