From 3b0a3e265d4bfffabc74e1a6b283afe55fbb4662 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Sat, 6 Jun 2015 00:38:51 -0700 Subject: [PATCH] Plugin key is now the plugin class instead of being defined by each plugin --- ...ces.xml => mousepadplugin_preferences.xml} | 0 ...rences.xml => mprisplugin_preferences.xml} | 0 ...rences.xml => shareplugin_preferences.xml} | 0 src/org/kde/kdeconnect/Device.java | 76 ++++++++++--------- .../Plugins/BatteryPlugin/BatteryPlugin.java | 10 +-- .../ClibpoardPlugin/ClipboardPlugin.java | 4 - .../MousePadPlugin/KeyListenerView.java | 2 +- .../MousePadPlugin/MousePadActivity.java | 16 ++-- .../MousePadPlugin/MousePadPlugin.java | 9 --- .../Plugins/MprisPlugin/MprisActivity.java | 22 +++--- .../Plugins/MprisPlugin/MprisPlugin.java | 8 -- .../NotificationsPlugin.java | 5 -- .../Plugins/PingPlugin/PingPlugin.java | 9 --- src/org/kde/kdeconnect/Plugins/Plugin.java | 13 ++-- .../kde/kdeconnect/Plugins/PluginFactory.java | 30 +++----- .../Plugins/SftpPlugin/SftpPlugin.java | 7 -- .../Plugins/SharePlugin/SharePlugin.java | 8 -- .../TelephonyPlugin/TelephonyPlugin.java | 9 --- .../UserInterface/PluginSettingsActivity.java | 6 +- .../UserInterface/SettingsActivity.java | 15 ++-- 20 files changed, 95 insertions(+), 154 deletions(-) rename res/xml/{plugin_mousepad_preferences.xml => mousepadplugin_preferences.xml} (100%) rename res/xml/{plugin_mpris_preferences.xml => mprisplugin_preferences.xml} (100%) rename res/xml/{plugin_share_preferences.xml => shareplugin_preferences.xml} (100%) diff --git a/res/xml/plugin_mousepad_preferences.xml b/res/xml/mousepadplugin_preferences.xml similarity index 100% rename from res/xml/plugin_mousepad_preferences.xml rename to res/xml/mousepadplugin_preferences.xml diff --git a/res/xml/plugin_mpris_preferences.xml b/res/xml/mprisplugin_preferences.xml similarity index 100% rename from res/xml/plugin_mpris_preferences.xml rename to res/xml/mprisplugin_preferences.xml diff --git a/res/xml/plugin_share_preferences.xml b/res/xml/shareplugin_preferences.xml similarity index 100% rename from res/xml/plugin_share_preferences.xml rename to res/xml/shareplugin_preferences.xml diff --git a/src/org/kde/kdeconnect/Device.java b/src/org/kde/kdeconnect/Device.java index f7853730..a8a25c77 100644 --- a/src/org/kde/kdeconnect/Device.java +++ b/src/org/kde/kdeconnect/Device.java @@ -614,29 +614,37 @@ public class Device implements BaseLink.PackageReceiver { // Plugin-related functions // - public Plugin getPlugin(String name) { - return getPlugin(name, false); + public T getPlugin(Class pluginClass) { + return (T)getPlugin(Plugin.getPluginKey(pluginClass)); } - public Plugin getPlugin(String name, boolean includeFailed) { - Plugin plugin = plugins.get(name); + public T getPlugin(Class pluginClass, boolean includeFailed) { + return (T)getPlugin(Plugin.getPluginKey(pluginClass), includeFailed); + } + + public Plugin getPlugin(String pluginKey) { + return getPlugin(pluginKey, false); + } + + public Plugin getPlugin(String pluginKey, boolean includeFailed) { + Plugin plugin = plugins.get(pluginKey); if (includeFailed && plugin == null) { - plugin = failedPlugins.get(name); + plugin = failedPlugins.get(pluginKey); } return plugin; } - private synchronized void addPlugin(final String name) { - Plugin existing = plugins.get(name); + private synchronized void addPlugin(final String pluginKey) { + Plugin existing = plugins.get(pluginKey); if (existing != null) { - Log.w("KDE/addPlugin","plugin already present:" + name); + Log.w("KDE/addPlugin","plugin already present:" + pluginKey); return; } - final Plugin plugin = PluginFactory.instantiatePluginForDevice(context, name, this); + final Plugin plugin = PluginFactory.instantiatePluginForDevice(context, pluginKey, this); if (plugin == null) { - Log.e("KDE/addPlugin","could not instantiate plugin: "+name); - failedPlugins.put(name, plugin); + Log.e("KDE/addPlugin","could not instantiate plugin: "+pluginKey); + failedPlugins.put(pluginKey, plugin); return; } @@ -650,17 +658,17 @@ public class Device implements BaseLink.PackageReceiver { } catch (Exception e) { success = false; e.printStackTrace(); - Log.e("KDE/addPlugin", "Exception loading plugin " + name); + Log.e("KDE/addPlugin", "Exception loading plugin " + pluginKey); } if (success) { - //Log.e("addPlugin","added " + name); - failedPlugins.remove(name); - plugins.put(name, plugin); + //Log.e("addPlugin","added " + pluginKey); + failedPlugins.remove(pluginKey); + plugins.put(pluginKey, plugin); } else { - Log.e("KDE/addPlugin", "plugin failed to load " + name); - plugins.remove(name); - failedPlugins.put(name, plugin); + Log.e("KDE/addPlugin", "plugin failed to load " + pluginKey); + plugins.remove(pluginKey); + failedPlugins.put(pluginKey, plugin); } for (PluginsChangedListener listener : pluginsChangedListeners) { @@ -672,10 +680,10 @@ public class Device implements BaseLink.PackageReceiver { } - private synchronized boolean removePlugin(String name) { + private synchronized boolean removePlugin(String pluginKey) { - Plugin plugin = plugins.remove(name); - Plugin failedPlugin = failedPlugins.remove(name); + Plugin plugin = plugins.remove(pluginKey); + Plugin failedPlugin = failedPlugins.remove(pluginKey); if (plugin == null) { if (failedPlugin == null) { @@ -687,10 +695,10 @@ public class Device implements BaseLink.PackageReceiver { try { plugin.onDestroy(); - //Log.e("removePlugin","removed " + name); + //Log.e("removePlugin","removed " + pluginKey); } catch (Exception e) { e.printStackTrace(); - Log.e("KDE/removePlugin","Exception calling onDestroy for plugin "+name); + Log.e("KDE/removePlugin","Exception calling onDestroy for plugin "+pluginKey); } for (PluginsChangedListener listener : pluginsChangedListeners) { @@ -700,15 +708,15 @@ public class Device implements BaseLink.PackageReceiver { return true; } - public void setPluginEnabled(String pluginName, boolean value) { - settings.edit().putBoolean(pluginName,value).apply(); - if (value && isPaired() && isReachable()) addPlugin(pluginName); - else removePlugin(pluginName); + public void setPluginEnabled(String pluginKey, boolean value) { + settings.edit().putBoolean(pluginKey,value).apply(); + if (value && isPaired() && isReachable()) addPlugin(pluginKey); + else removePlugin(pluginKey); } - public boolean isPluginEnabled(String pluginName) { - boolean enabledByDefault = PluginFactory.getPluginInfo(context, pluginName).isEnabledByDefault(); - boolean enabled = settings.getBoolean(pluginName, enabledByDefault); + public boolean isPluginEnabled(String pluginKey) { + boolean enabledByDefault = PluginFactory.getPluginInfo(context, pluginKey).isEnabledByDefault(); + boolean enabled = settings.getBoolean(pluginKey, enabledByDefault); return enabled; } @@ -722,15 +730,15 @@ public class Device implements BaseLink.PackageReceiver { Set availablePlugins = PluginFactory.getAvailablePlugins(); - for(String pluginName : availablePlugins) { + for(String pluginKey : availablePlugins) { boolean enabled = false; if (isPaired() && isReachable()) { - enabled = isPluginEnabled(pluginName); + enabled = isPluginEnabled(pluginKey); } if (enabled) { - addPlugin(pluginName); + addPlugin(pluginKey); } else { - removePlugin(pluginName); + removePlugin(pluginKey); } } diff --git a/src/org/kde/kdeconnect/Plugins/BatteryPlugin/BatteryPlugin.java b/src/org/kde/kdeconnect/Plugins/BatteryPlugin/BatteryPlugin.java index 35a5f011..ea034081 100644 --- a/src/org/kde/kdeconnect/Plugins/BatteryPlugin/BatteryPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/BatteryPlugin/BatteryPlugin.java @@ -20,20 +20,17 @@ package org.kde.kdeconnect.Plugins.BatteryPlugin; -import android.app.Activity; -import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.graphics.drawable.Drawable; import android.os.BatteryManager; -import android.widget.Button; import org.kde.kdeconnect.NetworkPackage; import org.kde.kdeconnect.Plugins.Plugin; import org.kde.kdeconnect_tp.R; + public class BatteryPlugin extends Plugin { // keep these fields in sync with kdeconnect-kded:BatteryPlugin.h:ThresholdBatteryEvent @@ -42,11 +39,6 @@ public class BatteryPlugin extends Plugin { private NetworkPackage lastInfo = null; - @Override - public String getPluginName() { - return "plugin_battery"; - } - @Override public String getDisplayName() { return context.getResources().getString(R.string.pref_plugin_battery); diff --git a/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardPlugin.java b/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardPlugin.java index d96857eb..de00305a 100644 --- a/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardPlugin.java @@ -31,10 +31,6 @@ import org.kde.kdeconnect_tp.R; public class ClipboardPlugin extends Plugin { - @Override - public String getPluginName() { - return "plugin_clipboard"; - } @Override public String getDisplayName() { return context.getResources().getString(R.string.pref_plugin_clipboard); diff --git a/src/org/kde/kdeconnect/Plugins/MousePadPlugin/KeyListenerView.java b/src/org/kde/kdeconnect/Plugins/MousePadPlugin/KeyListenerView.java index 7b069cf0..1228fd56 100644 --- a/src/org/kde/kdeconnect/Plugins/MousePadPlugin/KeyListenerView.java +++ b/src/org/kde/kdeconnect/Plugins/MousePadPlugin/KeyListenerView.java @@ -176,7 +176,7 @@ public class KeyListenerView extends View { @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MousePadPlugin mousePadPlugin = (MousePadPlugin) device.getPlugin("plugin_mousepad"); + MousePadPlugin mousePadPlugin = (MousePadPlugin) device.getPlugin(MousePadPlugin.class); if (mousePadPlugin == null) return; mousePadPlugin.sendKeyboardPacket(np); } diff --git a/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java b/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java index 9e6aab7f..322609de 100644 --- a/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java +++ b/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadActivity.java @@ -182,7 +182,7 @@ public class MousePadActivity extends ActionBarActivity implements GestureDetect @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad"); + MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class); if (mousePadPlugin == null) return; mousePadPlugin.sendMouseDelta(mCurrentX - mPrevX, mCurrentY - mPrevY); mPrevX = mCurrentX; @@ -227,7 +227,7 @@ public class MousePadActivity extends ActionBarActivity implements GestureDetect @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad"); + MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class); if (mousePadPlugin == null) return; mousePadPlugin.sendScroll(0, scrollToSendY); } @@ -248,7 +248,7 @@ public class MousePadActivity extends ActionBarActivity implements GestureDetect @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad"); + MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class); if (mousePadPlugin == null) return; mousePadPlugin.sendSingleHold(); } @@ -266,7 +266,7 @@ public class MousePadActivity extends ActionBarActivity implements GestureDetect @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad"); + MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class); if (mousePadPlugin == null) return; mousePadPlugin.sendSingleClick(); } @@ -280,7 +280,7 @@ public class MousePadActivity extends ActionBarActivity implements GestureDetect @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad"); + MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class); if (mousePadPlugin == null) return; mousePadPlugin.sendDoubleClick(); } @@ -327,7 +327,7 @@ public class MousePadActivity extends ActionBarActivity implements GestureDetect @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad"); + MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class); if (mousePadPlugin == null) return; mousePadPlugin.sendMiddleClick(); } @@ -339,7 +339,7 @@ public class MousePadActivity extends ActionBarActivity implements GestureDetect @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MousePadPlugin mousePadPlugin = (MousePadPlugin) device.getPlugin("plugin_mousepad"); + MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class); if (mousePadPlugin == null) return; mousePadPlugin.sendRightClick(); } @@ -350,7 +350,7 @@ public class MousePadActivity extends ActionBarActivity implements GestureDetect @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MousePadPlugin mousePadPlugin = (MousePadPlugin) device.getPlugin("plugin_mousepad"); + MousePadPlugin mousePadPlugin = device.getPlugin(MousePadPlugin.class); if (mousePadPlugin == null) return; mousePadPlugin.sendSingleHold(); } diff --git a/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadPlugin.java b/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadPlugin.java index 8f2126bd..f17bd31d 100644 --- a/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/MousePadPlugin/MousePadPlugin.java @@ -21,24 +21,15 @@ package org.kde.kdeconnect.Plugins.MousePadPlugin; import android.app.Activity; -import android.app.AlertDialog; import android.content.Intent; import android.graphics.drawable.Drawable; -import android.view.View; -import android.widget.Button; import org.kde.kdeconnect.NetworkPackage; -import org.kde.kdeconnect.Plugins.MprisPlugin.MprisActivity; import org.kde.kdeconnect.Plugins.Plugin; import org.kde.kdeconnect_tp.R; public class MousePadPlugin extends Plugin { - @Override - public String getPluginName() { - return "plugin_mousepad"; - } - @Override public String getDisplayName() { return context.getString(R.string.pref_plugin_mousepad); diff --git a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisActivity.java b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisActivity.java index d9c3f718..ccbd50fd 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisActivity.java +++ b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisActivity.java @@ -75,7 +75,7 @@ public class MprisActivity extends ActionBarActivity { public void onServiceStart(BackgroundService service) { final Device device = service.getDevice(deviceId); - final MprisPlugin mpris = (MprisPlugin) device.getPlugin("plugin_mpris"); + final MprisPlugin mpris = device.getPlugin(MprisPlugin.class); if (mpris == null) { Log.e("MprisActivity", "device has no mpris plugin!"); return; @@ -246,7 +246,7 @@ public class MprisActivity extends ActionBarActivity { @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MprisPlugin mpris = (MprisPlugin) device.getPlugin("plugin_mpris"); + MprisPlugin mpris = device.getPlugin(MprisPlugin.class); if (mpris == null) return; updateVolume(mpris, 5); } @@ -257,7 +257,7 @@ public class MprisActivity extends ActionBarActivity { @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MprisPlugin mpris = (MprisPlugin) device.getPlugin("plugin_mpris"); + MprisPlugin mpris = device.getPlugin(MprisPlugin.class); if (mpris == null) return; updateVolume(mpris, -5); } @@ -309,7 +309,7 @@ public class MprisActivity extends ActionBarActivity { @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MprisPlugin mpris = (MprisPlugin)device.getPlugin("plugin_mpris"); + MprisPlugin mpris = device.getPlugin(MprisPlugin.class); if (mpris == null) return; mpris.sendAction("PlayPause"); } @@ -324,7 +324,7 @@ public class MprisActivity extends ActionBarActivity { @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MprisPlugin mpris = (MprisPlugin)device.getPlugin("plugin_mpris"); + MprisPlugin mpris = device.getPlugin(MprisPlugin.class); if (mpris == null) return; mpris.sendAction("Previous"); } @@ -339,7 +339,7 @@ public class MprisActivity extends ActionBarActivity { @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MprisPlugin mpris = (MprisPlugin)device.getPlugin("plugin_mpris"); + MprisPlugin mpris = device.getPlugin(MprisPlugin.class); if (mpris == null) return; mpris.Seek(interval_time * -1); } @@ -354,7 +354,7 @@ public class MprisActivity extends ActionBarActivity { @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MprisPlugin mpris = (MprisPlugin)device.getPlugin("plugin_mpris"); + MprisPlugin mpris = device.getPlugin(MprisPlugin.class); if (mpris == null) return; mpris.Seek(interval_time); } @@ -369,7 +369,7 @@ public class MprisActivity extends ActionBarActivity { @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MprisPlugin mpris = (MprisPlugin)device.getPlugin("plugin_mpris"); + MprisPlugin mpris = device.getPlugin(MprisPlugin.class); if (mpris == null) return; mpris.sendAction("Next"); } @@ -392,7 +392,7 @@ public class MprisActivity extends ActionBarActivity { @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MprisPlugin mpris = (MprisPlugin) device.getPlugin("plugin_mpris"); + MprisPlugin mpris = device.getPlugin(MprisPlugin.class); if (mpris == null) return; mpris.setVolume(seekBar.getProgress()); } @@ -409,7 +409,7 @@ public class MprisActivity extends ActionBarActivity { @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MprisPlugin mpris = (MprisPlugin) device.getPlugin("plugin_mpris"); + MprisPlugin mpris = device.getPlugin(MprisPlugin.class); if (mpris != null) { positionSeek.setProgress((int) (mpris.getPosition())); } @@ -438,7 +438,7 @@ public class MprisActivity extends ActionBarActivity { @Override public void onServiceStart(BackgroundService service) { Device device = service.getDevice(deviceId); - MprisPlugin mpris = (MprisPlugin) device.getPlugin("plugin_mpris"); + MprisPlugin mpris = device.getPlugin(MprisPlugin.class); if (mpris != null) { mpris.setPosition(seekBar.getProgress()); } diff --git a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisPlugin.java b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisPlugin.java index 0a278185..ee110e4d 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisPlugin.java @@ -21,14 +21,11 @@ package org.kde.kdeconnect.Plugins.MprisPlugin; import android.app.Activity; -import android.app.AlertDialog; import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Message; import android.util.Log; -import android.view.View; -import android.widget.Button; import org.kde.kdeconnect.NetworkPackage; import org.kde.kdeconnect.Plugins.Plugin; @@ -51,11 +48,6 @@ public class MprisPlugin extends Plugin { private ArrayList playerList = new ArrayList(); private HashMap playerListUpdated = new HashMap(); - @Override - public String getPluginName() { - return "plugin_mpris"; - } - @Override public String getDisplayName() { return context.getResources().getString(R.string.pref_plugin_mpris); diff --git a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java index 2d57abb2..03b9c626 100644 --- a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java @@ -30,7 +30,6 @@ import android.os.Bundle; import android.provider.Settings; import android.service.notification.StatusBarNotification; import android.util.Log; -import android.widget.Button; import org.kde.kdeconnect.Helpers.AppsHelper; import org.kde.kdeconnect.NetworkPackage; @@ -43,10 +42,6 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver. /* private boolean sendIcons = false; */ - @Override - public String getPluginName() { - return "plugin_notifications"; - } @Override public String getDisplayName() { diff --git a/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java b/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java index a65c9213..1b58e804 100644 --- a/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java @@ -21,17 +21,13 @@ package org.kde.kdeconnect.Plugins.PingPlugin; import android.app.Activity; -import android.app.AlertDialog; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; -import android.graphics.drawable.Drawable; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; -import android.view.View; -import android.widget.Button; import org.kde.kdeconnect.NetworkPackage; import org.kde.kdeconnect.Plugins.Plugin; @@ -41,11 +37,6 @@ import org.kde.kdeconnect_tp.R; public class PingPlugin extends Plugin { - @Override - public String getPluginName() { - return "plugin_ping"; - } - @Override public String getDisplayName() { return context.getResources().getString(R.string.pref_plugin_ping); diff --git a/src/org/kde/kdeconnect/Plugins/Plugin.java b/src/org/kde/kdeconnect/Plugins/Plugin.java index f4a6d5fb..556e89d2 100644 --- a/src/org/kde/kdeconnect/Plugins/Plugin.java +++ b/src/org/kde/kdeconnect/Plugins/Plugin.java @@ -32,7 +32,6 @@ import org.kde.kdeconnect.Device; import org.kde.kdeconnect.NetworkPackage; import org.kde.kdeconnect.UserInterface.PluginSettingsActivity; import org.kde.kdeconnect.UserInterface.SettingsActivity; -import org.kde.kdeconnect_tp.R; public abstract class Plugin { @@ -46,10 +45,14 @@ public abstract class Plugin { /** * Return the internal plugin name, that will be used as a - * unique key to distinguish it. This function can not access - * this.context nor this.device. + * unique key to distinguish it. Use the class name as key. */ - public abstract String getPluginName(); + public String getPluginKey() { + return getPluginKey(this.getClass()); + } + public static String getPluginKey(Class p) { + return p.getSimpleName(); + } /** * Return the human-readable plugin name. This function can @@ -104,7 +107,7 @@ public abstract class Plugin { public void startPreferencesActivity(SettingsActivity parentActivity) { Intent intent = new Intent(parentActivity, PluginSettingsActivity.class); intent.putExtra("plugin_display_name", getDisplayName()); - intent.putExtra("plugin_name", getPluginName()); + intent.putExtra("plugin_key", getPluginKey()); parentActivity.startActivity(intent); } diff --git a/src/org/kde/kdeconnect/Plugins/PluginFactory.java b/src/org/kde/kdeconnect/Plugins/PluginFactory.java index 01006905..2448bd63 100644 --- a/src/org/kde/kdeconnect/Plugins/PluginFactory.java +++ b/src/org/kde/kdeconnect/Plugins/PluginFactory.java @@ -44,9 +44,8 @@ public class PluginFactory { public static class PluginInfo { - public PluginInfo(String pluginName, String displayName, String description, Drawable icon, + public PluginInfo(String displayName, String description, Drawable icon, boolean enabledByDefault, boolean hasSettings) { - this.pluginName = pluginName; this.displayName = displayName; this.description = description; this.icon = icon; @@ -54,10 +53,6 @@ public class PluginFactory { this.hasSettings = hasSettings; } - public String getPluginName() { - return pluginName; - } - public String getDisplayName() { return displayName; } @@ -76,7 +71,6 @@ public class PluginFactory { return enabledByDefault; } - private final String pluginName; private final String displayName; private final String description; private final Drawable icon; @@ -101,15 +95,15 @@ public class PluginFactory { PluginFactory.registerPlugin(SharePlugin.class); } - public static PluginInfo getPluginInfo(Context context, String pluginName) { - PluginInfo info = availablePluginsInfo.get(pluginName); //Is it cached? + public static PluginInfo getPluginInfo(Context context, String pluginKey) { + PluginInfo info = availablePluginsInfo.get(pluginKey); //Is it cached? if (info != null) return info; try { - Plugin p = ((Plugin)availablePlugins.get(pluginName).newInstance()); + Plugin p = ((Plugin)availablePlugins.get(pluginKey).newInstance()); p.setContext(context, null); - info = new PluginInfo(pluginName, p.getDisplayName(), p.getDescription(), p.getIcon(), + info = new PluginInfo(p.getDisplayName(), p.getDescription(), p.getIcon(), p.isEnabledByDefault(), p.hasSettings()); - availablePluginsInfo.put(pluginName, info); //Cache it + availablePluginsInfo.put(pluginKey, info); //Cache it return info; } catch(Exception e) { Log.e("PluginFactory","getPluginInfo exception"); @@ -122,10 +116,10 @@ public class PluginFactory { return availablePlugins.keySet(); } - public static Plugin instantiatePluginForDevice(Context context, String pluginName, Device device) { - Class c = availablePlugins.get(pluginName); + public static Plugin instantiatePluginForDevice(Context context, String pluginKey, Device device) { + Class c = availablePlugins.get(pluginKey); if (c == null) { - Log.e("PluginFactory", "Plugin not found: "+pluginName); + Log.e("PluginFactory", "Plugin not found: "+pluginKey); return null; } @@ -134,7 +128,7 @@ public class PluginFactory { plugin.setContext(context, device); return plugin; } catch(Exception e) { - Log.e("PluginFactory", "Could not instantiate plugin: "+pluginName); + Log.e("PluginFactory", "Could not instantiate plugin: "+pluginKey); e.printStackTrace(); return null; } @@ -144,8 +138,8 @@ public class PluginFactory { public static void registerPlugin(Class pluginClass) { try { //I hate this but I need to create an instance because abstract static functions can't be declared - String pluginName = (pluginClass.newInstance()).getPluginName(); - availablePlugins.put(pluginName, pluginClass); + String pluginKey = Plugin.getPluginKey(pluginClass); + availablePlugins.put(pluginKey, pluginClass); } catch(Exception e) { Log.e("PluginFactory","addPlugin exception"); e.printStackTrace(); diff --git a/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java b/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java index ec480c71..ecf5ca0b 100644 --- a/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/SftpPlugin/SftpPlugin.java @@ -20,11 +20,7 @@ package org.kde.kdeconnect.Plugins.SftpPlugin; -import android.app.Activity; -import android.app.AlertDialog; -import android.graphics.drawable.Drawable; import android.os.Environment; -import android.widget.Button; import org.kde.kdeconnect.Helpers.StorageHelper; import org.kde.kdeconnect.NetworkPackage; @@ -39,9 +35,6 @@ public class SftpPlugin extends Plugin { private static final SimpleSftpServer server = new SimpleSftpServer(); - @Override - public String getPluginName() {return "plugin_sftp";} - @Override public String getDisplayName() { return context.getResources().getString(R.string.pref_plugin_sftp); diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java index 779aa7b8..0d14cb1f 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java @@ -21,7 +21,6 @@ package org.kde.kdeconnect.Plugins.SharePlugin; import android.app.Activity; -import android.app.AlertDialog; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; @@ -37,7 +36,6 @@ import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.util.Log; -import android.widget.Button; import android.widget.Toast; import org.kde.kdeconnect.Helpers.FilesHelper; @@ -50,14 +48,8 @@ import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; - public class SharePlugin extends Plugin { - @Override - public String getPluginName() { - return "plugin_share"; - } - @Override public String getDisplayName() { return context.getResources().getString(R.string.pref_plugin_sharereceiver); diff --git a/src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java b/src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java index 65b3b393..f1e5ae2b 100644 --- a/src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java @@ -20,18 +20,14 @@ package org.kde.kdeconnect.Plugins.TelephonyPlugin; -import android.app.Activity; -import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.graphics.drawable.Drawable; import android.media.AudioManager; import android.os.Bundle; import android.telephony.SmsMessage; import android.telephony.TelephonyManager; -import android.widget.Button; import org.kde.kdeconnect.Helpers.ContactsHelper; import org.kde.kdeconnect.NetworkPackage; @@ -47,11 +43,6 @@ public class TelephonyPlugin extends Plugin { private NetworkPackage lastPackage = null; private boolean isMuted = false; - @Override - public String getPluginName() { - return "plugin_telephony"; - } - @Override public String getDisplayName() { return context.getResources().getString(R.string.pref_plugin_telephony); diff --git a/src/org/kde/kdeconnect/UserInterface/PluginSettingsActivity.java b/src/org/kde/kdeconnect/UserInterface/PluginSettingsActivity.java index f359056f..ba44e4ff 100644 --- a/src/org/kde/kdeconnect/UserInterface/PluginSettingsActivity.java +++ b/src/org/kde/kdeconnect/UserInterface/PluginSettingsActivity.java @@ -25,6 +25,8 @@ import android.preference.PreferenceActivity; import org.kde.kdeconnect_tp.R; +import java.util.Locale; + public class PluginSettingsActivity extends PreferenceActivity { @Override @@ -34,8 +36,8 @@ public class PluginSettingsActivity extends PreferenceActivity { String pluginDisplayName = getIntent().getStringExtra("plugin_display_name"); setTitle(getString(R.string.plugin_settings_with_name, pluginDisplayName)); - String pluginName = getIntent().getStringExtra("plugin_name"); - int resFile = getResources().getIdentifier(pluginName + "_preferences", "xml", getPackageName()); + String pluginKey = getIntent().getStringExtra("plugin_key"); + int resFile = getResources().getIdentifier(pluginKey.toLowerCase(Locale.ENGLISH) + "_preferences", "xml", getPackageName()); addPreferencesFromResource(resFile); } diff --git a/src/org/kde/kdeconnect/UserInterface/SettingsActivity.java b/src/org/kde/kdeconnect/UserInterface/SettingsActivity.java index 4e9d8525..06fd373a 100644 --- a/src/org/kde/kdeconnect/UserInterface/SettingsActivity.java +++ b/src/org/kde/kdeconnect/UserInterface/SettingsActivity.java @@ -35,6 +35,7 @@ import org.kde.kdeconnect.Plugins.PluginFactory; import org.kde.kdeconnect_tp.R; import java.util.ArrayList; +import java.util.Locale; import java.util.Set; public class SettingsActivity extends PreferenceActivity { @@ -59,20 +60,20 @@ public class SettingsActivity extends PreferenceActivity { Set plugins = PluginFactory.getAvailablePlugins(); final ArrayList preferences = new ArrayList(); - for (final String pluginName : plugins) { + for (final String pluginKey : plugins) { final CheckBoxPreference pref = new CheckBoxPreference(getBaseContext()); - PluginFactory.PluginInfo info = PluginFactory.getPluginInfo(getBaseContext(), pluginName); - pref.setKey(pluginName); + PluginFactory.PluginInfo info = PluginFactory.getPluginInfo(getBaseContext(), pluginKey); + pref.setKey(pluginKey); pref.setTitle(info.getDisplayName()); pref.setSummary(info.getDescription()); - pref.setChecked(device.isPluginEnabled(pluginName)); + pref.setChecked(device.isPluginEnabled(pluginKey)); preferences.add(pref); preferenceScreen.addPreference(pref); if (info.hasSettings()) { final Preference pluginPreference = new Preference(getBaseContext()); - pluginPreference.setKey(pluginName + "_preferences"); + pluginPreference.setKey(pluginKey.toLowerCase(Locale.ENGLISH) + "_preferences"); pluginPreference.setSummary(getString(R.string.plugin_settings_with_name, info.getDisplayName())); preferences.add(pluginPreference); preferenceScreen.addPreference(pluginPreference); @@ -91,8 +92,8 @@ public class SettingsActivity extends PreferenceActivity { check.setChecked(!enabled); } else { //Is a plugin suboption if (pref.isEnabled()) { - String pluginName = pref.getDependency(); //The parent pref will be named like the plugin - Plugin plugin = device.getPlugin(pluginName, true); + String pluginKey = pref.getDependency(); //The parent pref will be named like the plugin + Plugin plugin = device.getPlugin(pluginKey, true); if (plugin != null) { plugin.startPreferencesActivity(SettingsActivity.this); } else { //Could happen if the device is not connected anymore