天津出行司机端 v6.9.3版本的 MD5 值为:b7a18e0f0e865860fd80aa9ed7cf4ae1
以下内容为反编译后的 BridgeWebView.java 源代码,内容仅作参考
package com.github.lzyzsd.jsbridge;
import android.content.Context;
import android.os.Build;
import android.os.Looper;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.webkit.WebView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BridgeWebView extends WebView implements WebViewJavascriptBridge {
public static final String toLoadJs = "WebViewJavascriptBridge.js";
private final String TAG;
BridgeHandler defaultHandler;
Map<String, BridgeHandler> messageHandlers;
Map<String, CallBackFunction> responseCallbacks;
private List<Message> startupMessage;
private long uniqueId;
public List<Message> getStartupMessage() {
return this.startupMessage;
}
public void setStartupMessage(List<Message> startupMessage) {
this.startupMessage = startupMessage;
}
public BridgeWebView(Context context, AttributeSet attrs) {
super(context, attrs);
this.TAG = "BridgeWebView";
this.responseCallbacks = new HashMap();
this.messageHandlers = new HashMap();
this.defaultHandler = new DefaultHandler();
this.startupMessage = new ArrayList();
this.uniqueId = 0L;
init();
}
public BridgeWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.TAG = "BridgeWebView";
this.responseCallbacks = new HashMap();
this.messageHandlers = new HashMap();
this.defaultHandler = new DefaultHandler();
this.startupMessage = new ArrayList();
this.uniqueId = 0L;
init();
}
public BridgeWebView(Context context) {
super(context);
this.TAG = "BridgeWebView";
this.responseCallbacks = new HashMap();
this.messageHandlers = new HashMap();
this.defaultHandler = new DefaultHandler();
this.startupMessage = new ArrayList();
this.uniqueId = 0L;
init();
}
public void setDefaultHandler(BridgeHandler handler) {
this.defaultHandler = handler;
}
private void init() {
setVerticalScrollBarEnabled(false);
setHorizontalScrollBarEnabled(false);
getSettings().setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT >= 19) {
WebView.setWebContentsDebuggingEnabled(true);
}
setWebViewClient(generateBridgeWebViewClient());
}
protected BridgeWebViewClient generateBridgeWebViewClient() {
return new BridgeWebViewClient(this);
}
public void handlerReturnData(String url) {
String functionName = BridgeUtil.getFunctionFromReturnUrl(url);
CallBackFunction f = this.responseCallbacks.get(functionName);
String data = BridgeUtil.getDataFromReturnUrl(url);
if (f != null) {
f.onCallBack(data);
this.responseCallbacks.remove(functionName);
}
}
@Override
public void send(String data) {
send(data, null);
}
@Override
public void send(String data, CallBackFunction responseCallback) {
doSend(null, data, responseCallback);
}
private void doSend(String handlerName, String data, CallBackFunction responseCallback) {
Message m = new Message();
if (!TextUtils.isEmpty(data)) {
m.setData(data);
}
if (responseCallback != null) {
StringBuilder sb = new StringBuilder();
long j = this.uniqueId + 1;
this.uniqueId = j;
sb.append(j);
sb.append("_");
sb.append(SystemClock.currentThreadTimeMillis());
String callbackStr = String.format("JAVA_CB_%s", sb.toString());
this.responseCallbacks.put(callbackStr, responseCallback);
m.setCallbackId(callbackStr);
}
if (!TextUtils.isEmpty(handlerName)) {
m.setHandlerName(handlerName);
}
queueMessage(m);
}
public void queueMessage(Message m) {
List<Message> list = this.startupMessage;
if (list != null) {
list.add(m);
} else {
dispatchMessage(m);
}
}
public void dispatchMessage(Message m) {
String messageJson = m.toJson();
String javascriptCommand = String.format("javascript:WebViewJavascriptBridge._handleMessageFromNative('%s');", messageJson.replaceAll("(\\\\)([^utrn])", "\\\\\\\\$1$2").replaceAll("(?<=[^\\\\])(\")", "\\\\\""));
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
loadUrl(javascriptCommand);
}
}
public void flushMessageQueue() {
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
loadUrl("javascript:WebViewJavascriptBridge._fetchQueue();", new CallBackFunction() {
@Override
public void onCallBack(String data) {
CallBackFunction responseFunction;
BridgeHandler handler;
try {
List<Message> list = Message.toArrayList(data);
if (list == null || list.size() == 0) {
return;
}
for (int i = 0; i < list.size(); i++) {
Message m = list.get(i);
String responseId = m.getResponseId();
if (!TextUtils.isEmpty(responseId)) {
CallBackFunction function = BridgeWebView.this.responseCallbacks.get(responseId);
String responseData = m.getResponseData();
function.onCallBack(responseData);
BridgeWebView.this.responseCallbacks.remove(responseId);
} else {
final String callbackId = m.getCallbackId();
if (!TextUtils.isEmpty(callbackId)) {
responseFunction = new CallBackFunction() {
@Override
public void onCallBack(String data2) {
Message responseMsg = new Message();
responseMsg.setResponseId(callbackId);
responseMsg.setResponseData(data2);
BridgeWebView.this.queueMessage(responseMsg);
}
};
} else {
responseFunction = new CallBackFunction() {
@Override
public void onCallBack(String data2) {
}
};
}
if (!TextUtils.isEmpty(m.getHandlerName())) {
handler = BridgeWebView.this.messageHandlers.get(m.getHandlerName());
} else {
handler = BridgeWebView.this.defaultHandler;
}
if (handler != null) {
handler.handler(m.getData(), responseFunction);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
public void loadUrl(String jsUrl, CallBackFunction returnCallback) {
loadUrl(jsUrl);
this.responseCallbacks.put(BridgeUtil.parseFunctionName(jsUrl), returnCallback);
}
public void registerHandler(String handlerName, BridgeHandler handler) {
if (handler != null) {
this.messageHandlers.put(handlerName, handler);
}
}
public void callHandler(String handlerName, String data, CallBackFunction callBack) {
doSend(handlerName, data, callBack);
}
}