diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index ea037b362e..913f5bca4a 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -323,6 +323,22 @@ public class MainApp extends Application { return newList; } + public static ArrayList getSpecificPluginsListByInterfaceTypeSafe(Class interfaceClass) { + ArrayList newList = new ArrayList<>(); + + if (pluginsList != null) { + for (PluginBase p : pluginsList) { + if (p.getClass() != ConfigBuilderPlugin.class + && interfaceClass.isAssignableFrom(p.getClass())) + newList.add((T) p); + } + } else { + log.error("pluginsList=null"); + } + + return newList; + } + public static ArrayList getSpecificPluginsVisibleInListByInterface(Class interfaceClass, int type) { ArrayList newList = new ArrayList<>(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 1f0563bb0a..464ce57496 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -244,96 +244,90 @@ public class ConfigBuilderPlugin implements PluginBase, ConstraintsInterface, Tr ArrayList pluginsInCategory; // PluginBase.APS - pluginsInCategory = MainApp.getSpecificPluginsListByInterface(APSInterface.class); - activeAPS = (APSInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.APS); - if (activeAPS != null) { - if (Config.logConfigBuilder) - log.debug("Selected APS interface: " + ((PluginBase) activeAPS).getName()); - for (PluginBase p : pluginsInCategory) { - if (!p.getName().equals(((PluginBase) activeAPS).getName())) { - p.setFragmentVisible(PluginBase.APS, false); - } - } - } + activeAPS = this.setFragmentVisibility(APSInterface.class, PluginBase.APS); // PluginBase.INSULIN - pluginsInCategory = MainApp.getSpecificPluginsListByInterface(InsulinInterface.class); - activeInsulin = (InsulinInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.INSULIN); - if (Config.logConfigBuilder) - log.debug("Selected insulin interface: " + ((PluginBase) activeInsulin).getName()); - for (PluginBase p : pluginsInCategory) { - if (!p.getName().equals(((PluginBase) activeInsulin).getName())) { - p.setFragmentVisible(PluginBase.INSULIN, false); - } - } + activeInsulin = this.setFragmentVisibility(InsulinInterface.class, PluginBase.INSULIN); // PluginBase.SENSITIVITY - pluginsInCategory = MainApp.getSpecificPluginsListByInterface(SensitivityInterface.class); - activeSensitivity = (SensitivityInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.SENSITIVITY); - if (Config.logConfigBuilder) - log.debug("Selected sensitivity interface: " + ((PluginBase) activeSensitivity).getName()); - for (PluginBase p : pluginsInCategory) { - if (!p.getName().equals(((PluginBase) activeSensitivity).getName())) { - p.setFragmentVisible(PluginBase.SENSITIVITY, false); - } - } + activeSensitivity = this.setFragmentVisibility(SensitivityInterface.class, PluginBase.SENSITIVITY); // PluginBase.PROFILE - pluginsInCategory = MainApp.getSpecificPluginsListByInterface(ProfileInterface.class); - activeProfile = (ProfileInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PROFILE); - if (Config.logConfigBuilder) - log.debug("Selected profile interface: " + ((PluginBase) activeProfile).getName()); - for (PluginBase p : pluginsInCategory) { - if (!p.getName().equals(((PluginBase) activeProfile).getName())) { - p.setFragmentVisible(PluginBase.PROFILE, false); - } - } + activeProfile = this.setFragmentVisibility(ProfileInterface.class, PluginBase.PROFILE); // PluginBase.BGSOURCE - pluginsInCategory = MainApp.getSpecificPluginsListByInterface(BgSourceInterface.class); - activeBgSource = (BgSourceInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.BGSOURCE); - if (Config.logConfigBuilder) - log.debug("Selected bgSource interface: " + ((PluginBase) activeBgSource).getName()); - for (PluginBase p : pluginsInCategory) { - if (!p.getName().equals(((PluginBase) activeBgSource).getName())) { - p.setFragmentVisible(PluginBase.BGSOURCE, false); - } - } + activeBgSource = this.setFragmentVisibility(BgSourceInterface.class, PluginBase.BGSOURCE); // PluginBase.PUMP pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.PUMP); activePump = (PumpInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.PUMP); if (activePump == null) activePump = VirtualPumpPlugin.getPlugin(); // for NSClient build - if (Config.logConfigBuilder) - log.debug("Selected pump interface: " + ((PluginBase) activePump).getName()); - for (PluginBase p : pluginsInCategory) { - if (!p.getName().equals(((PluginBase) activePump).getName())) { - p.setFragmentVisible(PluginBase.PUMP, false); - } - } + this.disableFragmentVisiblities(((PluginBase)activePump).getName(), pluginsInCategory, PluginBase.PUMP); // PluginBase.LOOP - pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.LOOP); - activeLoop = (LoopPlugin) getTheOneEnabledInArray(pluginsInCategory, PluginBase.LOOP); - if (activeLoop != null) { - if (Config.logConfigBuilder) - log.debug("Selected loop interface: " + activeLoop.getName()); - for (PluginBase p : pluginsInCategory) { - if (!p.getName().equals(activeLoop.getName())) { - p.setFragmentVisible(PluginBase.LOOP, false); - } - } - } + activeLoop = this.setFragmentVisibility(PluginBase.LOOP); // PluginBase.TREATMENT - pluginsInCategory = MainApp.getSpecificPluginsList(PluginBase.TREATMENT); - activeTreatments = (TreatmentsInterface) getTheOneEnabledInArray(pluginsInCategory, PluginBase.TREATMENT); + activeTreatments = this.setFragmentVisibility(PluginBase.TREATMENT); + } + + /** + * disables the visibility for all fragments of Plugins with the given PluginType + * which are not equally named to the Plugin implementing the given Plugin Interface. + * + * @param pluginInterface + * @param pluginType + * @param + * @return + */ + private T setFragmentVisibility(Class pluginInterface, int pluginType) { + ArrayList pluginsInCategory; + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(pluginInterface); + + return this.setFragmentVisibility(pluginsInCategory, pluginType); + } + + private T setFragmentVisibility(int pluginType) { + ArrayList pluginsInCategory; + pluginsInCategory = MainApp.getSpecificPluginsList(pluginType); + + return this.setFragmentVisibility(pluginsInCategory, pluginType); + } + + /** + * disables the visibility for all fragments of Plugins in the given pluginsInCategory + * with the given PluginType which are not equally named to the Plugin implementing the + * given Plugin Interface. + * + * TODO we are casting an interface to PluginBase, which seems to be rather odd, since + * TODO the interface is not implementing PluginBase (this is just avoiding errors through + * TODO conventions. + * + * @param pluginsInCategory + * @param pluginType + * @param + * @return + */ + private T setFragmentVisibility(ArrayList pluginsInCategory, + int pluginType) { + T activePlugin = (T) getTheOneEnabledInArray(pluginsInCategory, pluginType); + + if (activePlugin != null) { + this.disableFragmentVisiblities(((PluginBase)activePlugin).getName(), + pluginsInCategory, pluginType); + } + + return activePlugin; + } + + private void disableFragmentVisiblities(String activePluginName, ArrayList pluginsInCategory, + int pluginType) { if (Config.logConfigBuilder) - log.debug("Selected treatment interface: " + ((PluginBase) activeTreatments).getName()); + log.debug("Selected interface: " + activePluginName); for (PluginBase p : pluginsInCategory) { - if (!p.getName().equals(((PluginBase) activeTreatments).getName())) { - p.setFragmentVisible(PluginBase.TREATMENT, false); + if (!p.getName().equals(activePluginName)) { + p.setFragmentVisible(pluginType, false); } } }