mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-31 14:15:14 +00:00
Load plugins in the same thread, so we know if they failed or not
This way we can update the capabilities accordingly.
This commit is contained in:
@@ -692,46 +692,40 @@ public class Device implements BaseLink.PackageReceiver {
|
|||||||
return plugin;
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void addPlugin(final String pluginKey) {
|
private synchronized boolean addPlugin(final String pluginKey) {
|
||||||
Plugin existing = plugins.get(pluginKey);
|
Plugin existing = plugins.get(pluginKey);
|
||||||
if (existing != null) {
|
if (existing != null) {
|
||||||
//Log.w("KDE/addPlugin","plugin already present:" + pluginKey);
|
//Log.w("KDE/addPlugin","plugin already present:" + pluginKey);
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Plugin plugin = PluginFactory.instantiatePluginForDevice(context, pluginKey, this);
|
final Plugin plugin = PluginFactory.instantiatePluginForDevice(context, pluginKey, this);
|
||||||
if (plugin == null) {
|
if (plugin == null) {
|
||||||
Log.e("KDE/addPlugin","could not instantiate plugin: "+pluginKey);
|
Log.e("KDE/addPlugin","could not instantiate plugin: "+pluginKey);
|
||||||
failedPlugins.put(pluginKey, null);
|
failedPlugins.put(pluginKey, null);
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
boolean success;
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
success = plugin.onCreate();
|
||||||
|
} catch (Exception e) {
|
||||||
|
success = false;
|
||||||
|
e.printStackTrace();
|
||||||
|
Log.e("KDE/addPlugin", "Exception loading plugin " + pluginKey);
|
||||||
|
}
|
||||||
|
|
||||||
boolean success;
|
if (success) {
|
||||||
try {
|
//Log.e("addPlugin","added " + pluginKey);
|
||||||
success = plugin.onCreate();
|
failedPlugins.remove(pluginKey);
|
||||||
} catch (Exception e) {
|
plugins.put(pluginKey, plugin);
|
||||||
success = false;
|
} else {
|
||||||
e.printStackTrace();
|
Log.e("KDE/addPlugin", "plugin failed to load " + pluginKey);
|
||||||
Log.e("KDE/addPlugin", "Exception loading plugin " + 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) {
|
private synchronized boolean removePlugin(String pluginKey) {
|
||||||
@@ -816,19 +810,21 @@ public class Device implements BaseLink.PackageReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pluginEnabled) {
|
if (pluginEnabled) {
|
||||||
addPlugin(pluginKey);
|
boolean success = addPlugin(pluginKey);
|
||||||
|
|
||||||
for (String packageType : incomingInterfaces) {
|
if (success) {
|
||||||
ArrayList<String> plugins = newPluginsByIncomingInterface.get(packageType);
|
for (String packageType : incomingInterfaces) {
|
||||||
if (plugins == null) plugins = new ArrayList<>();
|
ArrayList<String> plugins = newPluginsByIncomingInterface.get(packageType);
|
||||||
plugins.add(pluginKey);
|
if (plugins == null) plugins = new ArrayList<>();
|
||||||
newPluginsByIncomingInterface.put(packageType, plugins);
|
plugins.add(pluginKey);
|
||||||
}
|
newPluginsByIncomingInterface.put(packageType, plugins);
|
||||||
for (String packageType : outgoingInterfaces) {
|
}
|
||||||
ArrayList<String> plugins = newPluginsByOutgoingInterface.get(packageType);
|
for (String packageType : outgoingInterfaces) {
|
||||||
if (plugins == null) plugins = new ArrayList<>();
|
ArrayList<String> plugins = newPluginsByOutgoingInterface.get(packageType);
|
||||||
plugins.add(pluginKey);
|
if (plugins == null) plugins = new ArrayList<>();
|
||||||
newPluginsByOutgoingInterface.put(packageType, plugins);
|
plugins.add(pluginKey);
|
||||||
|
newPluginsByOutgoingInterface.put(packageType, plugins);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
removePlugin(pluginKey);
|
removePlugin(pluginKey);
|
||||||
|
@@ -25,6 +25,8 @@ import android.content.ClipData;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.ClipboardManager;
|
import android.content.ClipboardManager;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
|
|
||||||
import org.kde.kdeconnect.Device;
|
import org.kde.kdeconnect.Device;
|
||||||
import org.kde.kdeconnect.NetworkPackage;
|
import org.kde.kdeconnect.NetworkPackage;
|
||||||
@@ -45,28 +47,33 @@ public class ClipboardListener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
|
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
||||||
listener = new ClipboardManager.OnPrimaryClipChangedListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onPrimaryClipChanged() {
|
public void run() {
|
||||||
try {
|
cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
|
listener = new ClipboardManager.OnPrimaryClipChangedListener() {
|
||||||
|
@Override
|
||||||
|
public void onPrimaryClipChanged() {
|
||||||
|
try {
|
||||||
|
|
||||||
ClipData.Item item = cm.getPrimaryClip().getItemAt(0);
|
ClipData.Item item = cm.getPrimaryClip().getItemAt(0);
|
||||||
String content = item.coerceToText(context).toString();
|
String content = item.coerceToText(context).toString();
|
||||||
|
|
||||||
if (!content.equals(currentContent)) {
|
if (!content.equals(currentContent)) {
|
||||||
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_CLIPBOARD);
|
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_CLIPBOARD);
|
||||||
np.set("content", content);
|
np.set("content", content);
|
||||||
device.sendPackage(np);
|
device.sendPackage(np);
|
||||||
currentContent = content;
|
currentContent = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
//Probably clipboard was not text
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
} catch(Exception e) {
|
cm.addPrimaryClipChangedListener(listener);
|
||||||
//Probably clipboard was not text
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
cm.addPrimaryClipChangedListener(listener);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
|
Reference in New Issue
Block a user