Defer construction of client objects to WebView

This commit is contained in:
Ian Clelland 2014-04-24 14:57:34 -04:00
parent f4555f7c96
commit 8e31ef7be6
3 changed files with 30 additions and 19 deletions

View File

@ -166,8 +166,6 @@ public class AndroidWebView extends WebView implements CordovaWebView {
{ {
Log.d(TAG, "Your activity must implement CordovaInterface to work"); Log.d(TAG, "Your activity must implement CordovaInterface to work");
} }
this.setWebChromeClient((CordovaChromeClient) new AndroidChromeClient(this.cordova, this));
this.initWebViewClient(this.cordova);
this.loadConfiguration(); this.loadConfiguration();
this.setup(); this.setup();
} }
@ -190,7 +188,6 @@ public class AndroidWebView extends WebView implements CordovaWebView {
{ {
Log.d(TAG, "Your activity must implement CordovaInterface to work"); Log.d(TAG, "Your activity must implement CordovaInterface to work");
} }
this.setWebChromeClient((CordovaChromeClient) new AndroidChromeClient(this.cordova, this));
this.loadConfiguration(); this.loadConfiguration();
this.setup(); this.setup();
} }
@ -214,27 +211,39 @@ public class AndroidWebView extends WebView implements CordovaWebView {
{ {
Log.d(TAG, "Your activity must implement CordovaInterface to work"); Log.d(TAG, "Your activity must implement CordovaInterface to work");
} }
this.setWebChromeClient((CordovaChromeClient) new AndroidChromeClient(this.cordova));
this.initWebViewClient(this.cordova);
this.loadConfiguration(); this.loadConfiguration();
this.setup(); this.setup();
} }
/** /**
* set the WebViewClient, but provide special case handling for IceCreamSandwich. * Create a default WebViewClient object for this webview. This can be overridden by the
* main application's CordovaActivity subclass.
*
* By default, it creates an AndroidWebViewClient, but we provide special case handling for
* IceCreamSandwich.
*/ */
private void initWebViewClient(CordovaInterface cordova) { @Override
public CordovaWebViewClient makeWebViewClient() {
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB || if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB ||
android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
{ {
this.setWebViewClient((CordovaWebViewClient) new AndroidWebViewClient(this.cordova, this)); return (CordovaWebViewClient) new AndroidWebViewClient(this.cordova, this);
} }
else else
{ {
this.setWebViewClient((CordovaWebViewClient) new IceCreamCordovaWebViewClient(this.cordova, this)); return (CordovaWebViewClient) new IceCreamCordovaWebViewClient(this.cordova, this);
} }
} }
/**
* Create a default WebViewClient object for this webview. This can be overridden by the
* main application's CordovaActivity subclass.
*/
@Override
public CordovaChromeClient makeWebChromeClient() {
return (CordovaChromeClient) new AndroidChromeClient(this.cordova);
}
/** /**
* Initialize webview. * Initialize webview.
*/ */

View File

@ -248,29 +248,27 @@ public class CordovaActivity extends Activity implements CordovaInterface {
/** /**
* Construct the client for the default web view object. * Construct the client for the default web view object.
* *
* This is intended to be overridable by subclasses of CordovaIntent which * This is intended to be overridable by subclasses of CordovaActivity which
* require a more specialized web view. * require a more specialized web view. By default, it allows the webView
* to create its own client objects.
* *
* @param webView the default constructed web view object * @param webView the default constructed web view object
*/ */
protected CordovaWebViewClient makeWebViewClient(CordovaWebView webView) { protected CordovaWebViewClient makeWebViewClient(CordovaWebView webView) {
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { return webView.makeWebViewClient();
return (CordovaWebViewClient) new AndroidWebViewClient(this, webView);
} else {
return (CordovaWebViewClient) new IceCreamCordovaWebViewClient(this, webView);
}
} }
/** /**
* Construct the chrome client for the default web view object. * Construct the chrome client for the default web view object.
* *
* This is intended to be overridable by subclasses of CordovaIntent which * This is intended to be overridable by subclasses of CordovaActivity which
* require a more specialized web view. * require a more specialized web view. By default, it allows the webView
* to create its own client objects.
* *
* @param webView the default constructed web view object * @param webView the default constructed web view object
*/ */
protected CordovaChromeClient makeChromeClient(CordovaWebView webView) { protected CordovaChromeClient makeChromeClient(CordovaWebView webView) {
return (CordovaChromeClient) new AndroidChromeClient(this, webView); return webView.makeWebChromeClient();
} }
/** /**

View File

@ -19,6 +19,10 @@ public interface CordovaWebView {
View getView(); View getView();
CordovaWebViewClient makeWebViewClient();
CordovaChromeClient makeWebChromeClient();
void setWebViewClient(CordovaWebViewClient webViewClient); void setWebViewClient(CordovaWebViewClient webViewClient);
void setWebChromeClient(CordovaChromeClient webChromeClient); void setWebChromeClient(CordovaChromeClient webChromeClient);