Skip to content

Commit

Permalink
Moved some WebView config code from EditorFragment to EditorWebViewAb…
Browse files Browse the repository at this point in the history
…stract
  • Loading branch information
aforcier committed Apr 27, 2015
1 parent c5882ba commit 2d7c9c4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 2d7c9c4

Please sign in to comment.