Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): Plugin Instance Support #6073

Merged
merged 4 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 77 additions & 25 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public class Bridge {

private final List<Class<? extends Plugin>> initialPlugins;

private final List<Plugin> pluginInstances;

// A map of Plugin Id's to PluginHandle's
private Map<String, PluginHandle> plugins = new HashMap<>();

Expand Down Expand Up @@ -162,7 +164,7 @@ public Bridge(
CordovaPreferences preferences,
CapConfig config
) {
this(context, null, null, webView, initialPlugins, cordovaInterface, pluginManager, preferences, config);
this(context, null, null, webView, initialPlugins, new ArrayList<>(), cordovaInterface, pluginManager, preferences, config);
}

private Bridge(
Expand All @@ -171,6 +173,7 @@ private Bridge(
Fragment fragment,
WebView webView,
List<Class<? extends Plugin>> initialPlugins,
List<Plugin> pluginInstances,
MockCordovaInterfaceImpl cordovaInterface,
PluginManager pluginManager,
CordovaPreferences preferences,
Expand All @@ -183,6 +186,7 @@ private Bridge(
this.webView = webView;
this.webViewClient = new BridgeWebViewClient(this);
this.initialPlugins = initialPlugins;
this.pluginInstances = pluginInstances;
this.cordovaInterface = cordovaInterface;
this.preferences = preferences;

Expand Down Expand Up @@ -578,6 +582,10 @@ private void registerAllPlugins() {
for (Class<? extends Plugin> pluginClass : this.initialPlugins) {
this.registerPlugin(pluginClass);
}

for (Plugin plugin : pluginInstances) {
registerPluginInstance(plugin);
}
}

/**
Expand All @@ -590,6 +598,12 @@ public void registerPlugins(Class<? extends Plugin>[] pluginClasses) {
}
}

public void registerPluginInstances(Plugin[] pluginInstances) {
for (Plugin plugin : pluginInstances) {
this.registerPluginInstance(plugin);
}
}

@SuppressWarnings("deprecation")
private String getLegacyPluginName(Class<? extends Plugin> pluginClass) {
NativePlugin legacyPluginAnnotation = pluginClass.getAnnotation(NativePlugin.class);
Expand All @@ -606,39 +620,65 @@ private String getLegacyPluginName(Class<? extends Plugin> pluginClass) {
* @param pluginClass a class inheriting from Plugin
*/
public void registerPlugin(Class<? extends Plugin> pluginClass) {
String pluginName;
String pluginId = pluginId(pluginClass);
if (pluginId == null) return;

CapacitorPlugin pluginAnnotation = pluginClass.getAnnotation(CapacitorPlugin.class);
if (pluginAnnotation == null) {
pluginName = this.getLegacyPluginName(pluginClass);
if (pluginName == null) {
return;
}
} else {
pluginName = pluginAnnotation.name();
try {
this.plugins.put(pluginId, new PluginHandle(this, pluginClass));
} catch (InvalidPluginException ex) {
logInvalidPluginException(pluginClass);
} catch (PluginLoadException ex) {
logPluginLoadException(pluginClass, ex);
}
}

public void registerPluginInstance(Plugin plugin) {
Class<? extends Plugin> clazz = plugin.getClass();
String pluginId = pluginId(clazz);
if (pluginId == null) return;

try {
this.plugins.put(pluginId, new PluginHandle(this, plugin));
} catch (InvalidPluginException ex) {
logInvalidPluginException(clazz);
}
}

String pluginId = pluginClass.getSimpleName();
private String pluginId(Class<? extends Plugin> clazz) {
String pluginName = pluginName(clazz);
String pluginId = clazz.getSimpleName();
if (pluginName == null) return null;

// Use the supplied name as the id if available
if (!pluginName.equals("")) {
pluginId = pluginName;
}
Logger.debug("Registering plugin instance: " + pluginId);
return pluginId;
}

Logger.debug("Registering plugin: " + pluginId);

try {
this.plugins.put(pluginId, new PluginHandle(this, pluginClass));
} catch (InvalidPluginException ex) {
Logger.error(
"NativePlugin " +
pluginClass.getName() +
" is invalid. Ensure the @CapacitorPlugin annotation exists on the plugin class and" +
" the class extends Plugin"
);
} catch (PluginLoadException ex) {
Logger.error("NativePlugin " + pluginClass.getName() + " failed to load", ex);
private String pluginName(Class<? extends Plugin> clazz) {
String pluginName;
CapacitorPlugin pluginAnnotation = clazz.getAnnotation(CapacitorPlugin.class);
if (pluginAnnotation == null) {
pluginName = this.getLegacyPluginName(clazz);
} else {
pluginName = pluginAnnotation.name();
}

return pluginName;
}

private void logInvalidPluginException(Class<? extends Plugin> clazz) {
Logger.error(
"NativePlugin " +
clazz.getName() +
" is invalid. Ensure the @CapacitorPlugin annotation exists on the plugin class and" +
" the class extends Plugin"
);
}

private void logPluginLoadException(Class<? extends Plugin> clazz, Exception ex) {
Logger.error("NativePlugin " + clazz.getName() + " failed to load", ex);
}

public PluginHandle getPlugin(String pluginId) {
Expand Down Expand Up @@ -1360,6 +1400,7 @@ public static class Builder {
private Bundle instanceState = null;
private CapConfig config = null;
private List<Class<? extends Plugin>> plugins = new ArrayList<>();
private List<Plugin> pluginInstances = new ArrayList<>();
private AppCompatActivity activity;
private Fragment fragment;
private RouteProcessor routeProcessor;
Expand Down Expand Up @@ -1403,6 +1444,16 @@ public Builder addPlugins(List<Class<? extends Plugin>> plugins) {
return this;
}

public Builder addPluginInstance(Plugin plugin) {
this.pluginInstances.add(plugin);
return this;
}

public Builder addPluginInstances(List<Plugin> plugins) {
this.pluginInstances.addAll(plugins);
return this;
}

public Builder addWebViewListener(WebViewListener webViewListener) {
webViewListeners.add(webViewListener);
return this;
Expand Down Expand Up @@ -1452,6 +1503,7 @@ public Bridge create() {
fragment,
webView,
plugins,
pluginInstances,
cordovaInterface,
pluginManager,
preferences,
Expand Down
31 changes: 22 additions & 9 deletions android/capacitor/src/main/java/com/getcapacitor/PluginHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class PluginHandle {
private final Bridge bridge;
private final Class<? extends Plugin> pluginClass;

private Map<String, PluginMethodHandle> pluginMethods = new HashMap<>();
private final Map<String, PluginMethodHandle> pluginMethods = new HashMap<>();

private final String pluginId;

Expand All @@ -28,9 +28,9 @@ public class PluginHandle {
private Plugin instance;

@SuppressWarnings("deprecation")
public PluginHandle(Bridge bridge, Class<? extends Plugin> pluginClass) throws InvalidPluginException, PluginLoadException {
private PluginHandle(Class<? extends Plugin> clazz, Bridge bridge) throws InvalidPluginException {
this.bridge = bridge;
this.pluginClass = pluginClass;
this.pluginClass = clazz;

CapacitorPlugin pluginAnnotation = pluginClass.getAnnotation(CapacitorPlugin.class);
if (pluginAnnotation == null) {
Expand All @@ -57,11 +57,19 @@ public PluginHandle(Bridge bridge, Class<? extends Plugin> pluginClass) throws I
this.pluginAnnotation = pluginAnnotation;
}

this.indexMethods(pluginClass);
this.indexMethods(clazz);
}

public PluginHandle(Bridge bridge, Class<? extends Plugin> pluginClass) throws InvalidPluginException, PluginLoadException {
this(pluginClass, bridge);
this.load();
}

public PluginHandle(Bridge bridge, Plugin plugin) throws InvalidPluginException {
this(plugin.getClass(), bridge);
this.loadInstance(plugin);
}

public Class<? extends Plugin> getPluginClass() {
return pluginClass;
}
Expand Down Expand Up @@ -94,16 +102,21 @@ public Plugin load() throws PluginLoadException {

try {
this.instance = this.pluginClass.newInstance();
this.instance.setPluginHandle(this);
this.instance.setBridge(this.bridge);
this.instance.load();
this.instance.initializeActivityLaunchers();
return this.instance;
return this.loadInstance(instance);
} catch (InstantiationException | IllegalAccessException ex) {
throw new PluginLoadException("Unable to load plugin instance. Ensure plugin is publicly accessible");
}
}

public Plugin loadInstance(Plugin plugin) {
this.instance = plugin;
this.instance.setPluginHandle(this);
this.instance.setBridge(this.bridge);
this.instance.load();
this.instance.initializeActivityLaunchers();
return this.instance;
}

/**
* Call a method on a plugin.
* @param methodName the name of the method to call
Expand Down