|
| 1 | +package org.wordpress.editor; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.content.res.AssetManager; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.support.v7.app.ActionBarActivity; |
| 7 | +import android.util.Log; |
| 8 | +import android.webkit.ConsoleMessage; |
| 9 | +import android.webkit.JsResult; |
| 10 | +import android.webkit.WebChromeClient; |
| 11 | +import android.webkit.WebSettings; |
| 12 | +import android.webkit.WebView; |
| 13 | +import android.webkit.WebViewClient; |
| 14 | + |
| 15 | +import java.io.BufferedReader; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.io.InputStreamReader; |
| 19 | + |
| 20 | +public class EditorActivity extends ActionBarActivity { |
| 21 | + WebView mWebView; |
| 22 | + |
| 23 | + @SuppressLint("SetJavaScriptEnabled") |
| 24 | + @Override |
| 25 | + protected void onCreate(Bundle savedInstanceState) { |
| 26 | + super.onCreate(savedInstanceState); |
| 27 | + setContentView(R.layout.activity_editor); |
| 28 | + mWebView = (WebView) findViewById(R.id.webview); |
| 29 | + WebSettings webSettings = mWebView.getSettings(); |
| 30 | + webSettings.setJavaScriptEnabled(true); |
| 31 | + webSettings.setDefaultTextEncodingName("utf-8"); |
| 32 | + mWebView.getSettings().setJavaScriptEnabled(true); |
| 33 | + mWebView.setWebViewClient(new WebViewClient() { |
| 34 | + public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { |
| 35 | + Log.e("WordPress-Editor", description); |
| 36 | + } |
| 37 | + }); |
| 38 | + mWebView.setWebChromeClient(new WebChromeClient() { |
| 39 | + public boolean onConsoleMessage(ConsoleMessage cm) { |
| 40 | + Log.e("WordPress-Editor", cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId()); |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean onJsAlert(WebView view, String url, String message, JsResult result) { |
| 46 | + Log.e("WordPress-Editor", message); |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onConsoleMessage(String message, int lineNumber, String sourceId) { |
| 52 | + Log.e("WordPress-Editor", message + " -- from line " + lineNumber + " of " + sourceId); |
| 53 | + } |
| 54 | + }); |
| 55 | + String htmlEditor = getHtmlEditor(); |
| 56 | + mWebView.loadDataWithBaseURL("file:///android_asset/", htmlEditor, "text/html", "utf-8", ""); |
| 57 | + } |
| 58 | + |
| 59 | + private String getStringFromAsset(String filename) throws IOException { |
| 60 | + AssetManager assetManager = getAssets(); |
| 61 | + InputStream in = assetManager.open(filename); |
| 62 | + InputStreamReader is = new InputStreamReader(in); |
| 63 | + StringBuilder sb = new StringBuilder(); |
| 64 | + BufferedReader br = new BufferedReader(is); |
| 65 | + String read = br.readLine(); |
| 66 | + while (read != null) { |
| 67 | + sb.append(read); |
| 68 | + sb.append('\n'); |
| 69 | + read = br.readLine(); |
| 70 | + } |
| 71 | + return sb.toString(); |
| 72 | + } |
| 73 | + |
| 74 | + private String getHtmlEditor() { |
| 75 | + try { |
| 76 | + return getStringFromAsset("android-editor.html"); |
| 77 | + } catch (IOException e) { |
| 78 | + Log.e("WordPress-Editor", e.getMessage()); |
| 79 | + return null; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments