Skip to content

Instantly share code, notes, and snippets.

@Aracem
Created May 9, 2013 09:32
Show Gist options
  • Save Aracem/5546538 to your computer and use it in GitHub Desktop.
Save Aracem/5546538 to your computer and use it in GitHub Desktop.
Base WebView perfect to extend in your project and initialize it like you want -- this implementation hasn't any inicialization -- It has a workaround to avoid problems on Android 4.2.x version with the webview implemented by Jose I Honrado Has a method to load a Error page. Has a method to enable with reflection (when possible) the "plugins" at…
package com.acdroid.widget.webview;
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import timber.log.Timber;
/**
* Created by Marcos Trujillo (>‘o’)> on 3/02/14.
*/
public class BaseWebView extends WebView {
/** UTF-8 Charset */
public static final String CHARSET_UTF8 = "UTF-8";
/** Path to webview html template */
public static final String HTML_TEMPLATE_ERROR = "html/template_webview_error.html";
/** substring where the error message will be placed */
public static final String HTML_TEMPLATE_CONTENT = "##CONTENT##";
public MioBaseWebView(Context context) {
/*
* Important: don't allow any WebView subclass to be instantiated using
* an Activity context, as it will leak on Froyo devices and earlier.
*/
super(context.getApplicationContext());
enablePlugins(false);
}
public MioBaseWebView(Context context, AttributeSet attrs) {
super(context.getApplicationContext(), attrs);
enablePlugins(false);
}
public MioBaseWebView(Context context, AttributeSet attrs, int defStyle) {
super(context.getApplicationContext(), attrs, defStyle);
enablePlugins(false);
}
/**
* Loads an error page with the <var>error message</var>
*
* @param error message
*/
public void loadErrorPage(String error) {
try {
// Load HTML template from assets
String htmlContent = "";
InputStream is = getContext().getAssets().open(HTML_TEMPLATE_ERROR);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
htmlContent += line;
}
br.close();
htmlContent = htmlContent.replace(HTML_TEMPLATE_CONTENT, error);
loadData(htmlContent, "text/html; charset=" + CHARSET_UTF8, CHARSET_UTF8);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDraw(Canvas canvas) {
// Workaround for problems in Android 4.2.x
// You can't type properly in input texts and nor textareas until you
// touch the webview
// You can't change the font size until you touch the webview
// http://stackoverflow.com/questions/15127762/webview-fails-to-render-until-touched-android-4-2-2
if (SupportVersion.JellyBean()) {
invalidate();
}
super.onDraw(canvas);
}
protected void enablePlugins(final boolean enabled) {
// Android 4.3 and above has no concept of plugin states
if (SupportVersion.JellyBeanMR1()) {
return;
}
if (SupportVersion.Froyo()) {
// Note: this is needed to compile against api level 18.
try {
Class<Enum> pluginStateClass = (Class<Enum>) Class
.forName("android.webkit.WebSettings$PluginState");
Class<?>[] parameters = {pluginStateClass};
Method method = getSettings().getClass()
.getDeclaredMethod("setPluginState", parameters);
Object pluginState = Enum.valueOf(pluginStateClass, enabled ? "ON" : "OFF");
method.invoke(getSettings(), pluginState);
} catch (Exception e) {
Timber.w("Unable to modify WebView plugin state.");
}
} else {
try {
Method method = Class.forName("android.webkit.WebSettings")
.getDeclaredMethod("setPluginsEnabled", boolean.class);
method.invoke(getSettings(), enabled);
} catch (Exception e) {
Timber.w("Unable to " + (enabled ? "enable" : "disable")
+ "WebSettings plugins for BaseWebView.");
}
}
}
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<style type="text/css">
body { font-size:1em; padding:.15em; }
.error { text-align:center; font-weight:bold; font-size:1.3em; text-color:#555; margin-top:6em; }
</style>
</head>
<body>
<p class="error">##CONTENT##</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment