From 9c29cbf5943651a704a924fa5316f6044797f84f Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Sat, 12 Sep 2015 07:29:45 -0700 Subject: [PATCH] Load plugins in the same thread, so we know if they failed or not This way we can update the capabilities accordingly. --- src/org/kde/kdeconnect/Device.java | 74 +++++++++---------- .../ClibpoardPlugin/ClipboardListener.java | 41 +++++----- 2 files changed, 59 insertions(+), 56 deletions(-) diff --git a/src/org/kde/kdeconnect/Device.java b/src/org/kde/kdeconnect/Device.java index 572d8c6f..503f7154 100644 --- a/src/org/kde/kdeconnect/Device.java +++ b/src/org/kde/kdeconnect/Device.java @@ -692,46 +692,40 @@ public class Device implements BaseLink.PackageReceiver { return plugin; } - private synchronized void addPlugin(final String pluginKey) { + private synchronized boolean addPlugin(final String pluginKey) { Plugin existing = plugins.get(pluginKey); if (existing != null) { //Log.w("KDE/addPlugin","plugin already present:" + pluginKey); - return; + return false; } final Plugin plugin = PluginFactory.instantiatePluginForDevice(context, pluginKey, this); if (plugin == null) { Log.e("KDE/addPlugin","could not instantiate plugin: "+pluginKey); failedPlugins.put(pluginKey, null); - return; + return false; } - new Handler(Looper.getMainLooper()).post(new Runnable() { - @Override - public void run() { + boolean success; + try { + success = plugin.onCreate(); + } catch (Exception e) { + success = false; + e.printStackTrace(); + Log.e("KDE/addPlugin", "Exception loading plugin " + pluginKey); + } - boolean success; - try { - success = plugin.onCreate(); - } catch (Exception e) { - success = false; - e.printStackTrace(); - Log.e("KDE/addPlugin", "Exception loading plugin " + pluginKey); - } - - if (success) { - //Log.e("addPlugin","added " + pluginKey); - failedPlugins.remove(pluginKey); - plugins.put(pluginKey, plugin); - } else { - Log.e("KDE/addPlugin", "plugin failed to load " + pluginKey); - plugins.remove(pluginKey); - failedPlugins.put(pluginKey, plugin); - } - - } - }); + if (success) { + //Log.e("addPlugin","added " + pluginKey); + failedPlugins.remove(pluginKey); + plugins.put(pluginKey, plugin); + } else { + Log.e("KDE/addPlugin", "plugin failed to load " + pluginKey); + plugins.remove(pluginKey); + failedPlugins.put(pluginKey, plugin); + } + return success; } private synchronized boolean removePlugin(String pluginKey) { @@ -816,19 +810,21 @@ public class Device implements BaseLink.PackageReceiver { } if (pluginEnabled) { - addPlugin(pluginKey); + boolean success = addPlugin(pluginKey); - for (String packageType : incomingInterfaces) { - ArrayList plugins = newPluginsByIncomingInterface.get(packageType); - if (plugins == null) plugins = new ArrayList<>(); - plugins.add(pluginKey); - newPluginsByIncomingInterface.put(packageType, plugins); - } - for (String packageType : outgoingInterfaces) { - ArrayList plugins = newPluginsByOutgoingInterface.get(packageType); - if (plugins == null) plugins = new ArrayList<>(); - plugins.add(pluginKey); - newPluginsByOutgoingInterface.put(packageType, plugins); + if (success) { + for (String packageType : incomingInterfaces) { + ArrayList plugins = newPluginsByIncomingInterface.get(packageType); + if (plugins == null) plugins = new ArrayList<>(); + plugins.add(pluginKey); + newPluginsByIncomingInterface.put(packageType, plugins); + } + for (String packageType : outgoingInterfaces) { + ArrayList plugins = newPluginsByOutgoingInterface.get(packageType); + if (plugins == null) plugins = new ArrayList<>(); + plugins.add(pluginKey); + newPluginsByOutgoingInterface.put(packageType, plugins); + } } } else { removePlugin(pluginKey); diff --git a/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java b/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java index 87069c1a..1ee625ce 100644 --- a/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java +++ b/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java @@ -25,6 +25,8 @@ import android.content.ClipData; import android.content.Context; import android.content.ClipboardManager; import android.os.Build; +import android.os.Handler; +import android.os.Looper; import org.kde.kdeconnect.Device; import org.kde.kdeconnect.NetworkPackage; @@ -45,28 +47,33 @@ public class ClipboardListener { return; } - cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE); - listener = new ClipboardManager.OnPrimaryClipChangedListener() { + new Handler(Looper.getMainLooper()).post(new Runnable() { @Override - public void onPrimaryClipChanged() { - try { + public void run() { + cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); + listener = new ClipboardManager.OnPrimaryClipChangedListener() { + @Override + public void onPrimaryClipChanged() { + try { - ClipData.Item item = cm.getPrimaryClip().getItemAt(0); - String content = item.coerceToText(context).toString(); + ClipData.Item item = cm.getPrimaryClip().getItemAt(0); + String content = item.coerceToText(context).toString(); - if (!content.equals(currentContent)) { - NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_CLIPBOARD); - np.set("content", content); - device.sendPackage(np); - currentContent = content; + if (!content.equals(currentContent)) { + NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_CLIPBOARD); + np.set("content", content); + device.sendPackage(np); + currentContent = content; + } + + } catch (Exception e) { + //Probably clipboard was not text + } } - - } catch(Exception e) { - //Probably clipboard was not text - } + }; + cm.addPrimaryClipChangedListener(listener); } - }; - cm.addPrimaryClipChangedListener(listener); + }); } public void stop() {