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

View File

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

View File

@ -19,7 +19,6 @@
package org.apache.cordova;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.cordova.CordovaWebView;
@ -86,12 +85,12 @@ public class PluginManager {
* Load plugins from res/xml/config.xml
*/
public void loadPlugins() {
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(ctx.getActivity());
for (PluginEntry entry : parser.getPluginEntries()) {
addService(entry);
}
urlMap = parser.getPluginUrlMap();
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(ctx.getActivity());
urlMap = new HashMap<String, List<String>>();
for (PluginEntry entry : parser.getPluginEntries()) {
addService(entry);
}
}
/**
@ -206,6 +205,10 @@ public class PluginManager {
*/
public void addService(PluginEntry 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.
*/
public void onReset() {
Iterator<PluginEntry> it = this.entries.values().iterator();
while (it.hasNext()) {
CordovaPlugin plugin = it.next().plugin;
if (plugin != null) {
plugin.onReset();
for (PluginEntry entry : this.entries.values()) {
if (entry.plugin != null) {
entry.plugin.onReset();
}
}
}