2014-11-16 23:14:06 -08:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Albert Vaca Cintora <albertvaka@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-09-05 01:37:59 +02:00
|
|
|
package org.kde.kdeconnect.Plugins;
|
2013-08-16 10:31:01 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
2013-08-19 19:57:29 +02:00
|
|
|
import android.graphics.drawable.Drawable;
|
2013-08-16 10:31:01 +02:00
|
|
|
import android.util.Log;
|
|
|
|
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.Device;
|
|
|
|
import org.kde.kdeconnect.Plugins.BatteryPlugin.BatteryPlugin;
|
2016-06-09 13:42:15 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.ClibpoardPlugin.ClipboardPlugin;
|
2015-09-11 16:54:43 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.FindMyPhonePlugin.FindMyPhonePlugin;
|
2014-06-26 17:37:32 +00:00
|
|
|
import org.kde.kdeconnect.Plugins.MousePadPlugin.MousePadPlugin;
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.MprisPlugin.MprisPlugin;
|
|
|
|
import org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationsPlugin;
|
|
|
|
import org.kde.kdeconnect.Plugins.PingPlugin.PingPlugin;
|
2016-06-26 17:08:47 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.ReceiveNotificationsPlugin.ReceiveNotificationsPlugin;
|
2016-06-09 13:42:15 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.RunCommandPlugin.RunCommandPlugin;
|
|
|
|
import org.kde.kdeconnect.Plugins.SftpPlugin.SftpPlugin;
|
2014-06-27 14:44:40 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.SharePlugin.SharePlugin;
|
2016-08-26 19:08:03 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.TelepathyPlugin.TelepathyPlugin;
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.TelephonyPlugin.TelephonyPlugin;
|
2013-08-16 10:31:01 +02:00
|
|
|
|
2015-09-08 14:54:04 -07:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
2013-08-16 10:31:01 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
|
|
|
public class PluginFactory {
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
public static class PluginInfo {
|
|
|
|
|
2015-06-06 00:38:51 -07:00
|
|
|
public PluginInfo(String displayName, String description, Drawable icon,
|
2015-09-08 14:54:04 -07:00
|
|
|
boolean enabledByDefault, boolean hasSettings, boolean listenToUnpaired,
|
|
|
|
String[] supportedPackageTypes, String[] outgoingPackageTypes) {
|
2013-08-19 19:57:29 +02:00
|
|
|
this.displayName = displayName;
|
|
|
|
this.description = description;
|
|
|
|
this.icon = icon;
|
|
|
|
this.enabledByDefault = enabledByDefault;
|
2014-09-16 15:45:31 +02:00
|
|
|
this.hasSettings = hasSettings;
|
2015-08-27 13:06:24 +05:30
|
|
|
this.listenToUnpaired = listenToUnpaired;
|
2015-09-08 14:54:04 -07:00
|
|
|
HashSet<String> incoming = new HashSet<>();
|
|
|
|
if (supportedPackageTypes != null) Collections.addAll(incoming, supportedPackageTypes);
|
|
|
|
this.supportedPackageTypes = Collections.unmodifiableSet(incoming);
|
|
|
|
HashSet<String> outgoing = new HashSet<>();
|
|
|
|
if (outgoingPackageTypes != null) Collections.addAll(outgoing, outgoingPackageTypes);
|
|
|
|
this.outgoingPackageTypes = Collections.unmodifiableSet(outgoing);
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getDisplayName() {
|
|
|
|
return displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getDescription() {
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Drawable getIcon() {
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
2014-09-16 15:45:31 +02:00
|
|
|
public boolean hasSettings() { return hasSettings; }
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
public boolean isEnabledByDefault() {
|
|
|
|
return enabledByDefault;
|
|
|
|
}
|
|
|
|
|
2015-08-27 13:06:24 +05:30
|
|
|
public boolean listenToUnpaired() {
|
|
|
|
return listenToUnpaired;
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:54:04 -07:00
|
|
|
public Set<String> getOutgoingPackageTypes() {
|
|
|
|
return outgoingPackageTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Set<String> getSupportedPackageTypes() {
|
|
|
|
return supportedPackageTypes;
|
|
|
|
}
|
|
|
|
|
2014-03-29 01:47:15 +01:00
|
|
|
private final String displayName;
|
|
|
|
private final String description;
|
2013-08-19 19:57:29 +02:00
|
|
|
private final Drawable icon;
|
2014-03-29 01:47:15 +01:00
|
|
|
private final boolean enabledByDefault;
|
2014-09-16 15:45:31 +02:00
|
|
|
private final boolean hasSettings;
|
2015-08-27 13:06:24 +05:30
|
|
|
private final boolean listenToUnpaired;
|
2015-09-08 14:54:04 -07:00
|
|
|
private final Set<String> supportedPackageTypes;
|
|
|
|
private final Set<String> outgoingPackageTypes;
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
|
2015-08-10 00:26:58 -07:00
|
|
|
private static final Map<String, Class> availablePlugins = new TreeMap<>();
|
2016-07-06 17:45:01 +02:00
|
|
|
private static final Map<String, PluginInfo> pluginInfoCache = new TreeMap<>();
|
2013-08-19 19:57:29 +02:00
|
|
|
|
2013-08-16 10:31:01 +02:00
|
|
|
static {
|
2013-08-19 19:57:29 +02:00
|
|
|
PluginFactory.registerPlugin(TelephonyPlugin.class);
|
|
|
|
PluginFactory.registerPlugin(PingPlugin.class);
|
|
|
|
PluginFactory.registerPlugin(MprisPlugin.class);
|
|
|
|
PluginFactory.registerPlugin(ClipboardPlugin.class);
|
|
|
|
PluginFactory.registerPlugin(BatteryPlugin.class);
|
2016-02-17 04:06:35 -08:00
|
|
|
PluginFactory.registerPlugin(SftpPlugin.class);
|
2013-08-19 19:57:29 +02:00
|
|
|
PluginFactory.registerPlugin(NotificationsPlugin.class);
|
2016-06-26 17:08:47 +02:00
|
|
|
PluginFactory.registerPlugin(ReceiveNotificationsPlugin.class);
|
2014-06-26 17:37:32 +00:00
|
|
|
PluginFactory.registerPlugin(MousePadPlugin.class);
|
2014-06-27 14:44:40 +02:00
|
|
|
PluginFactory.registerPlugin(SharePlugin.class);
|
2016-08-26 19:08:03 +02:00
|
|
|
PluginFactory.registerPlugin(TelepathyPlugin.class);
|
2015-09-11 16:54:43 +02:00
|
|
|
PluginFactory.registerPlugin(FindMyPhonePlugin.class);
|
2015-09-12 12:28:27 +02:00
|
|
|
PluginFactory.registerPlugin(RunCommandPlugin.class);
|
2013-08-16 10:31:01 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 00:38:51 -07:00
|
|
|
public static PluginInfo getPluginInfo(Context context, String pluginKey) {
|
2015-09-08 14:54:04 -07:00
|
|
|
|
2016-07-06 17:45:01 +02:00
|
|
|
PluginInfo info = pluginInfoCache.get(pluginKey); //Is it cached?
|
2015-09-08 14:54:04 -07:00
|
|
|
if (info != null) {
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
try {
|
2015-06-06 00:38:51 -07:00
|
|
|
Plugin p = ((Plugin)availablePlugins.get(pluginKey).newInstance());
|
2013-08-19 19:57:29 +02:00
|
|
|
p.setContext(context, null);
|
2015-06-06 00:38:51 -07:00
|
|
|
info = new PluginInfo(p.getDisplayName(), p.getDescription(), p.getIcon(),
|
2015-09-08 14:54:04 -07:00
|
|
|
p.isEnabledByDefault(), p.hasSettings(), p.listensToUnpairedDevices(),
|
|
|
|
p.getSupportedPackageTypes(), p.getOutgoingPackageTypes());
|
2016-07-06 17:45:01 +02:00
|
|
|
pluginInfoCache.put(pluginKey, info); //Cache it
|
2013-08-19 19:57:29 +02:00
|
|
|
return info;
|
|
|
|
} catch(Exception e) {
|
|
|
|
Log.e("PluginFactory","getPluginInfo exception");
|
2015-01-22 21:30:32 -08:00
|
|
|
e.printStackTrace();
|
2013-08-19 19:57:29 +02:00
|
|
|
return null;
|
|
|
|
}
|
2015-09-08 14:54:04 -07:00
|
|
|
|
2013-08-16 10:31:01 +02:00
|
|
|
}
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
public static Set<String> getAvailablePlugins() {
|
|
|
|
return availablePlugins.keySet();
|
2013-08-16 10:31:01 +02:00
|
|
|
}
|
|
|
|
|
2015-06-06 00:38:51 -07:00
|
|
|
public static Plugin instantiatePluginForDevice(Context context, String pluginKey, Device device) {
|
|
|
|
Class c = availablePlugins.get(pluginKey);
|
2013-08-16 10:31:01 +02:00
|
|
|
if (c == null) {
|
2015-06-06 00:38:51 -07:00
|
|
|
Log.e("PluginFactory", "Plugin not found: "+pluginKey);
|
2013-08-16 10:31:01 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Plugin plugin = (Plugin)c.newInstance();
|
|
|
|
plugin.setContext(context, device);
|
|
|
|
return plugin;
|
|
|
|
} catch(Exception e) {
|
2015-06-06 00:38:51 -07:00
|
|
|
Log.e("PluginFactory", "Could not instantiate plugin: "+pluginKey);
|
2015-01-22 21:30:32 -08:00
|
|
|
e.printStackTrace();
|
2013-08-16 10:31:01 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-26 17:37:32 +00:00
|
|
|
public static void registerPlugin(Class<? extends Plugin> pluginClass) {
|
2013-08-19 19:57:29 +02:00
|
|
|
try {
|
2015-06-06 00:38:51 -07:00
|
|
|
String pluginKey = Plugin.getPluginKey(pluginClass);
|
|
|
|
availablePlugins.put(pluginKey, pluginClass);
|
2013-08-19 19:57:29 +02:00
|
|
|
} catch(Exception e) {
|
|
|
|
Log.e("PluginFactory","addPlugin exception");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 17:45:01 +02:00
|
|
|
|
|
|
|
public static Set<String> getIncomingCapabilities(Context context) {
|
|
|
|
HashSet<String> capabilities = new HashSet<>();
|
|
|
|
for (String pluginId : availablePlugins.keySet()) {
|
|
|
|
PluginInfo plugin = getPluginInfo(context, pluginId);
|
|
|
|
capabilities.addAll(plugin.getSupportedPackageTypes());
|
|
|
|
}
|
|
|
|
|
|
|
|
return capabilities;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Set<String> getOutgoingCapabilities(Context context) {
|
|
|
|
HashSet<String> capabilities = new HashSet<>();
|
|
|
|
for (String pluginId : availablePlugins.keySet()) {
|
|
|
|
PluginInfo plugin = getPluginInfo(context, pluginId);
|
|
|
|
capabilities.addAll(plugin.getOutgoingPackageTypes());
|
|
|
|
}
|
|
|
|
return capabilities;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Set<String> pluginsForCapabilities(Context context, Set<String> incoming, Set<String> outgoing) {
|
|
|
|
HashSet<String> plugins = new HashSet<>();
|
|
|
|
for (String pluginId : availablePlugins.keySet()) {
|
|
|
|
PluginInfo plugin = getPluginInfo(context, pluginId);
|
|
|
|
//Check incoming against outgoing
|
|
|
|
if (Collections.disjoint(outgoing, plugin.getSupportedPackageTypes())
|
|
|
|
&& Collections.disjoint(incoming, plugin.getOutgoingPackageTypes())) {
|
|
|
|
Log.i("PluginFactory", "Won't load " + pluginId + " because of unmatched capabilities");
|
|
|
|
continue; //No capabilities in common, do not load this plugin
|
|
|
|
}
|
|
|
|
plugins.add(pluginId);
|
|
|
|
}
|
|
|
|
return plugins;
|
|
|
|
}
|
|
|
|
|
2013-08-16 10:31:01 +02:00
|
|
|
}
|