From 2d7c9c4d8375227f592182c2ae6e56efb87cf477 Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Mon, 27 Apr 2015 10:28:36 -0400 Subject: [PATCH] Moved some WebView config code from EditorFragment to EditorWebViewAbstract --- .../android/editor/EditorFragment.java | 33 +-------------- .../android/editor/EditorWebViewAbstract.java | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java index 1669819cf623..09e3e9dc2d11 100755 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java @@ -4,17 +4,11 @@ import android.app.Activity; import android.os.Build; import android.os.Bundle; -import android.support.annotation.NonNull; import android.text.Spanned; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.webkit.ConsoleMessage; -import android.webkit.JsResult; -import android.webkit.WebChromeClient; -import android.webkit.WebSettings; import android.webkit.WebView; -import android.webkit.WebViewClient; import android.widget.ToggleButton; import com.android.volley.toolbox.ImageLoader; @@ -79,7 +73,7 @@ public void onCreate(Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_editor, container, false); mWebView = (EditorWebViewAbstract) view.findViewById(R.id.webview); - initWebView(); + initJsEditor(); ToggleButton mediaButton = (ToggleButton) view.findViewById(R.id.format_bar_button_media); mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_MEDIA, mediaButton); @@ -117,30 +111,7 @@ public void onDetach() { super.onDetach(); } - @SuppressLint("SetJavaScriptEnabled") - private void initWebView() { - WebSettings webSettings = mWebView.getSettings(); - webSettings.setJavaScriptEnabled(true); - webSettings.setDefaultTextEncodingName("utf-8"); - mWebView.setWebViewClient(new WebViewClient() { - public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { - AppLog.e(T.EDITOR, description); - } - }); - mWebView.setWebChromeClient(new WebChromeClient() { - @Override - public boolean onConsoleMessage(@NonNull ConsoleMessage cm) { - AppLog.d(T.EDITOR, cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId()); - return true; - } - - @Override - public boolean onJsAlert(WebView view, String url, String message, JsResult result) { - AppLog.d(T.EDITOR, message); - return true; - } - }); - + private void initJsEditor() { String htmlEditor = Utils.getHtmlFromFile(mActivity, "android-editor.html"); mWebView.addJavascriptInterface(new JsCallbackReceiver(this), JS_CALLBACK_HANDLER); diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorWebViewAbstract.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorWebViewAbstract.java index 24a3f6604ab9..26973d755f66 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorWebViewAbstract.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorWebViewAbstract.java @@ -1,14 +1,54 @@ package org.wordpress.android.editor; +import android.annotation.SuppressLint; import android.content.Context; +import android.support.annotation.NonNull; import android.util.AttributeSet; +import android.webkit.ConsoleMessage; +import android.webkit.JsResult; +import android.webkit.WebChromeClient; +import android.webkit.WebSettings; import android.webkit.WebView; +import android.webkit.WebViewClient; +import org.wordpress.android.util.AppLog; + +/** + * A text editor WebView with support for JavaScript execution. + */ public abstract class EditorWebViewAbstract extends WebView { public abstract void execJavaScriptFromString(String javaScript); public EditorWebViewAbstract(Context context, AttributeSet attrs) { super(context, attrs); + configureWebView(); + } + + @SuppressLint("SetJavaScriptEnabled") + private void configureWebView() { + WebSettings webSettings = this.getSettings(); + webSettings.setJavaScriptEnabled(true); + webSettings.setDefaultTextEncodingName("utf-8"); + + this.setWebViewClient(new WebViewClient() { + public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { + AppLog.e(AppLog.T.EDITOR, description); + } + }); + + this.setWebChromeClient(new WebChromeClient() { + @Override + public boolean onConsoleMessage(@NonNull ConsoleMessage cm) { + AppLog.d(AppLog.T.EDITOR, cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId()); + return true; + } + + @Override + public boolean onJsAlert(WebView view, String url, String message, JsResult result) { + AppLog.d(AppLog.T.EDITOR, message); + return true; + } + }); } @Override