From 0eb3c6e9f5b767523aab435314de8c379d395780 Mon Sep 17 00:00:00 2001 From: Ashish Bansal Date: Thu, 27 Aug 2015 13:06:24 +0530 Subject: [PATCH] Load plugins which are registered to listen to unpaired devices Plugins which are registered to receive packages from unpaired devices should be loaded even if device is not paired, otherwise they won't be able to listen to NetworkPackages from the unpaired devices. REVIEW: 124906 --- src/org/kde/kdeconnect/Device.java | 17 ++++------------- src/org/kde/kdeconnect/Plugins/Plugin.java | 12 ++++++++++-- .../kde/kdeconnect/Plugins/PluginFactory.java | 12 +++++++++--- 3 files changed, 23 insertions(+), 18 deletions(-) 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) {