From ae43667f2f04509fa0a76541fec0ddbba92695da Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 1 Sep 2014 14:32:13 -0400 Subject: [PATCH] JS can call Android and get a value back! --- app/build.gradle | 1 + app/src/main/assets/dist/book-bundle.js | 2 ++ app/src/main/assets/src/book.coffee | 2 ++ .../minimalbible/activity/viewer/BookFragment.java | 10 ++++++++++ 4 files changed, 15 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index cbea937..0ffaddb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -67,6 +67,7 @@ dependencies { compile 'com.netflix.rxjava:rxjava-android:+' compile 'com.android.support:appcompat-v7:20.+' compile 'org.apache.commons:commons-lang3:+' + compile 'com.google.code.gson:gson:+' androidTestCompile 'com.jayway.awaitility:awaitility:+' androidTestCompile 'org.mockito:mockito-core:+' diff --git a/app/src/main/assets/dist/book-bundle.js b/app/src/main/assets/dist/book-bundle.js index 984d8ae..50a2b95 100755 --- a/app/src/main/assets/dist/book-bundle.js +++ b/app/src/main/assets/dist/book-bundle.js @@ -31,6 +31,8 @@ window.appendVerse = function(text) { return scope.$apply(); }; +console.log(Android.testReturn("Good morning.")); + /* Future reference: Get the controller scope like so: diff --git a/app/src/main/assets/src/book.coffee b/app/src/main/assets/src/book.coffee index 73c29a1..c7fd49e 100644 --- a/app/src/main/assets/src/book.coffee +++ b/app/src/main/assets/src/book.coffee @@ -23,6 +23,8 @@ window.appendVerse = (text) -> # Since we're calling outside of angular, we need to manually apply scope.$apply() +console.log Android.testReturn "Good morning." + ### Future reference: Get the controller scope like so: angular.element($("")).scope(). diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java index b29db2d..d5f257d 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java @@ -7,6 +7,7 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.webkit.JavascriptInterface; import android.webkit.WebView; import android.webkit.WebViewClient; @@ -125,6 +126,15 @@ public class BookFragment extends BaseFragment { } }); + // We can receive and return only primitives and Strings. Still means we can use JSON :) + mainContent.addJavascriptInterface(new Object() { + @JavascriptInterface + @SuppressWarnings("unused") + public String testReturn(String echo) { + return echo; + } + }, "Android"); + // TODO: Remove remote debugging when ready - or should this be removed? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { WebView.setWebContentsDebuggingEnabled(true);