public class JSInterface {
public static String INTERFACE_NAME = "xcwlandroid";
private static final int REQUEST1_LOGIN = 2000;
private final WebView webview;
private BrowserActivity browserActivity;
private long mLastClickTimeMillis = 0;
public class JSCallParam implements Serializable {
String callname;
String callid;
String calldata;//json format
}
public class JSResult implements Serializable {
public String callid;
public int code;
public String result;
public String message;
public JSResult(String callid, int code, String result, String msg) {
this.callid = callid;
this.code = code;
this.result = result;
this.message = msg;
}
}
public JSInterface(BrowserActivity browserActivity, WebView webView) {
this.webview = webView;
this.browserActivity = browserActivity;
}
@JavascriptInterface
public void call(final String paramJson) {
Log4a.e("js call param=" + paramJson);
try {
final JSCallParam jsCallParam = GsonKit.fromJson(paramJson, JSCallParam.class);
this.browserActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
handle(jsCallParam);
} catch (Exception e) {
Log4a.e("js call error. paramJson=" + paramJson);
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
Log4a.e("js call error. paramJson=" + paramJson);
}
}
private void handle(JSCallParam jsCallParam) throws Exception {
if (jsCallParam == null) {
return;
}
Log4a.e("js handle " + jsCallParam.callname + "||" + jsCallParam.callid + "||" + jsCallParam.calldata);
if (TextUtils.isEmpty(jsCallParam.callname) || TextUtils.isEmpty(jsCallParam.callid)) {
Log4a.e("js handle error. callname or callid not exist");
return;
}
JSONObject jsonObject;
String url;
switch (jsCallParam.callname) {
case "showBackBar":
this.browserActivity.showTitleBar();
break;
case "getToken":
String token = CacheService.getInstance().getToken();
//token null?
if (TextUtils.isEmpty(token)) {
JSResult jsResult = new JSResult(jsCallParam.callid, 400, "", "no login");
callBackToJs(jsResult);
} else {
JSResult jsResult = new JSResult(jsCallParam.callid, 200, token, "success");
callBackToJs(jsResult);
}
break;
case "getUserId":
String userId = CacheService.getInstance().getToken();
if (TextUtils.isEmpty(userId)) {
ARouter.getInstance().build(Constants.RouteUrl.USER_LOGIN).navigation(browserActivity,REQUEST1_LOGIN);
} else {
JSResult jsResult = new JSResult(jsCallParam.callid, 200, userId, "success");
callBackToJs(jsResult);
}
break;
case "closeWindow":
this.browserActivity.finish();
break;
case "hideBackBar":
this.browserActivity.hideTitleBar();
break;
case "share":
jsonObject = new JSONObject(jsCallParam.calldata);
String link = jsonObject.getString("link");
String imgUrl = jsonObject.getString("imgUrl");
String title = jsonObject.getString("title");
String desc = jsonObject.getString("desc");
showShare(jsCallParam.callid, title, desc, link, imgUrl);
break;
case "login":
ARouter.getInstance().build(Constants.RouteUrl.USER_LOGIN).withString("callid", jsCallParam.callid).navigation(this.browserActivity, REQUEST1_LOGIN);
break;
case "showMenu":
RwhMenuModel rwhMenuModel = GsonKit.fromJson(jsCallParam.calldata, RwhMenuModel.class);
List<WebMenu> list = new ArrayList<>();
if (rwhMenuModel.list.size() == 0) {
return;
}
for (RwhMenuModel.RMenu menu : rwhMenuModel.list) {
list.add(new WebMenu(menu.title, "", 0, menu.js));
}
this.browserActivity.setMenuItem(list);
break;
case "urlRoute":
// needlogin int型 0:不登录 1:登录(不跳url,如果客户端已经登录则直接传给我userId)
// closePage int型 0:不关闭 1:关闭,返回的话就跳过该页面,返回到上一页面(不登录时候才判断)
// 登录的时候不看closepage
jsonObject = new JSONObject(jsCallParam.calldata);
boolean needLogin = false;
if (jsonObject.has("needLogin")) {
needLogin = jsonObject.getBoolean("needLogin");
}
boolean closeBrowser = false;
if (jsonObject.has("closePage")) {
//关闭当前浏览器,然后再眺新的路由
closeBrowser = jsonObject.getBoolean("closePage");
}
url = jsonObject.getString("url");
if (needLogin) {
if (CacheService.getInstance().isLogin()) {
RouteDispathActivity.route(this.browserActivity, url);
} else {
ARouter.getInstance().build(Constants.RouteUrl.USER_LOGIN).withString("url", url).navigation();
}
} else {
if (url.startsWith("http")) {
if (closeBrowser) {
RouteDispathActivity.route(this.browserActivity, url);
browserActivity.finish();
} else {
webview.loadUrl(url);
}
} else {
RouteDispathActivity.route(this.browserActivity, url);
}
}
if (closeBrowser) {
browserActivity.finish();
}
break;
case "showLoading":
jsonObject = new JSONObject(jsCallParam.calldata);
int showLoad = jsonObject.getInt("isActivity");
if (showLoad == 1) {
this.browserActivity.showLoading(true);
} else if (showLoad == 0) {
this.browserActivity.cancelLoading();
JSResult jsResult = new JSResult(jsCallParam.callid, 200, "", "success");
callBackToJs(jsResult);
}
break;
case "bindBack":
jsonObject = new JSONObject(jsCallParam.calldata);
int type = -1;
if (jsonObject.has("type")) {
type = jsonObject.getInt("type");
}
String url3 = "";
if (jsonObject.has("url")) {
url3 = jsonObject.getString("url");
}
browserActivity.setBackControl(type, url3);
break;
case "authen":
// 芝麻认证类型与状态
// authStatus:0: 未认证; 1: 认证中; 2: 认证失败; 3:认证通过; 4:认证失效
// authType: 1:芝麻认证; 2:学历认证; 3:征信报告; 4:社保认证; 5:公积金认证
jsonObject = new JSONObject(jsCallParam.calldata);
String authStatus = jsonObject.getString("authStatus");
String authType = jsonObject.getString("authType");
String url1 = jsonObject.getString("url");
String message = jsonObject.getString("message");
if (url1.startsWith("http") || url1.startsWith("https")) {
ARouter.getInstance().build(Constants.RouteUrl.CORE_BROWSER)
.withString("url", url1)
.navigation(browserActivity);
} else {
String replace = url1.replace("xc://www.xiaochong.walian", "");
ARouter.getInstance().build(replace)
.withString("authStatus", authStatus)
.withString("authType", authType)
.withString("message", message)
.navigation(browserActivity);
}
break;
case "getArea":
browserActivity.showAreaDialog(jsCallParam.callid, new IAreaListener() {
@Override
public void onSelect(String callid, String addr) {
JSResult jsResult = new JSResult(callid, 200, addr, "success");
callBackToJs(jsResult);
}
});
break;
case "navTitles":
JsMenu jsMenu = GsonKit.fromJson(jsCallParam.calldata,JsMenu.class);
this.browserActivity.setJsMenu(jsMenu);
break;
default:
Log4a.e("not support " + jsCallParam.callname);
JSResult jsResult = new JSResult(jsCallParam.callid, 400, "", "not support");
callBackToJs(jsResult);
break;
}
}
public static interface IAreaListener {
void onSelect(String callid, String addr);
}
public void callBackToJs(JSResult jsResult) {
Log4a.e("callback js running");
if (jsResult == null) {
return;
}
if (TextUtils.isEmpty(jsResult.callid)) {
Log4a.v("callback js error,callid empty");
return;
}
if (jsResult.code != 200 && jsResult.code != 400) {
Log4a.v("callback js error,code=" + jsResult.code);
return;
}
String backdata = GsonKit.toJson(jsResult);
final String url = "javascript:xcqb.callbacks[\"" + jsResult.callid + "\"](" + backdata + ")";
Log4a.e("======callback=" + url);
this.browserActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
webview.loadUrl(url);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST1_LOGIN:
//js调用login
//login back
if (resultCode == Activity.RESULT_OK) {
String token = CacheService.getInstance().getToken();
//token null?
if (TextUtils.isEmpty(token)) {
JSResult jsResult = new JSResult(data.getStringExtra("callid"), 400, "", "login failed");
callBackToJs(jsResult);
} else {
JSResult jsResult = new JSResult(data.getStringExtra("callid"), 200, token, "success");
callBackToJs(jsResult);
}
}
break;
}
}
public void showShare(final String callid, String title, String desc, String link, String imgUrl) {
{
ShareHelper.getInstance(browserActivity).openShare(link, title, desc, imgUrl, new UMShareListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
}
@Override
public void onResult(SHARE_MEDIA share_media) {
JSResult jsResult = new JSResult(callid, 200, "", "success");
callBackToJs(jsResult);
}
@Override
public void onError(SHARE_MEDIA share_media, Throwable throwable) {
if (throwable != null && throwable.getMessage() != null && throwable.getMessage().contains("没有安装应用")) {
JSResult jsResult = new JSResult(callid, 400, "", "没有安装对应的应用程序");
callBackToJs(jsResult);
} else {
JSResult jsResult = new JSResult(callid, 400, "", "分享失败");
callBackToJs(jsResult);
}
}
@Override
public void onCancel(SHARE_MEDIA share_media) {
JSResult jsResult = new JSResult(callid, 400, "", "分享取消");
callBackToJs(jsResult);
}
});
}
}
}
Android-jsInterface
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 问题描述: Error:Could not find com.android.tools.build:gradle...
- 这个是写kotlin单模块测试时出现的问题,解决方法是: 1.在gradle.properties中添加andro...
- 1.主要LOG 信息 01-02 01:34:11.010 I/SystemServer( 1059): User...
- 华为手机8.0版本安装成功,但是点击桌面logo一直没反应,打不开 都还不知道怎么回事?黑人问号式:最后换了个启动...