Skip to content

Commit

Permalink
Merge pull request #3804 from wordpress-mobile/issue/enable-editor-de…
Browse files Browse the repository at this point in the history
…bug-mode

Enable editor debug mode when BuildConfig.DEBUG is true
  • Loading branch information
aforcier committed Mar 4, 2016
2 parents 878f7cd + ff707a2 commit df24592
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import android.webkit.URLUtil;
import android.widget.Toast;

import org.wordpress.android.BuildConfig;
import org.wordpress.android.Constants;
import org.wordpress.android.JavaScriptException;
import org.wordpress.android.R;
Expand Down Expand Up @@ -2034,6 +2035,7 @@ public void onEditorFragmentInitialized() {
fillContentEditorFields();
// Set the error listener
if (mEditorFragment instanceof EditorFragment) {
mEditorFragment.setDebugModeEnabled(BuildConfig.DEBUG);
((EditorFragment) mEditorFragment).setWebViewErrorListener(new ErrorListener() {
@Override
public void onJavaScriptError(String sourceFile, int lineNumber, String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -74,8 +71,6 @@ public class EditorFragment extends EditorFragmentAbstract implements View.OnCli
private static final float TOOLBAR_ALPHA_ENABLED = 1;
private static final float TOOLBAR_ALPHA_DISABLED = 0.5f;

protected static final int BUTTON_ID_LOG_HTML = 555;

private String mTitle = "";
private String mContentHtml = "";

Expand Down Expand Up @@ -409,8 +404,6 @@ protected void initJsEditor() {

if (mDebugModeEnabled) {
enableWebDebugging(true);
// Enable the HTML logging button
setHasOptionsMenu(true);
}
}

Expand Down Expand Up @@ -640,38 +633,11 @@ public void run() {

@SuppressLint("NewApi")
private void enableWebDebugging(boolean enable) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
AppLog.i(T.EDITOR, "Enabling web debugging");
WebView.setWebContentsDebuggingEnabled(enable);
}
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add(0, BUTTON_ID_LOG_HTML, 0, "Log HTML")
.setIcon(R.drawable.ic_log_html)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == BUTTON_ID_LOG_HTML) {
if (mDebugModeEnabled) {
// Log the raw html
mWebView.post(new Runnable() {
@Override
public void run() {
mWebView.execJavaScriptFromString("console.log(document.body.innerHTML);");
}
});
} else {
AppLog.d(T.EDITOR, "Could not execute JavaScript - debug mode not enabled");
}
return true;
} else {
return super.onOptionsItemSelected(item);
}
mWebView.setDebugModeEnabled(mDebugModeEnabled);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.wordpress.android.util.AppLog.T;
import org.wordpress.android.util.HTTPUtils;
import org.wordpress.android.util.StringUtils;
import org.wordpress.android.util.ToastUtils;
import org.wordpress.android.util.UrlUtils;

import java.io.IOException;
Expand All @@ -40,6 +41,7 @@ public abstract class EditorWebViewAbstract extends WebView {
private AuthHeaderRequestListener mAuthHeaderRequestListener;
private ErrorListener mErrorListener;
private JsCallbackReceiver mJsCallbackReceiver;
private boolean mDebugModeEnabled;

private Map<String, String> mHeaderMap = new HashMap<>();

Expand Down Expand Up @@ -175,6 +177,10 @@ public void setVisibility(int visibility) {
super.setVisibility(visibility);
}

public void setDebugModeEnabled(boolean enabled) {
mDebugModeEnabled = enabled;
}

/**
* Handles events that should be triggered when the WebView is hidden or is shown to the user
*
Expand Down Expand Up @@ -214,6 +220,13 @@ public boolean onKeyPreIme(int keyCode, KeyEvent event) {
mOnImeBackListener.onImeBack();
}
}
if (mDebugModeEnabled && event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP
&& event.getAction() == KeyEvent.ACTION_DOWN) {
// Log the raw html
execJavaScriptFromString("console.log(document.body.innerHTML);");
ToastUtils.showToast(getContext(), "Debug: Raw HTML has been logged");
return true;
}
return super.onKeyPreIme(keyCode, event);
}

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit df24592

Please sign in to comment.