-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 346e91a
Showing
26 changed files
with
915 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
WebViewBridge/bin/* | ||
WebViewBridge/gen/* | ||
WebViewBridge/.settings/* | ||
|
||
WebViewBridgeTest/bin/* | ||
WebViewBridgeTest/gen/* | ||
WebViewBridgeTest/.settings/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="src" path="gen"/> | ||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/> | ||
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/> | ||
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/> | ||
<classpathentry kind="output" path="bin/classes"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>WebViewBridgeAndroid</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>com.android.ide.eclipse.adt.ApkBuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>com.android.ide.eclipse.adt.AndroidNature</nature> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="kr.co.b612lodger.webViewBridge" | ||
android:versionCode="1" | ||
android:versionName="0.2" > | ||
|
||
<uses-sdk | ||
android:minSdkVersion="9" | ||
android:targetSdkVersion="19" /> | ||
|
||
<application | ||
android:allowBackup="true"> | ||
</application> | ||
|
||
</manifest> |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# To enable ProGuard in your project, edit project.properties | ||
# to define the proguard.config property as described in that file. | ||
# | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in ${sdk.dir}/tools/proguard/proguard-android.txt | ||
# You can edit the include path and order by changing the ProGuard | ||
# include property in project.properties. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# Add any project specific keep options here: | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# This file is automatically generated by Android Tools. | ||
# Do not modify this file -- YOUR CHANGES WILL BE ERASED! | ||
# | ||
# This file must be checked in Version Control Systems. | ||
# | ||
# To customize properties used by the Ant build system edit | ||
# "ant.properties", and override values to adapt the script to your | ||
# project structure. | ||
# | ||
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): | ||
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt | ||
|
||
# Project target. | ||
target=android-19 | ||
android.library=true |
82 changes: 82 additions & 0 deletions
82
WebViewBridge/src/kr/co/b612lodger/jsonRpc/AsyncServerStub.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package kr.co.b612lodger.jsonRpc; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import android.os.Handler; | ||
import android.os.HandlerThread; | ||
|
||
public class AsyncServerStub extends ServerStub { | ||
|
||
protected Handler mMethodHandler; | ||
|
||
protected OnResponseListener mOnResponseListener; | ||
|
||
public AsyncServerStub() { | ||
super(); | ||
HandlerThread methodHandlerThread = new HandlerThread("MethodHandlerThread"); | ||
methodHandlerThread.start(); | ||
mMethodHandler = new Handler(methodHandlerThread.getLooper()); | ||
} | ||
|
||
|
||
public String execute(final String request, int timeoutMillies) throws Exception { | ||
ExecutorService executor = Executors.newFixedThreadPool(1); | ||
Future<String> future = executor.submit(new Callable<String>() { | ||
@Override | ||
public String call() throws Exception { | ||
Thread.sleep(1); //To be interrupted. | ||
return AsyncServerStub.super.execute(request); | ||
} | ||
}); | ||
|
||
String response; | ||
try { | ||
response = future.get(10000, TimeUnit.MILLISECONDS); | ||
|
||
} catch (Exception e) { | ||
future.cancel(true); | ||
throw e; | ||
} | ||
return response; | ||
} | ||
|
||
|
||
public void executeAsync(final String request) { | ||
final Handler callerThreadHandler = new Handler(); | ||
mMethodHandler.post(new Runnable() { | ||
|
||
@Override | ||
public void run() { | ||
final String[] response = new String[1]; | ||
try { | ||
response[0] = AsyncServerStub.this.execute(request, 10000); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
callerThreadHandler.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
if(mOnResponseListener != null) { | ||
mOnResponseListener.onResponse(response[0]); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
return; | ||
} | ||
|
||
public void setOnResponseListener(OnResponseListener onResponseListener) { | ||
mOnResponseListener = onResponseListener; | ||
} | ||
|
||
public static interface OnResponseListener { | ||
public void onResponse(String response); | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
WebViewBridge/src/kr/co/b612lodger/jsonRpc/ServerStub.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package kr.co.b612lodger.jsonRpc; | ||
|
||
import java.util.HashMap; | ||
|
||
import kr.co.b612lodger.jsonRpc.core.MethodHandler; | ||
import kr.co.b612lodger.jsonRpc.core.RequestResponseFactory; | ||
import kr.co.b612lodger.jsonRpc.model.Request; | ||
import kr.co.b612lodger.jsonRpc.model.Response; | ||
|
||
/** | ||
* Server-side(android native) RPC Stub. | ||
* It handles JSON-RPC v2 requests. | ||
* | ||
* InjectMethodHandlers with specific RequestParams & method name. | ||
* injected method handlers will handle requests in json string format. | ||
* | ||
* @author apple | ||
* | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
public class ServerStub { | ||
|
||
/** | ||
* MethodHandler hash map. | ||
* execute function of given methodHandler will be processed according to request's method name. | ||
* | ||
*/ | ||
protected HashMap<String, MethodHandler> mMethodHandlerMap; | ||
|
||
protected RequestResponseFactory mRequestResponseFactory; | ||
|
||
|
||
/** | ||
* Initializer | ||
*/ | ||
public ServerStub() { | ||
mMethodHandlerMap = new HashMap<String, MethodHandler>(); | ||
mRequestResponseFactory = new RequestResponseFactory(this); | ||
} | ||
|
||
|
||
/** | ||
* Register method to handle request. | ||
* the method's execute function with methodName will be invoked according to request's method name. | ||
* | ||
* @param methodName same string value to request's method name you desire to handle. | ||
* @param method | ||
*/ | ||
public void registMethod(String methodName, MethodHandler method) { | ||
if(method == null) { | ||
return; | ||
} | ||
mMethodHandlerMap.put(methodName, method); | ||
} | ||
|
||
|
||
/** | ||
* Execute given string in JSON-RPC v2 format | ||
* | ||
* @param request string representation of JSON-RPC v2 | ||
* @return | ||
*/ | ||
public String execute(String request) { | ||
|
||
Request<?>[] requests = mRequestResponseFactory.makeRequest(request); | ||
Response<?>[] responses = new Response[requests.length]; | ||
for(int i = 0; i < requests.length; i++) { | ||
responses[i] = execute(requests[i]); | ||
} | ||
return mRequestResponseFactory.deserializeResponse(responses); | ||
} | ||
|
||
|
||
/** | ||
* Execute single request | ||
* | ||
* @param request | ||
* @return | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
private Response<?> execute(Request request) { | ||
if(request == null) { | ||
Response res = new Response(); | ||
res.setError(new Response.Error(-32600, "The JSON sent is not a valid Request object.")); | ||
return res; | ||
} | ||
|
||
if(!mMethodHandlerMap.containsKey(request.getMethod())) { | ||
Response res = new Response(); | ||
res.setError(new Response.Error(-32601, "The method does not exist / is not available.")); | ||
return res; | ||
} | ||
|
||
MethodHandler methodHandler = mMethodHandlerMap.get(request.getMethod()); | ||
Object result = methodHandler.execute(request.getParams()); | ||
Response response = new Response(); | ||
response.setId(request.getId()); | ||
response.setResult(result); | ||
return response; | ||
} | ||
|
||
|
||
/** | ||
* Utility class to get method's parameter type. | ||
* | ||
* @param methodName | ||
* @return | ||
*/ | ||
public Class findAvailableParamClass(String methodName) { | ||
if(!mMethodHandlerMap.containsKey(methodName)) { | ||
return null; | ||
} | ||
|
||
MethodHandler methodHandler = mMethodHandlerMap.get(methodName); | ||
return methodHandler.getParamClass(); | ||
} | ||
|
||
|
||
|
||
} |
38 changes: 38 additions & 0 deletions
38
WebViewBridge/src/kr/co/b612lodger/jsonRpc/core/MethodHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package kr.co.b612lodger.jsonRpc.core; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
|
||
public abstract class MethodHandler<T, U> { | ||
|
||
public abstract U execute(T request); | ||
|
||
@SuppressWarnings("unchecked") | ||
public Class<T> getParamClass() { | ||
Class<T> typeClass = null; | ||
try { | ||
typeClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()) | ||
.getActualTypeArguments()[0]; | ||
} catch(Exception e) {} | ||
|
||
if(typeClass.getName().equals(Object.class.getName())) { | ||
typeClass = null; | ||
} | ||
|
||
return typeClass; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public Class<T> getResultClass() { | ||
Class<T> typeClass = null; | ||
try { | ||
typeClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()) | ||
.getActualTypeArguments()[1]; | ||
} catch(Exception e) {} | ||
|
||
if(typeClass.getName().equals(Object.class.getName())) { | ||
typeClass = null; | ||
} | ||
|
||
return typeClass; | ||
} | ||
} |
Oops, something went wrong.