Skip to content

Commit

Permalink
initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
b612lodger committed Dec 22, 2013
0 parents commit 346e91a
Show file tree
Hide file tree
Showing 26 changed files with 915 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
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/*
9 changes: 9 additions & 0 deletions WebViewBridge/.classpath
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>
33 changes: 33 additions & 0 deletions WebViewBridge/.project
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>
14 changes: 14 additions & 0 deletions WebViewBridge/AndroidManifest.xml
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 added WebViewBridge/libs/android-support-v4.jar
Binary file not shown.
Binary file added WebViewBridge/libs/gson-2.2.4.jar
Binary file not shown.
20 changes: 20 additions & 0 deletions WebViewBridge/proguard-project.txt
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 *;
#}
15 changes: 15 additions & 0 deletions WebViewBridge/project.properties
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 WebViewBridge/src/kr/co/b612lodger/jsonRpc/AsyncServerStub.java
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 WebViewBridge/src/kr/co/b612lodger/jsonRpc/ServerStub.java
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 WebViewBridge/src/kr/co/b612lodger/jsonRpc/core/MethodHandler.java
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;
}
}
Loading

0 comments on commit 346e91a

Please sign in to comment.