mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 03:53:09 +08:00
Move plugin instantiation and instance storing logic PluginEntry->PluginManager
Instantiation and storing of the instance should be owned privately by PluginManager, not exposed via an unprotected public API. That said, this refactoring does not make any breaking changes to the public API, except for removing the createPlugin call in PluginEntry, which should not be called by anyone other than PluginManager anyway.
This commit is contained in:
parent
a33cdc9c7b
commit
145b50a320
@ -20,11 +20,8 @@ package org.apache.cordova;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.cordova.CordovaWebView;
|
||||
import org.apache.cordova.CordovaInterface;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
|
||||
|
||||
/**
|
||||
* This class represents a service entry object.
|
||||
*/
|
||||
@ -33,34 +30,31 @@ public class PluginEntry {
|
||||
/**
|
||||
* The name of the service that this plugin implements
|
||||
*/
|
||||
public String service = "";
|
||||
public String service;
|
||||
|
||||
/**
|
||||
* The plugin class name that implements the service.
|
||||
*/
|
||||
public String pluginClass = "";
|
||||
public String pluginClass;
|
||||
|
||||
/**
|
||||
* The plugin object.
|
||||
* Plugin objects are only created when they are called from JavaScript. (see PluginManager.exec)
|
||||
* The exception is if the onload flag is set, then they are created when PluginManager is initialized.
|
||||
* The pre-instantiated plugin to use for this entry.
|
||||
*/
|
||||
public CordovaPlugin plugin = null;
|
||||
public CordovaPlugin plugin;
|
||||
|
||||
/**
|
||||
* Flag that indicates the plugin object should be created when PluginManager is initialized.
|
||||
*/
|
||||
public boolean onload = false;
|
||||
public boolean onload;
|
||||
|
||||
private List<String> urlFilters;
|
||||
|
||||
|
||||
/**
|
||||
* @param service The name of the service
|
||||
* @param plugin The plugin associated with this entry
|
||||
* Constructs with a CordovaPlugin already instantiated.
|
||||
*/
|
||||
public PluginEntry(String service, CordovaPlugin plugin) {
|
||||
this(service, plugin.getClass().getName(), true, null);
|
||||
this.plugin = plugin;
|
||||
this(service, plugin.getClass().getName(), true, plugin, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,67 +63,27 @@ public class PluginEntry {
|
||||
* @param onload Create plugin object when HTML page is loaded
|
||||
*/
|
||||
public PluginEntry(String service, String pluginClass, boolean onload) {
|
||||
this(service, pluginClass, onload, null);
|
||||
this(service, pluginClass, onload, null, null);
|
||||
}
|
||||
|
||||
|
||||
@Deprecated // urlFilters are going away
|
||||
public PluginEntry(String service, String pluginClass, boolean onload, List<String> urlFilters) {
|
||||
this.service = service;
|
||||
this.pluginClass = pluginClass;
|
||||
this.onload = onload;
|
||||
this.urlFilters = urlFilters;
|
||||
plugin = null;
|
||||
}
|
||||
|
||||
private PluginEntry(String service, String pluginClass, boolean onload, CordovaPlugin plugin, List<String> urlFilters) {
|
||||
this.service = service;
|
||||
this.pluginClass = pluginClass;
|
||||
this.onload = onload;
|
||||
this.urlFilters = urlFilters;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public List<String> getUrlFilters() {
|
||||
return urlFilters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create plugin object.
|
||||
* If plugin is already created, then just return it.
|
||||
*
|
||||
* @return The plugin object
|
||||
*/
|
||||
public CordovaPlugin createPlugin(CordovaWebView webView, CordovaInterface ctx) {
|
||||
if (this.plugin != null) {
|
||||
return this.plugin;
|
||||
}
|
||||
try {
|
||||
Class<?> c = getClassByName(this.pluginClass);
|
||||
if (isCordovaPlugin(c)) {
|
||||
this.plugin = (CordovaPlugin) c.newInstance();
|
||||
this.plugin.privateInitialize(ctx, webView, webView.getPreferences());
|
||||
return plugin;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("Error adding plugin " + this.pluginClass + ".");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the class.
|
||||
*
|
||||
* @param clazz
|
||||
* @return a reference to the named class
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
private Class<?> getClassByName(final String clazz) throws ClassNotFoundException {
|
||||
Class<?> c = null;
|
||||
if ((clazz != null) && !("".equals(clazz))) {
|
||||
c = Class.forName(clazz);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given class extends CordovaPlugin.
|
||||
*/
|
||||
private boolean isCordovaPlugin(Class<?> c) {
|
||||
if (c != null) {
|
||||
return CordovaPlugin.class.isAssignableFrom(c);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,8 @@ public class PluginManager {
|
||||
private static final int SLOW_EXEC_WARNING_THRESHOLD = Debug.isDebuggerConnected() ? 60 : 16;
|
||||
|
||||
// List of service entries
|
||||
private final HashMap<String, PluginEntry> entries = new HashMap<String, PluginEntry>();
|
||||
private final HashMap<String, CordovaPlugin> pluginMap = new HashMap<String, CordovaPlugin>();
|
||||
private final HashMap<String, PluginEntry> entryMap = new HashMap<String, PluginEntry>();
|
||||
|
||||
private final CordovaInterface ctx;
|
||||
private final CordovaWebView app;
|
||||
@ -73,8 +74,7 @@ public class PluginManager {
|
||||
public void setPluginEntries(List<PluginEntry> pluginEntries) {
|
||||
this.onPause(false);
|
||||
this.onDestroy();
|
||||
this.clearPluginObjects();
|
||||
entries.clear();
|
||||
pluginMap.clear();
|
||||
urlMap.clear();
|
||||
for (PluginEntry entry : pluginEntries) {
|
||||
addService(entry);
|
||||
@ -88,7 +88,7 @@ public class PluginManager {
|
||||
LOG.d(TAG, "init()");
|
||||
this.onPause(false);
|
||||
this.onDestroy();
|
||||
this.clearPluginObjects();
|
||||
pluginMap.clear();
|
||||
this.startupPlugins();
|
||||
}
|
||||
|
||||
@ -100,18 +100,16 @@ public class PluginManager {
|
||||
* Delete all plugin objects.
|
||||
*/
|
||||
public void clearPluginObjects() {
|
||||
for (PluginEntry entry : this.entries.values()) {
|
||||
entry.plugin = null;
|
||||
}
|
||||
pluginMap.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create plugins objects that have onload set.
|
||||
*/
|
||||
public void startupPlugins() {
|
||||
for (PluginEntry entry : this.entries.values()) {
|
||||
for (PluginEntry entry : entryMap.values()) {
|
||||
if (entry.onload) {
|
||||
entry.createPlugin(this.app, this.ctx);
|
||||
getPlugin(entry.service);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -177,15 +175,21 @@ public class PluginManager {
|
||||
* @return CordovaPlugin or null
|
||||
*/
|
||||
public CordovaPlugin getPlugin(String service) {
|
||||
PluginEntry entry = this.entries.get(service);
|
||||
if (entry == null) {
|
||||
return null;
|
||||
CordovaPlugin ret = pluginMap.get(service);
|
||||
if (ret == null) {
|
||||
PluginEntry pe = entryMap.get(service);
|
||||
if (pe == null) {
|
||||
return null;
|
||||
}
|
||||
if (pe.plugin != null) {
|
||||
ret = pe.plugin;
|
||||
} else {
|
||||
ret = instantiatePlugin(pe.pluginClass);
|
||||
}
|
||||
ret.privateInitialize(ctx, app, app.getPreferences());
|
||||
pluginMap.put(service, ret);
|
||||
}
|
||||
CordovaPlugin plugin = entry.plugin;
|
||||
if (plugin == null) {
|
||||
plugin = entry.createPlugin(this.app, this.ctx);
|
||||
}
|
||||
return plugin;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -207,11 +211,16 @@ public class PluginManager {
|
||||
* @param entry The plugin entry
|
||||
*/
|
||||
public void addService(PluginEntry entry) {
|
||||
this.entries.put(entry.service, entry);
|
||||
this.entryMap.put(entry.service, entry);
|
||||
List<String> urlFilters = entry.getUrlFilters();
|
||||
if (urlFilters != null) {
|
||||
urlMap.put(entry.service, urlFilters);
|
||||
}
|
||||
if (entry.plugin != null) {
|
||||
entry.plugin.privateInitialize(ctx, app, app.getPreferences());
|
||||
pluginMap.put(entry.service, entry.plugin);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -220,10 +229,8 @@ public class PluginManager {
|
||||
* @param multitasking Flag indicating if multitasking is turned on for app
|
||||
*/
|
||||
public void onPause(boolean multitasking) {
|
||||
for (PluginEntry entry : this.entries.values()) {
|
||||
if (entry.plugin != null) {
|
||||
entry.plugin.onPause(multitasking);
|
||||
}
|
||||
for (CordovaPlugin plugin : this.pluginMap.values()) {
|
||||
plugin.onPause(multitasking);
|
||||
}
|
||||
}
|
||||
|
||||
@ -233,10 +240,8 @@ public class PluginManager {
|
||||
* @param multitasking Flag indicating if multitasking is turned on for app
|
||||
*/
|
||||
public void onResume(boolean multitasking) {
|
||||
for (PluginEntry entry : this.entries.values()) {
|
||||
if (entry.plugin != null) {
|
||||
entry.plugin.onResume(multitasking);
|
||||
}
|
||||
for (CordovaPlugin plugin : this.pluginMap.values()) {
|
||||
plugin.onResume(multitasking);
|
||||
}
|
||||
}
|
||||
|
||||
@ -244,10 +249,8 @@ public class PluginManager {
|
||||
* The final call you receive before your activity is destroyed.
|
||||
*/
|
||||
public void onDestroy() {
|
||||
for (PluginEntry entry : this.entries.values()) {
|
||||
if (entry.plugin != null) {
|
||||
entry.plugin.onDestroy();
|
||||
}
|
||||
for (CordovaPlugin plugin : this.pluginMap.values()) {
|
||||
plugin.onDestroy();
|
||||
}
|
||||
}
|
||||
|
||||
@ -263,12 +266,10 @@ public class PluginManager {
|
||||
if (obj != null) {
|
||||
return obj;
|
||||
}
|
||||
for (PluginEntry entry : this.entries.values()) {
|
||||
if (entry.plugin != null) {
|
||||
obj = entry.plugin.onMessage(id, data);
|
||||
if (obj != null) {
|
||||
return obj;
|
||||
}
|
||||
for (CordovaPlugin plugin : this.pluginMap.values()) {
|
||||
obj = plugin.onMessage(id, data);
|
||||
if (obj != null) {
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@ -278,10 +279,8 @@ public class PluginManager {
|
||||
* Called when the activity receives a new intent.
|
||||
*/
|
||||
public void onNewIntent(Intent intent) {
|
||||
for (PluginEntry entry : this.entries.values()) {
|
||||
if (entry.plugin != null) {
|
||||
entry.plugin.onNewIntent(intent);
|
||||
}
|
||||
for (CordovaPlugin plugin : this.pluginMap.values()) {
|
||||
plugin.onNewIntent(intent);
|
||||
}
|
||||
}
|
||||
|
||||
@ -296,7 +295,7 @@ public class PluginManager {
|
||||
// Instead, plugins should not include <url-filter> and instead ensure
|
||||
// that they are loaded before this function is called (either by setting
|
||||
// the onload <param> or by making an exec() call to them)
|
||||
for (PluginEntry entry : this.entries.values()) {
|
||||
for (PluginEntry entry : this.entryMap.values()) {
|
||||
List<String> urlFilters = urlMap.get(entry.service);
|
||||
if (urlFilters != null) {
|
||||
for (String s : urlFilters) {
|
||||
@ -304,8 +303,9 @@ public class PluginManager {
|
||||
return getPlugin(entry.service).onOverrideUrlLoading(url);
|
||||
}
|
||||
}
|
||||
} else if (entry.plugin != null) {
|
||||
if (entry.plugin.onOverrideUrlLoading(url)) {
|
||||
} else {
|
||||
CordovaPlugin plugin = pluginMap.get(entry.service);
|
||||
if (plugin != null && plugin.onOverrideUrlLoading(url)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -317,22 +317,38 @@ public class PluginManager {
|
||||
* Called when the app navigates or refreshes.
|
||||
*/
|
||||
public void onReset() {
|
||||
for (PluginEntry entry : this.entries.values()) {
|
||||
if (entry.plugin != null) {
|
||||
entry.plugin.onReset();
|
||||
}
|
||||
for (CordovaPlugin plugin : this.pluginMap.values()) {
|
||||
plugin.onReset();
|
||||
}
|
||||
}
|
||||
|
||||
Uri remapUri(Uri uri) {
|
||||
for (PluginEntry entry : this.entries.values()) {
|
||||
if (entry.plugin != null) {
|
||||
Uri ret = entry.plugin.remapUri(uri);
|
||||
if (ret != null) {
|
||||
return ret;
|
||||
}
|
||||
for (CordovaPlugin plugin : this.pluginMap.values()) {
|
||||
Uri ret = plugin.remapUri(uri);
|
||||
if (ret != null) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a plugin based on class name.
|
||||
*/
|
||||
private CordovaPlugin instantiatePlugin(String className) {
|
||||
CordovaPlugin ret = null;
|
||||
try {
|
||||
Class<?> c = null;
|
||||
if ((className != null) && !("".equals(className))) {
|
||||
c = Class.forName(className);
|
||||
}
|
||||
if (c != null & CordovaPlugin.class.isAssignableFrom(c)) {
|
||||
ret = (CordovaPlugin) c.newInstance();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("Error adding plugin " + className + ".");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user