Skip to content

Instantly share code, notes, and snippets.

@extralam
Created August 4, 2013 09:14
Show Gist options
  • Save extralam/6149817 to your computer and use it in GitHub Desktop.
Save extralam/6149817 to your computer and use it in GitHub Desktop.
A Custom WebView Helper for interface JS, and some common use function integrated.
package com.kirin.ui;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
public class CustomWebView extends WebView{
private final String TAG = "CUSTOMWEBVIEW";
/** custom view created by the browser (a video player for example) */
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private String js = "";
private JavaScriptInterface javaInterface;
static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
public CustomWebView(Context context) {
super(context);
}
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void init(){
js = getAssetsFileToString(getContext(), "jsInterface.js");
webviewSetting(this, new CustomWebViewChromeClient(), new CustomWebViewClient(), null);
}
/**
* after init() function
*/
public void setWebChromeClient(WebChromeClient myWebChromeClient){
this.setWebChromeClient(myWebChromeClient);
}
/**
* after init() function
*/
public void setWebViewclient(WebViewClient mWebViewClient){
this.setWebViewClient(mWebViewClient);
}
/**
* setup webview setup
* @param w = webview
*/
public void webviewSetting(WebView w , WebChromeClient myWebChromeClient, WebViewClient wvc ,JavaScriptInterface mJS ){
w.setWebViewClient(wvc);
w.setWebChromeClient(myWebChromeClient);
w.setInitialScale(0);
WebSettings setting = w.getSettings();
setting.setUseWideViewPort(true);
setting.setLoadWithOverviewMode(true);
setting.setJavaScriptEnabled(true); //--- JavaScript
setting.setGeolocationEnabled(true);
setting.setJavaScriptCanOpenWindowsAutomatically(true);
setting.setLoadsImagesAutomatically(true);
setting.setPluginsEnabled(true);
setting.setLightTouchEnabled(true);
setting.setSupportZoom(true);
setting.setBuiltInZoomControls(true);
//--- HTML5 Database
setting.setDatabaseEnabled(true); //--- HTML5 Database
//--- Database WebChromeClient.onExceededDatabaseQuota(~)
setting.setDatabasePath("/data/data/" + w.getContext().getPackageName() + "/database");
//--- HTML5DOM Storage
setting.setDomStorageEnabled(true); //--- HTML5 DOM Storage
//--- HTML5 Cache
//--- DatabaseWebChromeClient.onReachedMaxAppCacheSize(~)
setting.setAppCacheMaxSize(1024 * 1024 * 8); //--- Default Cache Size 8 MB
setting.setAppCachePath("/data/data/" + w.getContext().getPackageName() + "/cache");
setting.setAppCacheEnabled(true);
setting.setAllowFileAccess(true);
setting.setCacheMode(WebSettings.LOAD_DEFAULT);
if(mJS == null){
if(javaInterface == null)
javaInterface = new JavaScriptInterface();
}else{
javaInterface = mJS;
}
w.addJavascriptInterface(javaInterface, "_custom_js_controller");
}
public class CustomWebViewChromeClient extends WebChromeClient{
@Override
public void onConsoleMessage(String message, int lineNumber,
String sourceID) {
super.onConsoleMessage(message, lineNumber, sourceID);
l("MyApplication " + message + " -- From line "
+ lineNumber + " of "
+ sourceID );
}
};
public class JavaScriptInterface {
public void request(String jsResult) {
}
}
public class CustomWebViewClient extends WebViewClient{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
l("CustomWebViewClient onPageStarted : " + url);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
l("CustomWebViewClient onPageFinished");
loadSepcialResourceOnPageFinished(view , js);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
l("CustomWebViewClient shouldOverrideUrlLoading : " + url);
view.loadUrl(url);
return true ;
}
@Override
public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
l("CustomWebViewClient shouldOverrideUrlLoading : " + errorCod + " , des : " + description + " failingurl : " + failingUrl);
}
}
public void showCustomView(View view, WebChromeClient.CustomViewCallback callback) {
// This code is adapted from the original Android Browser code, licensed under the Apache License, Version 2.0
l("showing Custom View");
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
// Store the view and its callback for later (to kill it properly)
mCustomView = view;
mCustomViewCallback = callback;
// Add the custom view to its container.
ViewGroup parent = (ViewGroup) this.getParent();
parent.addView(view, COVER_SCREEN_GRAVITY_CENTER);
// Hide the content view.
this.setVisibility(View.GONE);
// Finally show the custom view container.
parent.setVisibility(View.VISIBLE);
parent.bringToFront();
}
public void hideCustomView() {
// This code is adapted from the original Android Browser code, licensed under the Apache License, Version 2.0
l("Hidding Custom View");
if (mCustomView == null) return;
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
ViewGroup parent = (ViewGroup) this.getParent();
parent.removeView(mCustomView);
mCustomView = null;
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
this.setVisibility(View.VISIBLE);
}
/**
* if the video overlay is showing then we need to know
* as it effects back button handling
*
* @return
*/
public boolean isCustomViewShowing() {
return mCustomView != null;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
// If back key
if (keyCode == KeyEvent.KEYCODE_BACK) {
// A custom view is currently displayed (e.g. playing a video)
if(mCustomView != null) {
this.hideCustomView();
}
}
//Does webkit change this behavior?
return super.onKeyUp(keyCode, event);
}
/**
* load special resources - js to current webpage
* @param wv
* @param res
*/
private void loadSepcialResourceOnPageFinished(WebView wv , String res){
wv.loadUrl("javascript: " + res );
}
private String getAssetsFileToString(Context context , String path){
String xmlString = null;
AssetManager am = context.getAssets();
try {
InputStream is = am.open(path);
int length = is.available();
byte[] data = new byte[length];
is.read(data);
xmlString = new String(data);
} catch (IOException e1) {
l("getAssets file error");
e1.printStackTrace();
}
return xmlString;
}
/**
* LogCat Helper
* @param str
*/
private void l(String str){
Log.d(TAG, "" + str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment