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;
|
2014-06-26 17:37:32 +00:00
|
|
|
import org.kde.kdeconnect.Plugins.MousePadPlugin.MousePadPlugin;
|
2014-01-06 19:30:55 +04:00
|
|
|
import org.kde.kdeconnect.Plugins.SftpPlugin.SftpPlugin;
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.ClibpoardPlugin.ClipboardPlugin;
|
|
|
|
import org.kde.kdeconnect.Plugins.MprisPlugin.MprisPlugin;
|
|
|
|
import org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationsPlugin;
|
|
|
|
import org.kde.kdeconnect.Plugins.PingPlugin.PingPlugin;
|
2014-06-27 14:44:40 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.SharePlugin.SharePlugin;
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.Plugins.TelephonyPlugin.TelephonyPlugin;
|
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 {
|
|
|
|
|
2014-09-16 15:45:31 +02:00
|
|
|
public PluginInfo(String pluginName, String displayName, String description, Drawable icon,
|
|
|
|
boolean enabledByDefault, boolean hasSettings) {
|
2013-08-19 19:57:29 +02:00
|
|
|
this.pluginName = pluginName;
|
|
|
|
this.displayName = displayName;
|
|
|
|
this.description = description;
|
|
|
|
this.icon = icon;
|
|
|
|
this.enabledByDefault = enabledByDefault;
|
2014-09-16 15:45:31 +02:00
|
|
|
this.hasSettings = hasSettings;
|
2013-08-19 19:57:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getPluginName() {
|
|
|
|
return pluginName;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-29 01:47:15 +01:00
|
|
|
private final String pluginName;
|
|
|
|
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;
|
2013-08-19 19:57:29 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-16 10:31:01 +02:00
|
|
|
private static final Map<String, Class> availablePlugins = new TreeMap<String, Class>();
|
2013-08-19 19:57:29 +02:00
|
|
|
private static final Map<String, PluginInfo> availablePluginsInfo = new TreeMap<String, PluginInfo>();
|
|
|
|
|
2013-08-16 10:31:01 +02:00
|
|
|
static {
|
2014-09-17 10:36:45 +02:00
|
|
|
//TODO: Use reflection to find all subclasses of Plugin, instead of adding them manually
|
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);
|
2014-01-06 19:30:55 +04:00
|
|
|
PluginFactory.registerPlugin(SftpPlugin.class);
|
2013-08-19 19:57:29 +02:00
|
|
|
PluginFactory.registerPlugin(NotificationsPlugin.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);
|
2013-08-16 10:31:01 +02:00
|
|
|
}
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
public static PluginInfo getPluginInfo(Context context, String pluginName) {
|
|
|
|
PluginInfo info = availablePluginsInfo.get(pluginName); //Is it cached?
|
|
|
|
if (info != null) return info;
|
|
|
|
try {
|
|
|
|
Plugin p = ((Plugin)availablePlugins.get(pluginName).newInstance());
|
|
|
|
p.setContext(context, null);
|
2014-09-16 15:45:31 +02:00
|
|
|
info = new PluginInfo(pluginName, p.getDisplayName(), p.getDescription(), p.getIcon(),
|
|
|
|
p.isEnabledByDefault(), p.hasSettings());
|
2013-08-19 19:57:29 +02:00
|
|
|
availablePluginsInfo.put(pluginName, info); //Cache it
|
|
|
|
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;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2013-08-19 19:57:29 +02:00
|
|
|
public static Plugin instantiatePluginForDevice(Context context, String pluginName, Device device) {
|
|
|
|
Class c = availablePlugins.get(pluginName);
|
2013-08-16 10:31:01 +02:00
|
|
|
if (c == null) {
|
2013-08-19 19:57:29 +02:00
|
|
|
Log.e("PluginFactory", "Plugin not found: "+pluginName);
|
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) {
|
2013-08-19 19:57:29 +02:00
|
|
|
Log.e("PluginFactory", "Could not instantiate plugin: "+pluginName);
|
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 {
|
|
|
|
//I hate this but I need to create an instance because abstract static functions can't be declared
|
2014-06-26 17:37:32 +00:00
|
|
|
String pluginName = (pluginClass.newInstance()).getPluginName();
|
2013-08-19 19:57:29 +02:00
|
|
|
availablePlugins.put(pluginName, pluginClass);
|
|
|
|
} catch(Exception e) {
|
|
|
|
Log.e("PluginFactory","addPlugin exception");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-16 10:31:01 +02:00
|
|
|
}
|