From f5800388d30062647afc97fcc49346a3ed2fc2b3 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Wed, 18 Jun 2014 22:51:31 -0400 Subject: [PATCH] Some basic WebView shenanigans --- MinimalBible/src/main/assets/book.html | 11 ++++++ .../activities/viewer/BookFragment.java | 38 ++++++++++++++++++- .../main/res/layout/fragment_viewer_main.xml | 2 +- .../src/main/res/values/html_strings.xml | 4 ++ 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 MinimalBible/src/main/assets/book.html create mode 100644 MinimalBible/src/main/res/values/html_strings.xml diff --git a/MinimalBible/src/main/assets/book.html b/MinimalBible/src/main/assets/book.html new file mode 100644 index 0000000..75f4951 --- /dev/null +++ b/MinimalBible/src/main/assets/book.html @@ -0,0 +1,11 @@ + + +
+ + + + \ No newline at end of file diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BookFragment.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BookFragment.java index c108ed3..19f32a4 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BookFragment.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BookFragment.java @@ -7,12 +7,15 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView; +import android.webkit.WebViewClient; import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activities.BaseFragment; import org.crosswire.jsword.book.Book; +import java.util.List; + import javax.inject.Inject; import butterknife.ButterKnife; @@ -61,6 +64,7 @@ public class BookFragment extends BaseFragment { View rootView = inflater.inflate(R.layout.fragment_viewer_main, container, false); ButterKnife.inject(this, rootView); + mainContent.getSettings().setJavaScriptEnabled(true); // TODO: Load initial text from SharedPreferences @@ -99,6 +103,38 @@ public class BookFragment extends BaseFragment { private void displayBook(Book b) { Log.d("BookFragment", b.getName()); ((BibleViewer)getActivity()).setActionBarTitle(b.getInitials()); - mainContent.loadData("

" + b.getName() + "

", "text/html", "utf-8"); + mainContent.loadUrl(getString(R.string.content_page)); + mainContent.setWebViewClient(new WebViewClient(){ + @Override + public void onPageFinished(WebView view, String url) { + super.onPageFinished(view, url); + invokeJavascript("set_content", "Hello, world."); + } + }); + + } + + private void invokeJavascript(String function, Object arg) { + mainContent.loadUrl("javascript:" + function + "('" + arg.toString() + "')"); + } + + private void invokeJavascript(String function, List args) { + mainContent.loadUrl("javascript:" + function + "(" + joinString(",", args.toArray()) + ")"); + } + + // Convenience from http://stackoverflow.com/a/17795110/1454178 + public static String joinString(String join, Object... strings) { + if (strings == null || strings.length == 0) { + return ""; + } else if (strings.length == 1) { + return strings[0].toString(); + } else { + StringBuilder sb = new StringBuilder(); + sb.append(strings[0]); + for (int i = 1; i < strings.length; i++) { + sb.append(join).append(strings[i].toString()); + } + return sb.toString(); + } } } diff --git a/MinimalBible/src/main/res/layout/fragment_viewer_main.xml b/MinimalBible/src/main/res/layout/fragment_viewer_main.xml index befcaf6..9147032 100644 --- a/MinimalBible/src/main/res/layout/fragment_viewer_main.xml +++ b/MinimalBible/src/main/res/layout/fragment_viewer_main.xml @@ -7,6 +7,6 @@ + android:layout_height="fill_parent" /> diff --git a/MinimalBible/src/main/res/values/html_strings.xml b/MinimalBible/src/main/res/values/html_strings.xml new file mode 100644 index 0000000..a228ccf --- /dev/null +++ b/MinimalBible/src/main/res/values/html_strings.xml @@ -0,0 +1,4 @@ + + + file:///android_asset/book.html + \ No newline at end of file