Refactor: Use ConfigXmlParser in activity. Adds CordovaWebView.init()

This does subtly change the API surface due to CordovaWebView.init(),
but only minimally, and is backwards compatibly with the default
generated projects from prior versions.
This commit is contained in:
Andrew Grieve 2014-07-04 16:32:09 -04:00
parent b636874bd9
commit 705991e5b0
7 changed files with 176 additions and 329 deletions

View File

@ -28,10 +28,7 @@ public class __ACTIVITY__ extends CordovaActivity
public void onCreate(Bundle savedInstanceState) public void onCreate(Bundle savedInstanceState)
{ {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
super.init();
// Set by <content src="index.html" /> in config.xml // Set by <content src="index.html" /> in config.xml
super.loadUrl(Config.getStartUrl()); loadUrl(launchUrl);
//super.loadUrl("file:///android_asset/www/index.html");
} }
} }

View File

@ -25,7 +25,7 @@ import android.util.Log;
public class Config { public class Config {
private static final String TAG = "Config"; private static final String TAG = "Config";
private static ConfigXmlParser parser; static ConfigXmlParser parser;
private Config() { private Config() {
} }

View File

@ -18,8 +18,8 @@
*/ */
package org.apache.cordova; package org.apache.cordova;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -72,9 +72,8 @@ import android.widget.LinearLayout;
* &#64;Override * &#64;Override
* public void onCreate(Bundle savedInstanceState) { * public void onCreate(Bundle savedInstanceState) {
* super.onCreate(savedInstanceState); * super.onCreate(savedInstanceState);
* super.init();
* // Load your application * // Load your application
* super.loadUrl(Config.getStartUrl()); * loadUrl(launchUrl);
* } * }
* } * }
* </pre> * </pre>
@ -110,10 +109,6 @@ public class CordovaActivity extends Activity implements CordovaInterface {
protected CordovaPlugin activityResultCallback = null; protected CordovaPlugin activityResultCallback = null;
protected boolean activityResultKeepRunning; protected boolean activityResultKeepRunning;
// Default background color for activity
// (this is not the color for the webview, which is set in HTML)
private int backgroundColor = Color.BLACK;
/* /*
* The variables below are used to cache some of the activity properties. * The variables below are used to cache some of the activity properties.
*/ */
@ -133,6 +128,12 @@ public class CordovaActivity extends Activity implements CordovaInterface {
private String initCallbackClass; private String initCallbackClass;
// Read from config.xml:
protected CordovaPreferences preferences;
protected Whitelist whitelist;
protected String launchUrl;
protected ArrayList<PluginEntry> pluginEntries;
/** /**
* Sets the authentication token. * Sets the authentication token.
* *
@ -194,10 +195,8 @@ public class CordovaActivity extends Activity implements CordovaInterface {
/** /**
* Called when the activity is first created. * Called when the activity is first created.
*/ */
@SuppressWarnings("deprecation")
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
Config.init(this);
LOG.i(TAG, "Apache Cordova native platform version " + CordovaWebView.CORDOVA_VERSION + " is starting"); LOG.i(TAG, "Apache Cordova native platform version " + CordovaWebView.CORDOVA_VERSION + " is starting");
LOG.d(TAG, "CordovaActivity.onCreate()"); LOG.d(TAG, "CordovaActivity.onCreate()");
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -207,17 +206,19 @@ public class CordovaActivity extends Activity implements CordovaInterface {
initCallbackClass = savedInstanceState.getString("callbackClass"); initCallbackClass = savedInstanceState.getString("callbackClass");
} }
if(!this.getBooleanProperty("ShowTitle", false)) loadConfig();
if(!preferences.getBoolean("ShowTitle", false))
{ {
getWindow().requestFeature(Window.FEATURE_NO_TITLE); getWindow().requestFeature(Window.FEATURE_NO_TITLE);
} }
if(this.getBooleanProperty("SetFullscreen", false)) if(preferences.getBoolean("SetFullscreen", false))
{ {
Log.d(TAG, "The SetFullscreen configuration is deprecated in favor of Fullscreen, and will be removed in a future version."); Log.d(TAG, "The SetFullscreen configuration is deprecated in favor of Fullscreen, and will be removed in a future version.");
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else if (this.getBooleanProperty("Fullscreen", false)) { } else if (preferences.getBoolean("Fullscreen", false)) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else { } else {
@ -225,6 +226,35 @@ public class CordovaActivity extends Activity implements CordovaInterface {
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} }
appView = makeWebView();
appView.init(this, makeWebViewClient(appView), makeChromeClient(appView), pluginEntries);
// TODO: Have the views set this themselves.
if (preferences.getBoolean("DisallowOverscroll", false)) {
appView.setOverScrollMode(View.OVER_SCROLL_NEVER);
}
createViews();
// TODO: Make this a preference (CB-6153)
// Setup the hardware volume controls to handle volume control
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
protected void loadConfig() {
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(this);
preferences = parser.getPreferences();
preferences.setPreferencesBundle(getIntent().getExtras());
preferences.copyIntoIntentExtras(this);
whitelist = parser.getWhitelist();
launchUrl = parser.getLaunchUrl();
pluginEntries = parser.getPluginEntries();
Config.parser = parser;
}
@SuppressWarnings("deprecation")
protected void createViews() {
// This builds the view. We could probably get away with NOT having a LinearLayout, but I like having a bucket!
// This builds the view. We could probably get away with NOT having a LinearLayout, but I like having a bucket! // This builds the view. We could probably get away with NOT having a LinearLayout, but I like having a bucket!
Display display = getWindowManager().getDefaultDisplay(); Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); int width = display.getWidth();
@ -232,12 +262,23 @@ public class CordovaActivity extends Activity implements CordovaInterface {
root = new LinearLayoutSoftKeyboardDetect(this, width, height); root = new LinearLayoutSoftKeyboardDetect(this, width, height);
root.setOrientation(LinearLayout.VERTICAL); root.setOrientation(LinearLayout.VERTICAL);
root.setBackgroundColor(this.backgroundColor);
root.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, root.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT, 0.0F)); ViewGroup.LayoutParams.MATCH_PARENT, 0.0F));
// Setup the hardware volume controls to handle volume control appView.setId(100);
setVolumeControlStream(AudioManager.STREAM_MUSIC); appView.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
1.0F));
// Add web view but make it invisible while loading URL
appView.setVisibility(View.INVISIBLE);
root.addView((View) appView);
setContentView(root);
// TODO: Setting this on the appView causes it to show when <html style="opacity:0">.
int backgroundColor = preferences.getInteger("BackgroundColor", Color.BLACK);
root.setBackgroundColor(backgroundColor);
} }
/** /**
@ -266,11 +307,7 @@ public class CordovaActivity extends Activity implements CordovaInterface {
* @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(this);
return new CordovaWebViewClient(this, webView);
} else {
return new IceCreamCordovaWebViewClient(this, webView);
}
} }
/** /**
@ -282,78 +319,46 @@ public class CordovaActivity extends Activity implements CordovaInterface {
* @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 new CordovaChromeClient(this, webView); return webView.makeChromeClient(this);
} }
/** @Deprecated // No need to call init() anymore.
* Create and initialize web container with default web view objects.
*/
public void init() { public void init() {
CordovaWebView webView = makeWebView(); this.init(appView, null, null);
this.init(webView, makeWebViewClient(webView), makeChromeClient(webView));
} }
/**
* Initialize web container with web view objects.
*
* @param webView
* @param webViewClient
* @param webChromeClient
*/
@SuppressLint("NewApi") @SuppressLint("NewApi")
@Deprecated // No need to call init() anymore.
public void init(CordovaWebView webView, CordovaWebViewClient webViewClient, CordovaChromeClient webChromeClient) { public void init(CordovaWebView webView, CordovaWebViewClient webViewClient, CordovaChromeClient webChromeClient) {
LOG.d(TAG, "CordovaActivity.init()"); LOG.d(TAG, "CordovaActivity.init()");
// Set up web container appView = webView;
this.appView = webView;
this.appView.setId(100);
this.appView.setWebViewClient(webViewClient); if (webViewClient != null) {
this.appView.setWebChromeClient(webChromeClient); this.appView.setWebViewClient(webViewClient);
webViewClient.setWebView(this.appView); webViewClient.setWebView(this.appView);
webChromeClient.setWebView(this.appView); }
if (webChromeClient != null) {
this.appView.setLayoutParams(new LinearLayout.LayoutParams( this.appView.setWebChromeClient(webChromeClient);
ViewGroup.LayoutParams.MATCH_PARENT, webChromeClient.setWebView(this.appView);
ViewGroup.LayoutParams.MATCH_PARENT,
1.0F));
if (this.getBooleanProperty("DisallowOverscroll", false)) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
this.appView.setOverScrollMode(CordovaWebView.OVER_SCROLL_NEVER);
}
} }
// Add web view but make it invisible while loading URL
this.appView.setVisibility(View.INVISIBLE);
this.root.addView(this.appView);
setContentView(this.root);
} }
/** /**
* Load the url into the webview. * Load the url into the webview.
*/ */
public void loadUrl(String url) { public void loadUrl(String url) {
this.splashscreenTime = preferences.getInteger("SplashScreenDelay", this.splashscreenTime);
// Init web view if not already done String splash = preferences.getString("SplashScreen", null);
if (this.appView == null) { if(this.splashscreenTime > 0 && splash != null)
this.init();
}
this.splashscreenTime = this.getIntegerProperty("SplashScreenDelay", this.splashscreenTime);
if(this.splashscreenTime > 0)
{ {
this.splashscreen = this.getIntegerProperty("SplashScreen", 0); this.splashscreen = getResources().getIdentifier(splash, "drawable", getClass().getPackage().getName());;
if(this.splashscreen != 0) if(this.splashscreen != 0)
{ {
this.showSplashScreen(this.splashscreenTime); this.showSplashScreen(this.splashscreenTime);
} }
} }
// Set backgroundColor
this.backgroundColor = this.getIntegerProperty("BackgroundColor", Color.BLACK);
this.root.setBackgroundColor(this.backgroundColor);
// If keepRunning // If keepRunning
this.keepRunning = this.getBooleanProperty("KeepRunning", true); this.keepRunning = this.getBooleanProperty("KeepRunning", true);
@ -430,9 +435,6 @@ public class CordovaActivity extends Activity implements CordovaInterface {
*/ */
@Deprecated // Call method on appView directly. @Deprecated // Call method on appView directly.
public void clearCache() { public void clearCache() {
if (this.appView == null) {
this.init();
}
this.appView.clearCache(true); this.appView.clearCache(true);
} }
@ -459,103 +461,34 @@ public class CordovaActivity extends Activity implements CordovaInterface {
/** /**
* Get boolean property for activity. * Get boolean property for activity.
*
* @param name
* @param defaultValue
* @return the boolean value of the named property
*/ */
@Deprecated // Call method on preferences directly.
public boolean getBooleanProperty(String name, boolean defaultValue) { public boolean getBooleanProperty(String name, boolean defaultValue) {
Bundle bundle = this.getIntent().getExtras(); return preferences.getBoolean(name, defaultValue);
if (bundle == null) {
return defaultValue;
}
name = name.toLowerCase(Locale.getDefault());
Boolean p;
try {
p = (Boolean) bundle.get(name);
} catch (ClassCastException e) {
String s = bundle.get(name).toString();
if ("true".equals(s)) {
p = true;
}
else {
p = false;
}
}
if (p == null) {
return defaultValue;
}
return p.booleanValue();
} }
/** /**
* Get int property for activity. * Get int property for activity.
*
* @param name
* @param defaultValue
* @return the int value for the named property
*/ */
@Deprecated // Call method on preferences directly.
public int getIntegerProperty(String name, int defaultValue) { public int getIntegerProperty(String name, int defaultValue) {
Bundle bundle = this.getIntent().getExtras(); return preferences.getInteger(name, defaultValue);
if (bundle == null) {
return defaultValue;
}
name = name.toLowerCase(Locale.getDefault());
Integer p;
try {
p = (Integer) bundle.get(name);
} catch (ClassCastException e) {
p = Integer.parseInt(bundle.get(name).toString());
}
if (p == null) {
return defaultValue;
}
return p.intValue();
} }
/** /**
* Get string property for activity. * Get string property for activity.
*
* @param name
* @param defaultValue
* @return the String value for the named property
*/ */
@Deprecated // Call method on preferences directly.
public String getStringProperty(String name, String defaultValue) { public String getStringProperty(String name, String defaultValue) {
Bundle bundle = this.getIntent().getExtras(); return preferences.getString(name, defaultValue);
if (bundle == null) {
return defaultValue;
}
name = name.toLowerCase(Locale.getDefault());
String p = bundle.getString(name);
if (p == null) {
return defaultValue;
}
return p;
} }
/** /**
* Get double property for activity. * Get double property for activity.
*
* @param name
* @param defaultValue
* @return the double value for the named property
*/ */
@Deprecated // Call method on preferences directly.
public double getDoubleProperty(String name, double defaultValue) { public double getDoubleProperty(String name, double defaultValue) {
Bundle bundle = this.getIntent().getExtras(); return preferences.getDouble(name, defaultValue);
if (bundle == null) {
return defaultValue;
}
name = name.toLowerCase(Locale.getDefault());
Double p;
try {
p = (Double) bundle.get(name);
} catch (ClassCastException e) {
p = Double.parseDouble(bundle.get(name).toString());
}
if (p == null) {
return defaultValue;
}
return p.doubleValue();
} }
/** /**

View File

@ -72,31 +72,17 @@ public class CordovaChromeClient extends WebChromeClient {
// File Chooser // File Chooser
public ValueCallback<Uri> mUploadMessage; public ValueCallback<Uri> mUploadMessage;
/** @Deprecated
* Constructor.
*
* @param cordova
*/
public CordovaChromeClient(CordovaInterface cordova) { public CordovaChromeClient(CordovaInterface cordova) {
this.cordova = cordova; this.cordova = cordova;
} }
/**
* Constructor.
*
* @param ctx
* @param app
*/
public CordovaChromeClient(CordovaInterface ctx, CordovaWebView app) { public CordovaChromeClient(CordovaInterface ctx, CordovaWebView app) {
this.cordova = ctx; this.cordova = ctx;
this.appView = app; this.appView = app;
} }
/** @Deprecated
* Constructor.
*
* @param view
*/
public void setWebView(CordovaWebView view) { public void setWebView(CordovaWebView view) {
this.appView = view; this.appView = view;
} }

View File

@ -21,9 +21,12 @@ package org.apache.cordova;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import org.apache.cordova.Config; import org.apache.cordova.Config;
import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaInterface;
@ -122,114 +125,51 @@ public class CordovaWebView extends WebView {
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
Gravity.CENTER); Gravity.CENTER);
/**
* Constructor.
*
* @param context
*/
public CordovaWebView(Context context) { public CordovaWebView(Context context) {
super(context); this(context, null);
if (CordovaInterface.class.isInstance(context))
{
this.cordova = (CordovaInterface) context;
}
else
{
Log.d(TAG, "Your activity must implement CordovaInterface to work");
}
this.setup();
} }
/**
* Constructor.
*
* @param context
* @param attrs
*/
public CordovaWebView(Context context, AttributeSet attrs) { public CordovaWebView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
if (CordovaInterface.class.isInstance(context))
{
this.cordova = (CordovaInterface) context;
}
else
{
Log.d(TAG, "Your activity must implement CordovaInterface to work");
}
this.setWebChromeClient(new CordovaChromeClient(this.cordova, this));
this.initWebViewClient(this.cordova);
this.setup();
} }
/** @Deprecated
* Constructor.
*
* @param context
* @param attrs
* @param defStyle
*
*/
public CordovaWebView(Context context, AttributeSet attrs, int defStyle) { public CordovaWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); super(context, attrs, defStyle);
if (CordovaInterface.class.isInstance(context))
{
this.cordova = (CordovaInterface) context;
}
else
{
Log.d(TAG, "Your activity must implement CordovaInterface to work");
}
this.setWebChromeClient(new CordovaChromeClient(this.cordova, this));
this.setup();
} }
/**
* Constructor.
*
* @param context
* @param attrs
* @param defStyle
* @param privateBrowsing
*/
@TargetApi(11) @TargetApi(11)
@Deprecated
public CordovaWebView(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) { public CordovaWebView(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
super(context, attrs, defStyle, privateBrowsing); super(context, attrs, defStyle, privateBrowsing);
if (CordovaInterface.class.isInstance(context))
{
this.cordova = (CordovaInterface) context;
}
else
{
Log.d(TAG, "Your activity must implement CordovaInterface to work");
}
this.setWebChromeClient(new CordovaChromeClient(this.cordova));
this.initWebViewClient(this.cordova);
this.setup();
} }
/** // Use two-phase init so that the control will work with XML layouts.
* set the WebViewClient, but provide special case handling for IceCreamSandwich. public void init(CordovaInterface cordova, CordovaWebViewClient webViewClient, CordovaChromeClient chromeClient, List<PluginEntry> pluginEntries) {
*/ if (this.cordova != null) {
private void initWebViewClient(CordovaInterface cordova) { throw new IllegalStateException();
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)
{
this.setWebViewClient(new CordovaWebViewClient(this.cordova, this));
}
else
{
this.setWebViewClient(new IceCreamCordovaWebViewClient(this.cordova, this));
} }
this.cordova = cordova;
this.viewClient = webViewClient;
this.chromeClient = chromeClient;
super.setWebChromeClient(chromeClient);
super.setWebViewClient(webViewClient);
pluginManager = new PluginManager(this, this.cordova, pluginEntries);
jsMessageQueue = new NativeToJsMessageQueue(this, cordova);
exposedJsApi = new ExposedJsApi(pluginManager, jsMessageQueue);
resourceApi = new CordovaResourceApi(this.getContext(), pluginManager);
initWebViewSettings();
exposeJsInterface();
} }
/** @SuppressLint("SetJavaScriptEnabled")
* Initialize webview.
*/
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@SuppressLint("NewApi") private void initWebViewSettings() {
private void setup() {
this.setInitialScale(0); this.setInitialScale(0);
this.setVerticalScrollBarEnabled(false); this.setVerticalScrollBarEnabled(false);
// TODO: The Activity is the one that should call requestFocus().
if (shouldRequestFocusOnInit()) { if (shouldRequestFocusOnInit()) {
this.requestFocusFromTouch(); this.requestFocusFromTouch();
} }
@ -270,30 +210,16 @@ public class CordovaWebView extends WebView {
Level16Apis.enableUniversalAccess(settings); Level16Apis.enableUniversalAccess(settings);
// Enable database // Enable database
// We keep this disabled because we use or shim to get around DOM_EXCEPTION_ERROR_16 // We keep this disabled because we use or shim to get around DOM_EXCEPTION_ERROR_16
String databasePath = this.cordova.getActivity().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); String databasePath = getContext().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
settings.setDatabaseEnabled(true); settings.setDatabaseEnabled(true);
settings.setDatabasePath(databasePath); settings.setDatabasePath(databasePath);
//Determine whether we're in debug or release mode, and turn on Debugging! //Determine whether we're in debug or release mode, and turn on Debugging!
try { ApplicationInfo appInfo = getContext().getApplicationContext().getApplicationInfo();
final String packageName = this.cordova.getActivity().getPackageName(); if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0 &&
final PackageManager pm = this.cordova.getActivity().getPackageManager(); android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
ApplicationInfo appInfo; enableRemoteDebugging();
appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
if((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0 &&
android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT)
{
setWebContentsDebuggingEnabled(true);
}
} catch (IllegalArgumentException e) {
Log.d(TAG, "You have one job! To turn on Remote Web Debugging! YOU HAVE FAILED! ");
e.printStackTrace();
} catch (NameNotFoundException e) {
Log.d(TAG, "This should never happen: Your application's package can't be found.");
e.printStackTrace();
} }
settings.setGeolocationDatabasePath(databasePath); settings.setGeolocationDatabasePath(databasePath);
@ -307,13 +233,12 @@ public class CordovaWebView extends WebView {
// Enable AppCache // Enable AppCache
// Fix for CB-2282 // Fix for CB-2282
settings.setAppCacheMaxSize(5 * 1048576); settings.setAppCacheMaxSize(5 * 1048576);
String pathToCache = this.cordova.getActivity().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); settings.setAppCachePath(databasePath);
settings.setAppCachePath(pathToCache);
settings.setAppCacheEnabled(true); settings.setAppCacheEnabled(true);
// Fix for CB-1405 // Fix for CB-1405
// Google issue 4641 // Google issue 4641
this.updateUserAgentString(); settings.getUserAgentString();
IntentFilter intentFilter = new IntentFilter(); IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
@ -321,18 +246,33 @@ public class CordovaWebView extends WebView {
this.receiver = new BroadcastReceiver() { this.receiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
updateUserAgentString(); getSettings().getUserAgentString();
} }
}; };
this.cordova.getActivity().registerReceiver(this.receiver, intentFilter); getContext().registerReceiver(this.receiver, intentFilter);
} }
// end CB-1405 // end CB-1405
}
pluginManager = new PluginManager(this, this.cordova); @TargetApi(Build.VERSION_CODES.KITKAT)
jsMessageQueue = new NativeToJsMessageQueue(this, cordova); private void enableRemoteDebugging() {
exposedJsApi = new ExposedJsApi(pluginManager, jsMessageQueue); try {
resourceApi = new CordovaResourceApi(this.getContext(), pluginManager); WebView.setWebContentsDebuggingEnabled(true);
exposeJsInterface(); } catch (IllegalArgumentException e) {
Log.d(TAG, "You have one job! To turn on Remote Web Debugging! YOU HAVE FAILED! ");
e.printStackTrace();
}
}
public CordovaChromeClient makeChromeClient(CordovaInterface cordova) {
return new CordovaChromeClient(cordova, this);
}
public CordovaWebViewClient makeWebViewClient(CordovaInterface cordova) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return new CordovaWebViewClient(cordova, this);
}
return new IceCreamCordovaWebViewClient(cordova, this);
} }
/** /**
@ -345,13 +285,8 @@ public class CordovaWebView extends WebView {
return true; return true;
} }
private void updateUserAgentString() {
this.getSettings().getUserAgentString();
}
private void exposeJsInterface() { private void exposeJsInterface() {
int SDK_INT = Build.VERSION.SDK_INT; if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)) {
if ((SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)) {
Log.i(TAG, "Disabled addJavascriptInterface() bridge since Android version is old."); Log.i(TAG, "Disabled addJavascriptInterface() bridge since Android version is old.");
// Bug being that Java Strings do not get converted to JS strings automatically. // Bug being that Java Strings do not get converted to JS strings automatically.
// This isn't hard to work-around on the JS side, but it's easier to just // This isn't hard to work-around on the JS side, but it's easier to just
@ -363,9 +298,8 @@ public class CordovaWebView extends WebView {
/** /**
* Set the WebViewClient. * Set the WebViewClient.
*
* @param client
*/ */
@Deprecated // Set this in init() instead.
public void setWebViewClient(CordovaWebViewClient client) { public void setWebViewClient(CordovaWebViewClient client) {
this.viewClient = client; this.viewClient = client;
super.setWebViewClient(client); super.setWebViewClient(client);
@ -373,9 +307,8 @@ public class CordovaWebView extends WebView {
/** /**
* Set the WebChromeClient. * Set the WebChromeClient.
*
* @param client
*/ */
@Deprecated // Set this in init() instead.
public void setWebChromeClient(CordovaChromeClient client) { public void setWebChromeClient(CordovaChromeClient client) {
this.chromeClient = client; this.chromeClient = client;
super.setWebChromeClient(client); super.setWebChromeClient(client);
@ -849,7 +782,7 @@ public class CordovaWebView extends WebView {
// unregister the receiver // unregister the receiver
if (this.receiver != null) { if (this.receiver != null) {
try { try {
this.cordova.getActivity().unregisterReceiver(this.receiver); getContext().unregisterReceiver(this.receiver);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Error unregistering configuration receiver: " + e.getMessage(), e); Log.e(TAG, "Error unregistering configuration receiver: " + e.getMessage(), e);
} }

View File

@ -64,11 +64,7 @@ public class CordovaWebViewClient extends WebViewClient {
/** The authorization tokens. */ /** The authorization tokens. */
private Hashtable<String, AuthenticationToken> authenticationTokens = new Hashtable<String, AuthenticationToken>(); private Hashtable<String, AuthenticationToken> authenticationTokens = new Hashtable<String, AuthenticationToken>();
/** @Deprecated
* Constructor.
*
* @param cordova
*/
public CordovaWebViewClient(CordovaInterface cordova) { public CordovaWebViewClient(CordovaInterface cordova) {
this.cordova = cordova; this.cordova = cordova;
} }
@ -90,6 +86,7 @@ public class CordovaWebViewClient extends WebViewClient {
* *
* @param view * @param view
*/ */
@Deprecated
public void setWebView(CordovaWebView view) { public void setWebView(CordovaWebView view) {
this.appView = view; this.appView = view;
helper = new CordovaUriHelper(cordova, view); helper = new CordovaUriHelper(cordova, view);

View File

@ -52,11 +52,31 @@ public class PluginManager {
// Stores mapping of Plugin Name -> <url-filter> values. // Stores mapping of Plugin Name -> <url-filter> values.
// Using <url-filter> is deprecated. // Using <url-filter> is deprecated.
protected HashMap<String, List<String>> urlMap; protected HashMap<String, List<String>> urlMap = new HashMap<String, List<String>>();
public PluginManager(CordovaWebView app, CordovaInterface ctx) { @Deprecated
this.ctx = ctx; PluginManager(CordovaWebView cordovaWebView, CordovaInterface cordova) {
this.app = app; this(cordovaWebView, cordova, null);
}
PluginManager(CordovaWebView cordovaWebView, CordovaInterface cordova, List<PluginEntry> pluginEntries) {
this.ctx = cordova;
this.app = cordovaWebView;
if (pluginEntries == null) {
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(ctx.getActivity());
pluginEntries = parser.getPluginEntries();
}
setPluginEntries(pluginEntries);
}
public void setPluginEntries(List<PluginEntry> pluginEntries) {
this.onPause(false);
this.onDestroy();
this.clearPluginObjects();
for (PluginEntry entry : pluginEntries) {
addService(entry);
}
} }
/** /**
@ -64,33 +84,14 @@ public class PluginManager {
*/ */
public void init() { public void init() {
LOG.d(TAG, "init()"); LOG.d(TAG, "init()");
this.onPause(false);
// If first time, then load plugins from config.xml file this.onDestroy();
if (urlMap == null) { this.clearPluginObjects();
this.loadPlugins();
}
// Stop plugins on current HTML page and discard plugin objects
else {
this.onPause(false);
this.onDestroy();
this.clearPluginObjects();
}
// Start up all plugins that have onload specified
this.startupPlugins(); this.startupPlugins();
} }
/** @Deprecated
* Load plugins from res/xml/config.xml
*/
public void loadPlugins() { public void loadPlugins() {
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(ctx.getActivity());
urlMap = new HashMap<String, List<String>>();
for (PluginEntry entry : parser.getPluginEntries()) {
addService(entry);
}
} }
/** /**