Skip to content

Commit

Permalink
bindwebview in bridge.
Browse files Browse the repository at this point in the history
  • Loading branch information
b612lodger committed Dec 25, 2013
1 parent b654c9c commit 82aa546
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,21 @@ public class WebviewBridge implements OnResponseListener {

private AsyncServerStub mServerStub;

public WebviewBridge(WebView webview) {

/**
* Constructor
*/
public WebviewBridge() {
mServerStub = new AsyncServerStub();
mServerStub.setOnResponseListener(this);
}


/**
* Bind Webview
* @param webview
*/
public void bindWebView(WebView webview) {
if(webview == null) {
Log.e(TAG, "webview can not be null");
return;
Expand Down Expand Up @@ -54,16 +68,19 @@ public void onPageFinished(WebView view, String url) {
mWebView.loadUrl("javascript:" + webviewJsCode);
}
});
mServerStub = new AsyncServerStub();
mServerStub.setOnResponseListener(this);

WebSettings webSettings = webview.getSettings();
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

mWebView.addJavascriptInterface(getJavascriptInterface(), "jsonRpc");
}


/**
* Register Method.
* @param methodName
* @param method
*/
public void registMethod(String methodName, MethodHandler<?, ?> method) {
if(mServerStub != null) {
mServerStub.registMethod(methodName, method);
Expand All @@ -87,10 +104,13 @@ public void request(String request) {
};
}


@Override
public void onResponse(String response) {
Log.v(TAG, "Response : [" + response + "]");
mWebView.loadUrl("javascript:rpcStub.response(" + response + ")");
if(mWebView != null) {
mWebView.loadUrl("javascript:rpcStub.response(" + response + ")");
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public void onCreate(Bundle savedInstanceState) {
private void initServerStub() {

mWebView = (WebView) findViewById(R.id.webview);
mWebviewBridge = new WebviewBridge(mWebView);
mWebviewBridge = new WebviewBridge();
mWebviewBridge.bindWebView(mWebView);
mWebviewBridge.registMethod("subtract", new MethodHandler<TestParam, Integer>() {

@Override
Expand Down

0 comments on commit 82aa546

Please sign in to comment.