Week 2.5 – androidJsBridge

Its been almost two and a half weeks since the GSoC coding period started and it has been nothing less than interesting. Finally, after quite a few hours of coding I’ve been able to successfully integrate the Posting-Applications, PlainPost and Zerobin, with the Privly Android Application.

The Posting applications being in Javascript created a really interesting problem of integration with the Native Android Code. Something I had never really worked with before.  The solution was however pretty simple. A Js interface is added to the WebView which enables the Js to talk to Java and then depending on the user selection, the Js code for either of the Posting Applications is loaded in the WebView.

WebView w = (WebView)findViewById(R.id.webview_1);
w.getSettings().setJavaScriptEnabled(true);
w.addJavascriptInterface(new JsObject(this), "androidJsBridge");

The JsObject Class defines the functions that are accessible to Js.

Like,

public class JsObject{

    Context c;

    JsObject(Context callingContext){
        c = callingContext;
    }

    @JavascriptInterface
    public String getDeviceVersion(){
        String deviceVersion= Build.VERSION.RELEASE;
        Log.d("androidJSBridge Version Request",deviceVersion);
        return deviceVersion;
    }
}

And the Js Code of the Posting-Applications can access these functions by using the androidJsBridge.
For example –

var android_version = androidJsBridge.getDeviceVersion();

I also pushed some Android specific Js code to the Posting-Applications branch and now, the Android Application is now able to generate PlainPost and ZeroBin Links and allows the user to share the new Privly Urls.

Latest Code for the Android App — https://github.com/vshivam/privly-android

Leave a comment