Refactor: Move url-filter information into PluginEntry.

This commit is contained in:
Andrew Grieve 2014-07-04 14:52:31 -04:00
parent e74baf188f
commit af77977fda
3 changed files with 46 additions and 47 deletions

View File

@ -21,8 +21,6 @@ package org.apache.cordova;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -41,7 +39,6 @@ public class ConfigXmlParser {
private CordovaPreferences prefs = new CordovaPreferences(); private CordovaPreferences prefs = new CordovaPreferences();
private Whitelist whitelist = new Whitelist(); private Whitelist whitelist = new Whitelist();
private ArrayList<PluginEntry> pluginEntries = new ArrayList<PluginEntry>(20); private ArrayList<PluginEntry> pluginEntries = new ArrayList<PluginEntry>(20);
private HashMap<String, List<String>> urlMap = new HashMap<String, List<String>>();
public Whitelist getWhitelist() { public Whitelist getWhitelist() {
return whitelist; return whitelist;
@ -59,10 +56,6 @@ public class ConfigXmlParser {
return launchUrl; return launchUrl;
} }
public HashMap<String, List<String>> getPluginUrlMap() {
return urlMap;
}
public void parse(Activity action) { public void parse(Activity action) {
// First checking the class namespace for config.xml // First checking the class namespace for config.xml
int id = action.getResources().getIdentifier("config", "xml", action.getClass().getPackage().getName()); int id = action.getResources().getIdentifier("config", "xml", action.getClass().getPackage().getName());
@ -82,16 +75,17 @@ public class ConfigXmlParser {
String service = "", pluginClass = "", paramType = ""; String service = "", pluginClass = "", paramType = "";
boolean onload = false; boolean onload = false;
boolean insideFeature = false; boolean insideFeature = false;
ArrayList<String> urlMap = null;
while (eventType != XmlResourceParser.END_DOCUMENT) { while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG) { if (eventType == XmlResourceParser.START_TAG) {
String strNode = xml.getName(); String strNode = xml.getName();
if (strNode.equals("url-filter")) { if (strNode.equals("url-filter")) {
Log.w(TAG, "Plugin " + service + " is using deprecated tag <url-filter>"); Log.w(TAG, "Plugin " + service + " is using deprecated tag <url-filter>");
if (urlMap.get(service) == null) { if (urlMap == null) {
urlMap.put(service, new ArrayList<String>(2)); urlMap = new ArrayList<String>(2);
} }
List<String> filters = urlMap.get(service); urlMap.add(xml.getAttributeValue(null, "value"));
filters.add(xml.getAttributeValue(null, "value"));
} else if (strNode.equals("feature")) { } else if (strNode.equals("feature")) {
//Check for supported feature sets aka. plugins (Accelerometer, Geolocation, etc) //Check for supported feature sets aka. plugins (Accelerometer, Geolocation, etc)
//Set the bit for reading params //Set the bit for reading params
@ -130,12 +124,13 @@ public class ConfigXmlParser {
{ {
String strNode = xml.getName(); String strNode = xml.getName();
if (strNode.equals("feature")) { if (strNode.equals("feature")) {
pluginEntries.add(new PluginEntry(service, pluginClass, onload)); pluginEntries.add(new PluginEntry(service, pluginClass, onload, urlMap));
service = ""; service = "";
pluginClass = ""; pluginClass = "";
insideFeature = false; insideFeature = false;
onload = false; onload = false;
urlMap = null;
} }
} }
try { try {

View File

@ -18,12 +18,12 @@
*/ */
package org.apache.cordova; package org.apache.cordova;
import java.util.List;
import org.apache.cordova.CordovaWebView; import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaPlugin;
//import android.content.Context;
//import android.webkit.WebView;
/** /**
* This class represents a service entry object. * This class represents a service entry object.
@ -52,30 +52,36 @@ public class PluginEntry {
*/ */
public boolean onload = false; public boolean onload = false;
private List<String> urlFilters;
/**
* @param service The name of the service
* @param plugin The plugin associated with this entry
*/
public PluginEntry(String service, CordovaPlugin plugin) {
this(service, plugin.getClass().getName(), true, null);
this.plugin = plugin;
}
/** /**
* Constructor
*
* @param service The name of the service * @param service The name of the service
* @param pluginClass The plugin class name * @param pluginClass The plugin class name
* @param onload Create plugin object when HTML page is loaded * @param onload Create plugin object when HTML page is loaded
*/ */
public PluginEntry(String service, String pluginClass, boolean onload) { public PluginEntry(String service, String pluginClass, boolean onload) {
this(service, pluginClass, onload, null);
}
public PluginEntry(String service, String pluginClass, boolean onload, List<String> urlFilters) {
this.service = service; this.service = service;
this.pluginClass = pluginClass; this.pluginClass = pluginClass;
this.onload = onload; this.onload = onload;
this.urlFilters = urlFilters;
} }
/** public List<String> getUrlFilters() {
* Alternate constructor return urlFilters;
*
* @param service The name of the service
* @param plugin The plugin associated with this entry
*/
public PluginEntry(String service, CordovaPlugin plugin) {
this.service = service;
this.plugin = plugin;
this.pluginClass = plugin.getClass().getName();
this.onload = false;
} }
/** /**
@ -89,8 +95,7 @@ public class PluginEntry {
return this.plugin; return this.plugin;
} }
try { try {
@SuppressWarnings("rawtypes") Class<?> c = getClassByName(this.pluginClass);
Class c = getClassByName(this.pluginClass);
if (isCordovaPlugin(c)) { if (isCordovaPlugin(c)) {
this.plugin = (CordovaPlugin) c.newInstance(); this.plugin = (CordovaPlugin) c.newInstance();
this.plugin.initialize(ctx, webView); this.plugin.initialize(ctx, webView);
@ -110,9 +115,8 @@ public class PluginEntry {
* @return a reference to the named class * @return a reference to the named class
* @throws ClassNotFoundException * @throws ClassNotFoundException
*/ */
@SuppressWarnings("rawtypes") private Class<?> getClassByName(final String clazz) throws ClassNotFoundException {
private Class getClassByName(final String clazz) throws ClassNotFoundException { Class<?> c = null;
Class c = null;
if ((clazz != null) && !("".equals(clazz))) { if ((clazz != null) && !("".equals(clazz))) {
c = Class.forName(clazz); c = Class.forName(clazz);
} }
@ -122,10 +126,9 @@ public class PluginEntry {
/** /**
* Returns whether the given class extends CordovaPlugin. * Returns whether the given class extends CordovaPlugin.
*/ */
@SuppressWarnings("rawtypes") private boolean isCordovaPlugin(Class<?> c) {
private boolean isCordovaPlugin(Class c) {
if (c != null) { if (c != null) {
return org.apache.cordova.CordovaPlugin.class.isAssignableFrom(c); return CordovaPlugin.class.isAssignableFrom(c);
} }
return false; return false;
} }

View File

@ -19,7 +19,6 @@
package org.apache.cordova; package org.apache.cordova;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import org.apache.cordova.CordovaWebView; import org.apache.cordova.CordovaWebView;
@ -86,12 +85,12 @@ public class PluginManager {
* Load plugins from res/xml/config.xml * Load plugins from res/xml/config.xml
*/ */
public void loadPlugins() { public void loadPlugins() {
ConfigXmlParser parser = new ConfigXmlParser(); ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(ctx.getActivity()); parser.parse(ctx.getActivity());
for (PluginEntry entry : parser.getPluginEntries()) { urlMap = new HashMap<String, List<String>>();
addService(entry); for (PluginEntry entry : parser.getPluginEntries()) {
} addService(entry);
urlMap = parser.getPluginUrlMap(); }
} }
/** /**
@ -206,6 +205,10 @@ public class PluginManager {
*/ */
public void addService(PluginEntry entry) { public void addService(PluginEntry entry) {
this.entries.put(entry.service, entry); this.entries.put(entry.service, entry);
List<String> urlFilters = entry.getUrlFilters();
if (urlFilters != null) {
urlMap.put(entry.service, urlFilters);
}
} }
/** /**
@ -311,11 +314,9 @@ public class PluginManager {
* Called when the app navigates or refreshes. * Called when the app navigates or refreshes.
*/ */
public void onReset() { public void onReset() {
Iterator<PluginEntry> it = this.entries.values().iterator(); for (PluginEntry entry : this.entries.values()) {
while (it.hasNext()) { if (entry.plugin != null) {
CordovaPlugin plugin = it.next().plugin; entry.plugin.onReset();
if (plugin != null) {
plugin.onReset();
} }
} }
} }