diff --git a/src/org/kde/kdeconnect/Device.java b/src/org/kde/kdeconnect/Device.java index a0b9392d..599e18da 100644 --- a/src/org/kde/kdeconnect/Device.java +++ b/src/org/kde/kdeconnect/Device.java @@ -554,7 +554,7 @@ public class Device implements BaseLink.PackageReceiver { unpair(); } - for (Plugin plugin : unpairedPackageListeners) { + for (Plugin plugin : plugins.values()) { try { plugin.onUnpairedDevicePackageReceived(np); } catch (Exception e) { @@ -725,10 +725,6 @@ public class Device implements BaseLink.PackageReceiver { Log.e("KDE/removePlugin","Exception calling onDestroy for plugin "+pluginKey); } - if (unpairedPackageListeners.contains(plugin)) { - unpairedPackageListeners.remove(plugin); - } - for (PluginsChangedListener listener : pluginsChangedListeners) { listener.onPluginsChanged(this); } @@ -760,9 +756,11 @@ public class Device implements BaseLink.PackageReceiver { for(String pluginKey : availablePlugins) { boolean enabled = false; - if (isPaired() && isReachable()) { + boolean listenToUnpaired = PluginFactory.getPluginInfo(context, pluginKey).listenToUnpaired(); + if ((isPaired() || listenToUnpaired) && isReachable()) { enabled = isPluginEnabled(pluginKey); } + if (enabled) { addPlugin(pluginKey); } else { @@ -795,11 +793,4 @@ public class Device implements BaseLink.PackageReceiver { pluginsChangedListeners.remove(listener); } - private final ArrayList unpairedPackageListeners = new ArrayList<>(); - - public void registerUnpairedPackageListener(Plugin p) { - Log.e("KDE/registerUnpairedPackageListener", p.getPluginKey() + " plugin registered to receive package from unpaired device"); - unpairedPackageListeners.add(p); - } - } diff --git a/src/org/kde/kdeconnect/Plugins/Plugin.java b/src/org/kde/kdeconnect/Plugins/Plugin.java index eff176f5..419aec7b 100644 --- a/src/org/kde/kdeconnect/Plugins/Plugin.java +++ b/src/org/kde/kdeconnect/Plugins/Plugin.java @@ -44,13 +44,21 @@ public abstract class Plugin { } /** - * To receive the network package from the unpaired device, Register your plugin - * using Device::registerUnpairedPackageListener and override this method. + * To receive the network package from the unpaired device, override + * listensToUnpairedDevices to return true and this method. */ public boolean onUnpairedDevicePackageReceived(NetworkPackage np) { return false; } + /** + * Returns whether this plugin should be loaded or not, to listen to NetworkPackages + * from the unpaired devices. By default, returns false. + */ + public boolean listensToUnpairedDevices() { + return false; + } + /** * Return the internal plugin name, that will be used as a * unique key to distinguish it. Use the class name as key. diff --git a/src/org/kde/kdeconnect/Plugins/PluginFactory.java b/src/org/kde/kdeconnect/Plugins/PluginFactory.java index e063d851..0226f61a 100644 --- a/src/org/kde/kdeconnect/Plugins/PluginFactory.java +++ b/src/org/kde/kdeconnect/Plugins/PluginFactory.java @@ -38,18 +38,20 @@ import org.kde.kdeconnect.Plugins.TelephonyPlugin.TelephonyPlugin; import java.util.Map; import java.util.Set; import java.util.TreeMap; +import java.lang.reflect.Method; public class PluginFactory { public static class PluginInfo { public PluginInfo(String displayName, String description, Drawable icon, - boolean enabledByDefault, boolean hasSettings) { + boolean enabledByDefault, boolean hasSettings, boolean listenToUnpaired) { this.displayName = displayName; this.description = description; this.icon = icon; this.enabledByDefault = enabledByDefault; this.hasSettings = hasSettings; + this.listenToUnpaired = listenToUnpaired; } public String getDisplayName() { @@ -70,12 +72,16 @@ public class PluginFactory { return enabledByDefault; } + public boolean listenToUnpaired() { + return listenToUnpaired; + } + private final String displayName; private final String description; private final Drawable icon; private final boolean enabledByDefault; private final boolean hasSettings; - + private final boolean listenToUnpaired; } private static final Map availablePlugins = new TreeMap<>(); @@ -101,7 +107,7 @@ public class PluginFactory { Plugin p = ((Plugin)availablePlugins.get(pluginKey).newInstance()); p.setContext(context, null); info = new PluginInfo(p.getDisplayName(), p.getDescription(), p.getIcon(), - p.isEnabledByDefault(), p.hasSettings()); + p.isEnabledByDefault(), p.hasSettings(), p.listensToUnpairedDevices()); availablePluginsInfo.put(pluginKey, info); //Cache it return info; } catch(Exception e) {