Js调用原生整改
This commit is contained in:
@@ -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<File>() {
|
||||
@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<Uri> 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user