Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce Lee committed Feb 28, 2015
1 parent 3cb495e commit 4164469
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
4 changes: 2 additions & 2 deletions example/src/main/assets/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@
}
console.log('JS responding with', data)
responseCallback(data)
})
});

bridge.registerHandler("functionInJs", function(data, responseCallback) {
document.getElementById("show").innerHTML = ("data from Java: = " + data)
var responseData = "Javascript Says Right back aka!"
responseCallback(responseData)
})
});
})
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected void onCreate(Bundle savedInstanceState) {

button.setOnClickListener(this);

webView.initContext(new DefaultHandler());
webView.setDefaultHandler(new DefaultHandler());

webView.setWebChromeClient(new WebChromeClient() {

Expand Down Expand Up @@ -88,12 +88,12 @@ public void handler(String data, CallBackFunction function) {
user.location = location;
user.name = "Bruce";

webView.callHandler(new Gson().toJson(user), new CallBackFunction() {
webView.callHandler("functionInJs", new Gson().toJson(user), new CallBackFunction() {
@Override
public void onCallBack(String data) {

}
}, "functionInJs");
});

}

Expand All @@ -118,15 +118,15 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
@Override
public void onClick(View v) {
if (button.equals(v)) {
webView.callHandler("data from Java", new CallBackFunction() {
webView.callHandler("functionInJs", "data from Java", new CallBackFunction() {

@Override
public void onCallBack(String data) {
// TODO Auto-generated method stub
Log.i(TAG, "reponse data from js " + data);
}

}, "functionInJs");
});
}

}
Expand Down
10 changes: 5 additions & 5 deletions library/src/main/assets/WebViewJavascriptBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
var receivedMessages = receiveMessageQueue;
receiveMessageQueue = null;
for (var i = 0; i < receivedMessages.length; i++) {
_dispatchMessageFromObjC(receivedMessages[i]);
_dispatchMessageFromNative(receivedMessages[i]);
}
}

Expand Down Expand Up @@ -97,7 +97,7 @@
}

//提供给native使用,
function _dispatchMessageFromObjC(messageJSON) {
function _dispatchMessageFromNative(messageJSON) {
setTimeout(function() {
var message = JSON.parse(messageJSON);
var responseCallback;
Expand Down Expand Up @@ -138,11 +138,11 @@
}

//提供给native调用,receiveMessageQueue 在会在页面加载完后赋值为null,所以
function _handleMessageFromObjC(messageJSON) {
function _handleMessageFromNative(messageJSON) {
if (receiveMessageQueue) {
receiveMessageQueue.push(messageJSON);
} else {
_dispatchMessageFromObjC(messageJSON);
_dispatchMessageFromNative(messageJSON);
}
}

Expand All @@ -152,7 +152,7 @@
registerHandler: registerHandler,
callHandler: callHandler,
_fetchQueue: _fetchQueue,
_handleMessageFromObjC: _handleMessageFromObjC
_handleMessageFromNative: _handleMessageFromNative
};

var doc = document;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class BridgeUtil {
final static String SPLIT_MARK = "/";

final static String CALLBACK_ID_FORMAT = "JAVA_CB_%s";
final static String JS_HANDLE_MESSAGE_FROM_JAVA = "javascript:WebViewJavascriptBridge._handleMessageFromObjC('%s');";
final static String JS_HANDLE_MESSAGE_FROM_JAVA = "javascript:WebViewJavascriptBridge._handleMessageFromNative('%s');";
final static String JS_FETCH_QUEUE_FROM_JAVA = "javascript:WebViewJavascriptBridge._fetchQueue();";
public final static String JAVASCRIPT_STR = "javascript:";

Expand Down
39 changes: 18 additions & 21 deletions library/src/main/java/com/github/lzyzsd/jsbridge/BridgeWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class BridgeWebView extends WebView implements WebViewJavascriptBridge {

private final String TAG = "BridgeWebView";

String toLoadJs = null;
String toLoadJs = "WebViewJavascriptBridge.js";
Map<String, CallBackFunction> responseCallbacks = new HashMap<String, CallBackFunction>();
Map<String, BridgeHandler> messageHandlers = new HashMap<String, BridgeHandler>();
BridgeHandler defaultHandler = new DefaultHandler();
Expand All @@ -34,33 +34,30 @@ public class BridgeWebView extends WebView implements WebViewJavascriptBridge {

public BridgeWebView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
init();
}

public BridgeWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
init();
}

public BridgeWebView(Context context) {
super(context);
init(context);
init();
}

/**
*
* @param handler
* 默认的handler,负责处理js端没有指定handlerName的消息,若js端有指定handlerName,
* 则由native端注册的指定处理
* default handler,handle messages send by js without assigned handler name,
* if js message has handler name, it will be handled by named handlers registered by native
*/
public void initContext(BridgeHandler handler) {
this.toLoadJs = "WebViewJavascriptBridge.js";
if (handler != null) {
this.defaultHandler = handler;
}
public void setDefaultHandler(BridgeHandler handler) {
this.defaultHandler = handler;
}

private void init(Context context) {
private void init() {
this.setVerticalScrollBarEnabled(false);
this.setHorizontalScrollBarEnabled(false);
this.getSettings().setJavaScriptEnabled(true);
Expand Down Expand Up @@ -138,10 +135,10 @@ public void send(String data) {

@Override
public void send(String data, CallBackFunction responseCallback) {
doSend(data, responseCallback, null);
doSend(null, data, responseCallback);
}

private void doSend(String data, CallBackFunction responseCallback, String handlerName) {
private void doSend(String handlerName, String data, CallBackFunction responseCallback) {
Message m = new Message();
if (!TextUtils.isEmpty(data)) {
m.setData(data);
Expand Down Expand Up @@ -203,7 +200,7 @@ public void onCallBack(String data) {
responseCallbacks.remove(responseId);
} else {
CallBackFunction responseFunction = null;
// 是否是callbackId
// if had callbackId
final String callbackId = m.getCallbackId();
if (!TextUtils.isEmpty(callbackId)) {
responseFunction = new CallBackFunction() {
Expand Down Expand Up @@ -243,7 +240,7 @@ public void loadUrl(String jsUrl, CallBackFunction returnCallback) {
}

/**
* 注册handler,方便web调用
* register handler,so that javascript can call it
*
* @param handlerName
* @param handler
Expand All @@ -255,13 +252,13 @@ public void registerHandler(String handlerName, BridgeHandler handler) {
}

/**
* 调用web的handler
*
* call javascript registered handler
*
* @param handlerName
* @param data
* @param callBack
* @param handlerName
*/
public void callHandler(String data, CallBackFunction callBack, String handlerName) {
doSend(data, callBack, handlerName);
public void callHandler(String handlerName, String data, CallBackFunction callBack) {
doSend(handlerName, data, callBack);
}
}
6 changes: 0 additions & 6 deletions library/src/main/java/com/github/lzyzsd/jsbridge/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ public class Message {
private String data; //data of message
private String handlerName; //name of handler

private final static String CALLBACK_ID_STR = "callbackId";
private final static String RESPONSE_ID_STR = "responseId";
private final static String RESPONSE_DATA_STR = "responseData";
private final static String DATA_STR = "data";
private final static String HANDLER_NAME_STR = "handlerName";

public String getResponseId() {
return responseId;
}
Expand Down

0 comments on commit 4164469

Please sign in to comment.