From 5da8d7949ed1b58aac13e3f3660c8baf3fd3b4b0 Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Thu, 9 Apr 2015 11:13:12 -0400 Subject: [PATCH 01/13] Added example format bar with bold button --- .../android/editor/EditorFragment.java | 11 ++++++-- .../src/main/res/layout/fragment_editor.xml | 28 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 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 6334ec458a0a..dd0a2832b8f0 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java @@ -15,6 +15,7 @@ import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; +import android.widget.ToggleButton; import com.android.volley.toolbox.ImageLoader; @@ -28,7 +29,7 @@ import java.io.InputStream; import java.io.InputStreamReader; -public class EditorFragment extends EditorFragmentAbstract { +public class EditorFragment extends EditorFragmentAbstract implements View.OnClickListener { private static final String ARG_PARAM_TITLE = "param_title"; private static final String ARG_PARAM_CONTENT = "param_content"; @@ -38,6 +39,7 @@ public class EditorFragment extends EditorFragmentAbstract { private String mParamContent; private WebView mWebView; + private ToggleButton mBoldButton; public static EditorFragment newInstance(String title, String content) { EditorFragment fragment = new EditorFragment(); Bundle args = new Bundle(); @@ -60,11 +62,14 @@ public void onCreate(Bundle savedInstanceState) { } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_editor, container, false); mWebView = (WebView) view.findViewById(R.id.webview); initWebView(); + + mBoldButton = (ToggleButton) view.findViewById(R.id.bold); + mBoldButton.setOnClickListener(this); + return view; } diff --git a/WordPressEditor/src/main/res/layout/fragment_editor.xml b/WordPressEditor/src/main/res/layout/fragment_editor.xml index 02eacce679e0..a2eabf64ba33 100644 --- a/WordPressEditor/src/main/res/layout/fragment_editor.xml +++ b/WordPressEditor/src/main/res/layout/fragment_editor.xml @@ -9,4 +9,32 @@ android:layout_height="match_parent" android:id="@+id/webview"/> + + + + + + + + + + + From f1f933b0905c274c501430e3c8a83b534b30d15f Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Thu, 9 Apr 2015 11:33:19 -0400 Subject: [PATCH 02/13] Moved JsCallbackHandler to its own file - Added a JsCallbackListener interface for callbacks from the handler to the editor fragment - Updated ZSSEditor.callback to send the full callback path --- .../android/editor/EditorFragment.java | 45 ++++++++----------- .../android/editor/JsCallbackHandler.java | 45 +++++++++++++++++++ .../android/editor/JsCallbackListener.java | 5 +++ .../editor-common/assets/ZSSRichTextEditor.js | 4 +- 4 files changed, 72 insertions(+), 27 deletions(-) create mode 100644 WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java create mode 100644 WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackListener.java 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 dd0a2832b8f0..f78c99f651d0 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java @@ -9,7 +9,6 @@ import android.view.View; import android.view.ViewGroup; import android.webkit.ConsoleMessage; -import android.webkit.JavascriptInterface; import android.webkit.JsResult; import android.webkit.WebChromeClient; import android.webkit.WebSettings; @@ -29,7 +28,7 @@ import java.io.InputStream; import java.io.InputStreamReader; -public class EditorFragment extends EditorFragmentAbstract implements View.OnClickListener { +public class EditorFragment extends EditorFragmentAbstract implements View.OnClickListener, JsCallbackListener { private static final String ARG_PARAM_TITLE = "param_title"; private static final String ARG_PARAM_CONTENT = "param_content"; @@ -89,6 +88,7 @@ public void onReceivedError(WebView view, int errorCode, String description, Str } }); mWebView.setWebChromeClient(new WebChromeClient() { + @Override public boolean onConsoleMessage(ConsoleMessage cm) { AppLog.d(T.EDITOR, cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId()); return true; @@ -99,16 +99,11 @@ public boolean onJsAlert(WebView view, String url, String message, JsResult resu AppLog.d(T.EDITOR, message); return true; } - - @Override - public void onConsoleMessage(String message, int lineNumber, String sourceId) { - AppLog.d(T.EDITOR, message + " -- from line " + lineNumber + " of " + sourceId); - } }); String htmlEditor = getHtmlFromFile("android-editor.html"); - mWebView.addJavascriptInterface(new JsCallbackHandler(), JS_CALLBACK_HANDLER); + mWebView.addJavascriptInterface(new JsCallbackHandler(this), JS_CALLBACK_HANDLER); mWebView.loadDataWithBaseURL("file:///android_asset/", htmlEditor, "text/html", "utf-8", ""); @@ -187,24 +182,22 @@ public Spanned getSpannedContent() { return null; } - class JsCallbackHandler { - @JavascriptInterface - public void executeCallback(final String callbackId) { - if (callbackId.equals("callback-dom-loaded")) { - // Run on UI thread - mWebView.post(new Runnable() { - public void run() { - String title = "I'm editing a post!"; - String contentHtml = getHtmlFromFile("example-content.html"); - - // Load example content into editor - mWebView.loadUrl("javascript:ZSSEditor.getField('zss_field_title').setHTML('" + - Utils.escapeHtml(title) + "');"); - mWebView.loadUrl("javascript:ZSSEditor.getField('zss_field_content').setHTML('" + - Utils.escapeHtml(contentHtml) + "');"); - } - }); + private void execJavaScriptFromString(String javaScript) { + mWebView.loadUrl("javascript:" + javaScript); + } + + public void onDomLoaded() { + mWebView.post(new Runnable() { + public void run() { + String title = "I'm editing a post!"; + String contentHtml = getHtmlFromFile("example-content.html"); + + // Load example content into editor + execJavaScriptFromString("ZSSEditor.getField('zss_field_title').setHTML('" + + Utils.escapeHtml(title) + "');"); + execJavaScriptFromString("ZSSEditor.getField('zss_field_content').setHTML('" + + Utils.escapeHtml(contentHtml) + "');"); } - } + }); } } \ No newline at end of file diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java new file mode 100644 index 000000000000..4391c4c35a58 --- /dev/null +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java @@ -0,0 +1,45 @@ +package org.wordpress.android.editor; + +import android.webkit.JavascriptInterface; + +import org.wordpress.android.util.AppLog; + +public class JsCallbackHandler { + private static final String CALLBACK_LOG = "callback-log"; + + private static final String CALLBACK_DOM_LOADED = "callback-dom-loaded"; + + private static final String CALLBACK_SELECTION_STYLE = "callback-selection-style"; + private static final String CALLBACK_SELECTION_CHANGED = "callback-selection-changed"; + + private static final String CALLBACK_FOCUS_IN = "callback-focus-in"; + private static final String CALLBACK_FOCUS_OUT = "callback-focus-out"; + + private static final String CALLBACK_IMAGE_REPLACED = "callback-image-replaced"; + private static final String CALLBACK_IMAGE_TAP = "callback-image-tap"; + private static final String CALLBACK_INPUT = "callback-input"; + private static final String CALLBACK_LINK_TAP = "callback-link-tap"; + + private static final String CALLBACK_NEW_FIELD = "callback-new-field"; + + private final JsCallbackListener mJsCallbackListener; + + public JsCallbackHandler(EditorFragmentAbstract editorFragmentAbstract) { + mJsCallbackListener = (JsCallbackListener) editorFragmentAbstract; + } + + @JavascriptInterface + public void executeCallback(String callbackId, String params) { + switch (callbackId) { + case CALLBACK_DOM_LOADED: + mJsCallbackListener.onDomLoaded(); + break; + case CALLBACK_LOG: + // Strip 'msg=' from beginning of string + AppLog.d(AppLog.T.EDITOR, callbackId + ": " + params.substring(4)); + break; + default: + AppLog.d(AppLog.T.EDITOR, "unhandled callback: " + callbackId + ":" + params); + } + } +} diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackListener.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackListener.java new file mode 100644 index 000000000000..b5c57e6163fc --- /dev/null +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackListener.java @@ -0,0 +1,5 @@ +package org.wordpress.android.editor; + +public interface JsCallbackListener { + void onDomLoaded(); +} diff --git a/libs/editor-common/assets/ZSSRichTextEditor.js b/libs/editor-common/assets/ZSSRichTextEditor.js index dd4665c6d8c4..3cabd416ab5d 100755 --- a/libs/editor-common/assets/ZSSRichTextEditor.js +++ b/libs/editor-common/assets/ZSSRichTextEditor.js @@ -199,7 +199,7 @@ ZSSEditor.callback = function(callbackScheme, callbackPath) { if (isUsingiOS) { ZSSEditor.callbackThroughIFrame(url); } else if (isUsingAndroid) { - nativeCallbackHandler.executeCallback(callbackScheme); + nativeCallbackHandler.executeCallback(callbackScheme, callbackPath); } else { console.log(url); } @@ -1877,6 +1877,8 @@ ZSSField.prototype.callback = function(callbackScheme, callbackPath) { if (isUsingiOS) { ZSSEditor.callbackThroughIFrame(url); + } else if (isUsingAndroid) { + nativeCallbackHandler.executeCallback(callbackScheme, callbackPath); } else { console.log(url); } From e9c64011e2aa9345d89e3cb1fecf6cbb026bdffa Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Thu, 9 Apr 2015 11:36:27 -0400 Subject: [PATCH 03/13] Updated onDomLoaded() to enable multiline support for the content field --- .../main/java/org/wordpress/android/editor/EditorFragment.java | 2 ++ 1 file changed, 2 insertions(+) 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 f78c99f651d0..d0050fef9c28 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java @@ -192,6 +192,8 @@ public void run() { String title = "I'm editing a post!"; String contentHtml = getHtmlFromFile("example-content.html"); + execJavaScriptFromString("ZSSEditor.getField('zss_field_content').setMultiline('true');"); + // Load example content into editor execJavaScriptFromString("ZSSEditor.getField('zss_field_title').setHTML('" + Utils.escapeHtml(title) + "');"); From b7be24246e1a91156d717050a71314c96608961b Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Thu, 9 Apr 2015 12:02:23 -0400 Subject: [PATCH 04/13] Added handling for the "callback-selection-style" JS callback - Added support for toggling styles in the editor using the format bar, and handling the callback to update the highlighted status of format bar buttons --- .../android/editor/EditorFragment.java | 34 ++++++++++-- .../android/editor/JsCallbackHandler.java | 13 +++++ .../android/editor/JsCallbackListener.java | 3 ++ .../org/wordpress/android/editor/Utils.java | 52 +++++++++++++++++++ 4 files changed, 99 insertions(+), 3 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 d0050fef9c28..d1b58797e9e2 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java @@ -27,6 +27,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; public class EditorFragment extends EditorFragmentAbstract implements View.OnClickListener, JsCallbackListener { private static final String ARG_PARAM_TITLE = "param_title"; @@ -34,11 +36,14 @@ public class EditorFragment extends EditorFragmentAbstract implements View.OnCli private static final String JS_CALLBACK_HANDLER = "nativeCallbackHandler"; + private static final String TAG_FORMAT_BAR_BUTTON_BOLD = "bold"; + private String mParamTitle; private String mParamContent; private WebView mWebView; - private ToggleButton mBoldButton; + private final Map mTagToggleButtonMap = new HashMap<>(); + public static EditorFragment newInstance(String title, String content) { EditorFragment fragment = new EditorFragment(); Bundle args = new Bundle(); @@ -66,8 +71,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa mWebView = (WebView) view.findViewById(R.id.webview); initWebView(); - mBoldButton = (ToggleButton) view.findViewById(R.id.bold); - mBoldButton.setOnClickListener(this); + ToggleButton boldButton = (ToggleButton) view.findViewById(R.id.bold); + boldButton.setOnClickListener(this); + mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_BOLD, boldButton); return view; } @@ -110,6 +116,14 @@ public boolean onJsAlert(WebView view, String url, String message, JsResult resu enableWebDebugging(true); } + @Override + public void onClick(View v) { + int id = v.getId(); + if (id == R.id.bold) { + execJavaScriptFromString("ZSSEditor.setBold();"); + } + } + private String getStringFromAsset(String filename) throws IOException { if (!isAdded()) { return null; @@ -202,4 +216,18 @@ public void run() { } }); } + + public void onSelectionStyleChanged(final Map changeMap) { + mWebView.post(new Runnable() { + public void run() { + for (Map.Entry entry : changeMap.entrySet()) { + // Handle toggling format bar style buttons + ToggleButton button = mTagToggleButtonMap.get(entry.getKey()); + if (button != null) { + button.setChecked(entry.getValue()); + } + } + } + }); + } } \ No newline at end of file diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java index 4391c4c35a58..c74b00e785db 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java @@ -4,7 +4,12 @@ import org.wordpress.android.util.AppLog; +import java.util.HashSet; +import java.util.Set; + public class JsCallbackHandler { + private static final String JS_CALLBACK_DELIMITER = "~"; + private static final String CALLBACK_LOG = "callback-log"; private static final String CALLBACK_DOM_LOADED = "callback-dom-loaded"; @@ -24,6 +29,8 @@ public class JsCallbackHandler { private final JsCallbackListener mJsCallbackListener; + private Set mPreviousStyleSet = new HashSet<>(); + public JsCallbackHandler(EditorFragmentAbstract editorFragmentAbstract) { mJsCallbackListener = (JsCallbackListener) editorFragmentAbstract; } @@ -34,6 +41,12 @@ public void executeCallback(String callbackId, String params) { case CALLBACK_DOM_LOADED: mJsCallbackListener.onDomLoaded(); break; + case CALLBACK_SELECTION_STYLE: + // Compare the new styles to the previous ones, and notify the JsCallbackListener of the changeset + Set newStyleSet = Utils.splitDelimitedString(params, JS_CALLBACK_DELIMITER); + mJsCallbackListener.onSelectionStyleChanged(Utils.getChangeMapFromSets(mPreviousStyleSet, newStyleSet)); + mPreviousStyleSet = newStyleSet; + break; case CALLBACK_LOG: // Strip 'msg=' from beginning of string AppLog.d(AppLog.T.EDITOR, callbackId + ": " + params.substring(4)); diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackListener.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackListener.java index b5c57e6163fc..91324d0188ca 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackListener.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackListener.java @@ -1,5 +1,8 @@ package org.wordpress.android.editor; +import java.util.Map; + public interface JsCallbackListener { void onDomLoaded(); + void onSelectionStyleChanged(Map changeSet); } diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java index cc581bf711c2..1e45d5f412ea 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java @@ -1,5 +1,11 @@ package org.wordpress.android.editor; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.StringTokenizer; + public class Utils { public static String escapeHtml(String html) { html = html.replace("\\", "\\\\"); @@ -9,4 +15,50 @@ public static String escapeHtml(String html) { html = html.replace("\n", "\\n"); return html; } + + /** + * Splits a delimited string into a set of strings. + * @param string the delimited string to split + * @param delimiter the string delimiter + */ + public static Set splitDelimitedString(String string, String delimiter) { + Set splitString = new HashSet<>(); + + StringTokenizer stringTokenizer = new StringTokenizer(string, delimiter); + while (stringTokenizer.hasMoreTokens()) { + splitString.add(stringTokenizer.nextToken()); + } + + return splitString; + } + + /** + * Compares two Sets and returns a Map of elements not contained in both + * Sets. Elements contained in oldSet but not in newSet will be marked + * false in the returned map; the converse will be marked true. + * @param oldSet the older of the two Sets + * @param newSet the newer of the two Sets + * @param type of element stored in the Sets + * @return a Map containing the difference between oldSet and newSet, and whether the + * element was added (true) or removed (false) in newSet + */ + public static Map getChangeMapFromSets(Set oldSet, Set newSet) { + Map changeMap = new HashMap<>(); + + Set additions = new HashSet<>(newSet); + additions.removeAll(oldSet); + + Set removals = new HashSet<>(oldSet); + removals.removeAll(newSet); + + for (E s : additions) { + changeMap.put(s, true); + } + + for (E s : removals) { + changeMap.put(s, false); + } + + return changeMap; + } } From 7f336ea49cba620ea1509775b440a995b029fa1c Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Thu, 9 Apr 2015 14:06:37 -0400 Subject: [PATCH 05/13] Added a WebView sublcass, EditorWebView, and switched the editor to use that - Overrides onCheckIsTextEditor() to declare itself an editor - Moved execJavaScriptFromString over to be a method of EditorWebView --- .../android/editor/EditorFragment.java | 16 ++++------ .../android/editor/EditorWebView.java | 30 +++++++++++++++++++ .../src/main/res/layout/fragment_editor.xml | 2 +- 3 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 WordPressEditor/src/main/java/org/wordpress/android/editor/EditorWebView.java 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 d1b58797e9e2..670459c5466e 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java @@ -40,7 +40,7 @@ public class EditorFragment extends EditorFragmentAbstract implements View.OnCli private String mParamTitle; private String mParamContent; - private WebView mWebView; + private EditorWebView mWebView; private final Map mTagToggleButtonMap = new HashMap<>(); @@ -68,7 +68,7 @@ public void onCreate(Bundle savedInstanceState) { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_editor, container, false); - mWebView = (WebView) view.findViewById(R.id.webview); + mWebView = (EditorWebView) view.findViewById(R.id.webview); initWebView(); ToggleButton boldButton = (ToggleButton) view.findViewById(R.id.bold); @@ -120,7 +120,7 @@ public boolean onJsAlert(WebView view, String url, String message, JsResult resu public void onClick(View v) { int id = v.getId(); if (id == R.id.bold) { - execJavaScriptFromString("ZSSEditor.setBold();"); + mWebView.execJavaScriptFromString("ZSSEditor.setBold();"); } } @@ -196,22 +196,18 @@ public Spanned getSpannedContent() { return null; } - private void execJavaScriptFromString(String javaScript) { - mWebView.loadUrl("javascript:" + javaScript); - } - public void onDomLoaded() { mWebView.post(new Runnable() { public void run() { String title = "I'm editing a post!"; String contentHtml = getHtmlFromFile("example-content.html"); - execJavaScriptFromString("ZSSEditor.getField('zss_field_content').setMultiline('true');"); + mWebView.execJavaScriptFromString("ZSSEditor.getField('zss_field_content').setMultiline('true');"); // Load example content into editor - execJavaScriptFromString("ZSSEditor.getField('zss_field_title').setHTML('" + + mWebView.execJavaScriptFromString("ZSSEditor.getField('zss_field_title').setHTML('" + Utils.escapeHtml(title) + "');"); - execJavaScriptFromString("ZSSEditor.getField('zss_field_content').setHTML('" + + mWebView.execJavaScriptFromString("ZSSEditor.getField('zss_field_content').setHTML('" + Utils.escapeHtml(contentHtml) + "');"); } }); diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorWebView.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorWebView.java new file mode 100644 index 000000000000..67dc35c2ddc4 --- /dev/null +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorWebView.java @@ -0,0 +1,30 @@ +package org.wordpress.android.editor; + +import android.content.Context; +import android.util.AttributeSet; +import android.webkit.WebView; + +public class EditorWebView extends WebView { + + public EditorWebView(Context context) { + super(context); + } + + public EditorWebView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public EditorWebView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + @Override + public boolean onCheckIsTextEditor() { + return true; + } + + public void execJavaScriptFromString(String javaScript) { + this.loadUrl("javascript:" + javaScript); + } + +} \ No newline at end of file diff --git a/WordPressEditor/src/main/res/layout/fragment_editor.xml b/WordPressEditor/src/main/res/layout/fragment_editor.xml index a2eabf64ba33..a573dfe74b40 100644 --- a/WordPressEditor/src/main/res/layout/fragment_editor.xml +++ b/WordPressEditor/src/main/res/layout/fragment_editor.xml @@ -4,7 +4,7 @@ android:layout_height="match_parent" tools:context="org.wordpress.android.editor.EditorFragment"> - From abcf3e336d9664ad0f4a0724e4e12600f9c5ce93 Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Thu, 9 Apr 2015 14:09:38 -0400 Subject: [PATCH 06/13] Moved some utility methods from EditorFragment to Utils --- .../android/editor/EditorFragment.java | 39 ++----------------- .../org/wordpress/android/editor/Utils.java | 34 ++++++++++++++++ 2 files changed, 38 insertions(+), 35 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 670459c5466e..6012d4aece61 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java @@ -1,9 +1,9 @@ package org.wordpress.android.editor; import android.annotation.SuppressLint; -import android.content.res.AssetManager; 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; @@ -23,10 +23,6 @@ import org.wordpress.android.util.helpers.MediaFile; import org.wordpress.android.util.helpers.MediaGallery; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; @@ -95,7 +91,7 @@ public void onReceivedError(WebView view, int errorCode, String description, Str }); mWebView.setWebChromeClient(new WebChromeClient() { @Override - public boolean onConsoleMessage(ConsoleMessage cm) { + public boolean onConsoleMessage(@NonNull ConsoleMessage cm) { AppLog.d(T.EDITOR, cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId()); return true; } @@ -107,7 +103,7 @@ public boolean onJsAlert(WebView view, String url, String message, JsResult resu } }); - String htmlEditor = getHtmlFromFile("android-editor.html"); + String htmlEditor = Utils.getHtmlFromFile(getActivity(), "android-editor.html"); mWebView.addJavascriptInterface(new JsCallbackHandler(this), JS_CALLBACK_HANDLER); @@ -124,33 +120,6 @@ public void onClick(View v) { } } - private String getStringFromAsset(String filename) throws IOException { - if (!isAdded()) { - return null; - } - AssetManager assetManager = getActivity().getAssets(); - InputStream in = assetManager.open(filename); - InputStreamReader is = new InputStreamReader(in); - StringBuilder sb = new StringBuilder(); - BufferedReader br = new BufferedReader(is); - String read = br.readLine(); - while (read != null) { - sb.append(read); - sb.append('\n'); - read = br.readLine(); - } - return sb.toString(); - } - - private String getHtmlFromFile(String filename) { - try { - return getStringFromAsset(filename); - } catch (IOException e) { - AppLog.e(T.EDITOR, e.getMessage()); - return null; - } - } - @SuppressLint("NewApi") private void enableWebDebugging(boolean enable) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { @@ -200,7 +169,7 @@ public void onDomLoaded() { mWebView.post(new Runnable() { public void run() { String title = "I'm editing a post!"; - String contentHtml = getHtmlFromFile("example-content.html"); + String contentHtml = Utils.getHtmlFromFile(getActivity(), "example-content.html"); mWebView.execJavaScriptFromString("ZSSEditor.getField('zss_field_content').setMultiline('true');"); diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java index 1e45d5f412ea..c865c07c40ef 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java @@ -1,5 +1,14 @@ package org.wordpress.android.editor; +import android.app.Activity; +import android.content.res.AssetManager; + +import org.wordpress.android.util.AppLog; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -7,6 +16,31 @@ import java.util.StringTokenizer; public class Utils { + + public static String getHtmlFromFile(Activity activity, String filename) { + try { + AssetManager assetManager = activity.getAssets(); + InputStream in = assetManager.open(filename); + return getStringFromInputStream(in); + } catch (IOException e) { + AppLog.e(AppLog.T.EDITOR, e.getMessage()); + return null; + } + } + + public static String getStringFromInputStream(InputStream inputStream) throws IOException { + InputStreamReader is = new InputStreamReader(inputStream); + StringBuilder sb = new StringBuilder(); + BufferedReader br = new BufferedReader(is); + String read = br.readLine(); + while (read != null) { + sb.append(read); + sb.append('\n'); + read = br.readLine(); + } + return sb.toString(); + } + public static String escapeHtml(String html) { html = html.replace("\\", "\\\\"); html = html.replace("\"", "\\\""); From 83c3108080274761f7495def8d2e6871004f23ee Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Fri, 10 Apr 2015 07:03:06 -0400 Subject: [PATCH 07/13] Added notes and logging for some currently unused JS callbacks --- .../android/editor/JsCallbackHandler.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java index c74b00e785db..1a22ed4c634e 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java @@ -10,22 +10,21 @@ public class JsCallbackHandler { private static final String JS_CALLBACK_DELIMITER = "~"; - private static final String CALLBACK_LOG = "callback-log"; - private static final String CALLBACK_DOM_LOADED = "callback-dom-loaded"; + private static final String CALLBACK_NEW_FIELD = "callback-new-field"; - private static final String CALLBACK_SELECTION_STYLE = "callback-selection-style"; + private static final String CALLBACK_INPUT = "callback-input"; private static final String CALLBACK_SELECTION_CHANGED = "callback-selection-changed"; + private static final String CALLBACK_SELECTION_STYLE = "callback-selection-style"; private static final String CALLBACK_FOCUS_IN = "callback-focus-in"; private static final String CALLBACK_FOCUS_OUT = "callback-focus-out"; private static final String CALLBACK_IMAGE_REPLACED = "callback-image-replaced"; private static final String CALLBACK_IMAGE_TAP = "callback-image-tap"; - private static final String CALLBACK_INPUT = "callback-input"; private static final String CALLBACK_LINK_TAP = "callback-link-tap"; - private static final String CALLBACK_NEW_FIELD = "callback-new-field"; + private static final String CALLBACK_LOG = "callback-log"; private final JsCallbackListener mJsCallbackListener; @@ -47,12 +46,32 @@ public void executeCallback(String callbackId, String params) { mJsCallbackListener.onSelectionStyleChanged(Utils.getChangeMapFromSets(mPreviousStyleSet, newStyleSet)); mPreviousStyleSet = newStyleSet; break; + case CALLBACK_SELECTION_CHANGED: + // Called when changes are made to selection (includes moving the caret without selecting text) + // TODO: Possibly needed for handling WebView scrolling when caret moves (from iOS) + break; + case CALLBACK_INPUT: + // Called on key press + // TODO: Possibly needed for handling WebView scrolling when caret moves (from iOS) + break; + case CALLBACK_FOCUS_IN: + // TODO: Needed to handle displaying/graying the format bar when focus changes between the title and content + AppLog.d(AppLog.T.EDITOR, "Focus in callback received"); + break; + case CALLBACK_FOCUS_OUT: + // TODO: Needed to handle displaying/graying the format bar when focus changes between the title and content + AppLog.d(AppLog.T.EDITOR, "Focus out callback received"); + break; + case CALLBACK_NEW_FIELD: + // TODO: Used for logging/testing purposes on iOS + AppLog.d(AppLog.T.EDITOR, "New field created, " + params); + break; case CALLBACK_LOG: // Strip 'msg=' from beginning of string AppLog.d(AppLog.T.EDITOR, callbackId + ": " + params.substring(4)); break; default: - AppLog.d(AppLog.T.EDITOR, "unhandled callback: " + callbackId + ":" + params); + AppLog.d(AppLog.T.EDITOR, "Unhandled callback: " + callbackId + ":" + params); } } } From b7462216e3211e149d531364e3fb10e8ae378209 Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Mon, 13 Apr 2015 12:30:50 -0400 Subject: [PATCH 08/13] Renamed callback classes for clarity - JsCallbackHandler -> JsCallbackReceiver - JsCallbackListener -> OnJsEditorStateChangedListener --- .../wordpress/android/editor/EditorFragment.java | 5 +++-- ...CallbackHandler.java => JsCallbackReceiver.java} | 13 +++++++------ ...ner.java => OnJsEditorStateChangedListener.java} | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) rename WordPressEditor/src/main/java/org/wordpress/android/editor/{JsCallbackHandler.java => JsCallbackReceiver.java} (88%) rename WordPressEditor/src/main/java/org/wordpress/android/editor/{JsCallbackListener.java => OnJsEditorStateChangedListener.java} (75%) 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 6012d4aece61..87bc449bccaa 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java @@ -26,7 +26,8 @@ import java.util.HashMap; import java.util.Map; -public class EditorFragment extends EditorFragmentAbstract implements View.OnClickListener, JsCallbackListener { +public class EditorFragment extends EditorFragmentAbstract implements View.OnClickListener, + OnJsEditorStateChangedListener { private static final String ARG_PARAM_TITLE = "param_title"; private static final String ARG_PARAM_CONTENT = "param_content"; @@ -105,7 +106,7 @@ public boolean onJsAlert(WebView view, String url, String message, JsResult resu String htmlEditor = Utils.getHtmlFromFile(getActivity(), "android-editor.html"); - mWebView.addJavascriptInterface(new JsCallbackHandler(this), JS_CALLBACK_HANDLER); + mWebView.addJavascriptInterface(new JsCallbackReceiver(this), JS_CALLBACK_HANDLER); mWebView.loadDataWithBaseURL("file:///android_asset/", htmlEditor, "text/html", "utf-8", ""); diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackReceiver.java similarity index 88% rename from WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java rename to WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackReceiver.java index 1a22ed4c634e..5293f6285cc8 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackHandler.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackReceiver.java @@ -7,7 +7,7 @@ import java.util.HashSet; import java.util.Set; -public class JsCallbackHandler { +public class JsCallbackReceiver { private static final String JS_CALLBACK_DELIMITER = "~"; private static final String CALLBACK_DOM_LOADED = "callback-dom-loaded"; @@ -26,24 +26,25 @@ public class JsCallbackHandler { private static final String CALLBACK_LOG = "callback-log"; - private final JsCallbackListener mJsCallbackListener; + private final OnJsEditorStateChangedListener mListener; private Set mPreviousStyleSet = new HashSet<>(); - public JsCallbackHandler(EditorFragmentAbstract editorFragmentAbstract) { - mJsCallbackListener = (JsCallbackListener) editorFragmentAbstract; + public JsCallbackReceiver(EditorFragmentAbstract editorFragmentAbstract) { + mListener = (OnJsEditorStateChangedListener) editorFragmentAbstract; } @JavascriptInterface public void executeCallback(String callbackId, String params) { switch (callbackId) { case CALLBACK_DOM_LOADED: - mJsCallbackListener.onDomLoaded(); + mListener.onDomLoaded(); break; case CALLBACK_SELECTION_STYLE: // Compare the new styles to the previous ones, and notify the JsCallbackListener of the changeset Set newStyleSet = Utils.splitDelimitedString(params, JS_CALLBACK_DELIMITER); - mJsCallbackListener.onSelectionStyleChanged(Utils.getChangeMapFromSets(mPreviousStyleSet, newStyleSet)); + mListener.onSelectionStyleChanged(Utils.getChangeMapFromSets(mPreviousStyleSet, + newStyleSet)); mPreviousStyleSet = newStyleSet; break; case CALLBACK_SELECTION_CHANGED: diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackListener.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/OnJsEditorStateChangedListener.java similarity index 75% rename from WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackListener.java rename to WordPressEditor/src/main/java/org/wordpress/android/editor/OnJsEditorStateChangedListener.java index 91324d0188ca..cf31d2880f6d 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackListener.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/OnJsEditorStateChangedListener.java @@ -2,7 +2,7 @@ import java.util.Map; -public interface JsCallbackListener { +public interface OnJsEditorStateChangedListener { void onDomLoaded(); void onSelectionStyleChanged(Map changeSet); } From 862e5334a4bdccb7f5c2ea92fbab05df071435f9 Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Mon, 13 Apr 2015 12:36:18 -0400 Subject: [PATCH 09/13] Added some callback placeholders to JsCallbackReceiver with logging --- .../wordpress/android/editor/JsCallbackReceiver.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackReceiver.java b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackReceiver.java index 5293f6285cc8..b0a5eecd895f 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackReceiver.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackReceiver.java @@ -67,6 +67,18 @@ public void executeCallback(String callbackId, String params) { // TODO: Used for logging/testing purposes on iOS AppLog.d(AppLog.T.EDITOR, "New field created, " + params); break; + case CALLBACK_IMAGE_REPLACED: + // TODO: Notifies that image upload has finished and that the local url was replaced by the remote url in the ZSS editor + AppLog.d(AppLog.T.EDITOR, "Image replaced, " + params); + break; + case CALLBACK_IMAGE_TAP: + // TODO: Notifies that an image was tapped + AppLog.d(AppLog.T.EDITOR, "Image tapped, " + params); + break; + case CALLBACK_LINK_TAP: + // TODO: Notifies that a link was tapped + AppLog.d(AppLog.T.EDITOR, "Link tapped, " + params); + break; case CALLBACK_LOG: // Strip 'msg=' from beginning of string AppLog.d(AppLog.T.EDITOR, callbackId + ": " + params.substring(4)); From 53821d7c68e3b2c689b65c8461b61ea636724eee Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Tue, 14 Apr 2015 08:20:26 -0400 Subject: [PATCH 10/13] Added units tests for JsCallbackHandler --- .../android/editor/JsCallbackHandlerTest.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 WordPressEditor/src/androidTest/java/org/wordpress/android/editor/JsCallbackHandlerTest.java diff --git a/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/JsCallbackHandlerTest.java b/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/JsCallbackHandlerTest.java new file mode 100644 index 000000000000..e528bc7065fc --- /dev/null +++ b/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/JsCallbackHandlerTest.java @@ -0,0 +1,96 @@ +package org.wordpress.android.editor; + +import android.util.Log; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowLog; +import org.wordpress.android.util.AppLog; + +import java.util.List; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static org.mockito.Mockito.mock; +import static org.robolectric.shadows.ShadowLog.LogItem; + +@Config(emulateSdk = 18) +@RunWith(RobolectricTestRunner.class) +public class JsCallbackHandlerTest { + private final static String EDITOR_LOG_TAG = "WordPress-" + AppLog.T.EDITOR.toString(); + + private JsCallbackReceiver mJsCallbackReceiver; + + @Before + public void setUp() { + EditorFragment editorFragment = mock(EditorFragment.class); + mJsCallbackReceiver = new JsCallbackReceiver(editorFragment); + } + + @Test + public void testCallbacksRecognized() { + mJsCallbackReceiver.executeCallback("callback-dom-loaded", ""); + assertNotLogged("Unhandled callback"); + + mJsCallbackReceiver.executeCallback("callback-new-field", "field-name"); + assertNotLogged("Unhandled callback"); + + mJsCallbackReceiver.executeCallback("callback-input", "arguments"); + assertNotLogged("Unhandled callback"); + + mJsCallbackReceiver.executeCallback("callback-selection-changed", "arguments"); + assertNotLogged("Unhandled callback"); + + mJsCallbackReceiver.executeCallback("callback-selection-style", "arguments"); + assertNotLogged("Unhandled callback"); + + mJsCallbackReceiver.executeCallback("callback-focus-in", ""); + assertNotLogged("Unhandled callback"); + + mJsCallbackReceiver.executeCallback("callback-focus-out", ""); + assertNotLogged("Unhandled callback"); + + mJsCallbackReceiver.executeCallback("callback-image-replaced", "arguments"); + assertNotLogged("Unhandled callback"); + + mJsCallbackReceiver.executeCallback("callback-image-tap", "arguments"); + assertNotLogged("Unhandled callback"); + + mJsCallbackReceiver.executeCallback("callback-link-tap", "arguments"); + assertNotLogged("Unhandled callback"); + + mJsCallbackReceiver.executeCallback("callback-log", "arguments"); + assertNotLogged("Unhandled callback"); + } + + @Test + public void testUnknownCallbackShouldBeLogged() { + mJsCallbackReceiver.executeCallback("callback-does-not-exist", "content"); + assertLogged(Log.DEBUG, EDITOR_LOG_TAG, "Unhandled callback: callback-does-not-exist:content", null); + } + + @Test + public void testCallbackLog() { + mJsCallbackReceiver.executeCallback("callback-log", "msg=test-message"); + assertLogged(Log.DEBUG, EDITOR_LOG_TAG, "callback-log: test-message", null); + } + + private void assertLogged(int type, String tag, String msg, Throwable throwable) { + LogItem lastLog = ShadowLog.getLogs().get(0); + assertEquals(type, lastLog.type); + assertEquals(msg, lastLog.msg); + assertEquals(tag, lastLog.tag); + assertEquals(throwable, lastLog.throwable); + } + + private void assertNotLogged(String msg) { + List logList = ShadowLog.getLogs(); + if (!logList.isEmpty()) { + assertFalse(logList.get(0).msg.contains(msg)); + ShadowLog.reset(); + } + } +} From db3bd70c2c3444225d8ed8fbce742f5352dc0552 Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Tue, 14 Apr 2015 08:53:26 -0400 Subject: [PATCH 11/13] Added units tests for Utils --- .../wordpress/android/editor/UtilsTest.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 WordPressEditor/src/androidTest/java/org/wordpress/android/editor/UtilsTest.java diff --git a/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/UtilsTest.java b/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/UtilsTest.java new file mode 100644 index 000000000000..54ade7107cd0 --- /dev/null +++ b/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/UtilsTest.java @@ -0,0 +1,70 @@ +package org.wordpress.android.editor; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.wordpress.android.editor.Utils.*; + +@Config(emulateSdk = 18) +@RunWith(RobolectricTestRunner.class) +public class UtilsTest { + + @Test + public void testSplitDelimitedString() { + Set splitString = new HashSet<>(); + + // Test normal usage + splitString.add("p"); + splitString.add("bold"); + splitString.add("justifyLeft"); + + assertEquals(splitString, splitDelimitedString("p~bold~justifyLeft", "~")); + + // Test empty string + assertEquals(Collections.emptySet(), splitDelimitedString("", "~")); + } + + @Test + public void testGetChangeMapFromSets() { + Set oldSet = new HashSet<>(); + Set newSet = new HashSet<>(); + Map expectedMap = new HashMap<>(); + + // Test normal usage + oldSet.add("p"); + oldSet.add("bold"); + oldSet.add("justifyLeft"); + + newSet.add("p"); + newSet.add("justifyRight"); + + expectedMap.put("bold", false); + expectedMap.put("justifyLeft", false); + expectedMap.put("justifyRight", true); + + assertEquals(expectedMap, getChangeMapFromSets(oldSet, newSet)); + + // Test no changes + oldSet.clear(); + oldSet.add("p"); + oldSet.add("bold"); + + newSet.clear(); + newSet.add("p"); + newSet.add("bold"); + + assertEquals(Collections.emptyMap(), getChangeMapFromSets(oldSet, newSet)); + + // Test empty sets + assertEquals(Collections.emptyMap(), getChangeMapFromSets(Collections.emptySet(), Collections.emptySet())); + } +} From 418d4b3ba81093e5a8c6450d6ee28f8220865d7a Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Tue, 14 Apr 2015 09:02:36 -0400 Subject: [PATCH 12/13] Some EditorFragment cleanup --- .../java/org/wordpress/android/editor/EditorFragment.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 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 87bc449bccaa..51b044749bb0 100644 --- a/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java +++ b/WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java @@ -1,6 +1,7 @@ package org.wordpress.android.editor; import android.annotation.SuppressLint; +import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; @@ -37,6 +38,8 @@ public class EditorFragment extends EditorFragmentAbstract implements View.OnCli private String mParamTitle; private String mParamContent; + + private Activity mActivity; private EditorWebView mWebView; private final Map mTagToggleButtonMap = new HashMap<>(); @@ -56,6 +59,7 @@ public EditorFragment() { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + mActivity = getActivity(); if (getArguments() != null) { mParamTitle = getArguments().getString(ARG_PARAM_TITLE); mParamContent = getArguments().getString(ARG_PARAM_CONTENT); @@ -104,7 +108,7 @@ public boolean onJsAlert(WebView view, String url, String message, JsResult resu } }); - String htmlEditor = Utils.getHtmlFromFile(getActivity(), "android-editor.html"); + String htmlEditor = Utils.getHtmlFromFile(mActivity, "android-editor.html"); mWebView.addJavascriptInterface(new JsCallbackReceiver(this), JS_CALLBACK_HANDLER); @@ -170,7 +174,7 @@ public void onDomLoaded() { mWebView.post(new Runnable() { public void run() { String title = "I'm editing a post!"; - String contentHtml = Utils.getHtmlFromFile(getActivity(), "example-content.html"); + String contentHtml = Utils.getHtmlFromFile(mActivity, "example-content.html"); mWebView.execJavaScriptFromString("ZSSEditor.getField('zss_field_content').setMultiline('true');"); From 228a563f4afe1e8b2fb7fcd79cc7cebb25585b32 Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Wed, 15 Apr 2015 16:46:40 -0400 Subject: [PATCH 13/13] Qualified Utils static imports in UtilsTest --- .../java/org/wordpress/android/editor/UtilsTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/UtilsTest.java b/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/UtilsTest.java index 54ade7107cd0..e37e04ae7934 100644 --- a/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/UtilsTest.java +++ b/WordPressEditor/src/androidTest/java/org/wordpress/android/editor/UtilsTest.java @@ -12,7 +12,8 @@ import java.util.Set; import static org.junit.Assert.assertEquals; -import static org.wordpress.android.editor.Utils.*; +import static org.wordpress.android.editor.Utils.getChangeMapFromSets; +import static org.wordpress.android.editor.Utils.splitDelimitedString; @Config(emulateSdk = 18) @RunWith(RobolectricTestRunner.class)