From b932b629c4f0e626a3baeebda14164721e28ac37 Mon Sep 17 00:00:00 2001 From: Chumva Date: Thu, 14 Nov 2019 11:01:09 +0200 Subject: [PATCH] Check enabled plugins from settings --- OsmAnd/src/net/osmand/plus/OsmandPlugin.java | 92 +++++++++++-------- .../osmand/plus/activities/MapActivity.java | 2 +- .../plus/activities/PluginActivity.java | 2 +- .../plus/activities/PluginsActivity.java | 2 +- 4 files changed, 58 insertions(+), 40 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java index 5f5b365e11..e2bd2addd2 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java @@ -137,32 +137,33 @@ public abstract class OsmandPlugin { } public static void initPlugins(@NonNull OsmandApplication app) { - OsmandSettings settings = app.getSettings(); - Set enabledPlugins = settings.getEnabledPlugins(); - allPlugins.clear(); allPlugins.add(new MapillaryPlugin(app)); - enabledPlugins.add(MapillaryPlugin.ID); + + if (!app.getSettings().getPlugins().contains(MapillaryPlugin.ID)) { + app.getSettings().enablePlugin(MapillaryPlugin.ID, true); + } allPlugins.add(new OsmandRasterMapsPlugin(app)); allPlugins.add(new OsmandMonitoringPlugin(app)); - checkMarketPlugin(app, enabledPlugins, new SRTMPlugin(app), true, SRTM_PLUGIN_COMPONENT_PAID, SRTM_PLUGIN_COMPONENT); + checkMarketPlugin(app, new SRTMPlugin(app), true, SRTM_PLUGIN_COMPONENT_PAID, SRTM_PLUGIN_COMPONENT); // ? questionable - definitely not market plugin // checkMarketPlugin(app, enabledPlugins, new TouringViewPlugin(app), false, TouringViewPlugin.COMPONENT, null); - checkMarketPlugin(app, enabledPlugins, new NauticalMapsPlugin(app), false, NauticalMapsPlugin.COMPONENT, null); - checkMarketPlugin(app, enabledPlugins, new SkiMapsPlugin(app), false, SkiMapsPlugin.COMPONENT, null); + checkMarketPlugin(app, new NauticalMapsPlugin(app), false, NauticalMapsPlugin.COMPONENT, null); + checkMarketPlugin(app, new SkiMapsPlugin(app), false, SkiMapsPlugin.COMPONENT, null); allPlugins.add(new AudioVideoNotesPlugin(app)); - checkMarketPlugin(app, enabledPlugins, new ParkingPositionPlugin(app), false, ParkingPositionPlugin.PARKING_PLUGIN_COMPONENT, null); + checkMarketPlugin(app, new ParkingPositionPlugin(app), false, ParkingPositionPlugin.PARKING_PLUGIN_COMPONENT, null); allPlugins.add(new AccessibilityPlugin(app)); allPlugins.add(new OsmEditingPlugin(app)); allPlugins.add(new OsmandDevelopmentPlugin(app)); - activatePlugins(app, enabledPlugins); + activatePlugins(app); } - private static void activatePlugins(OsmandApplication app, Set enabledPlugins) { + private static void activatePlugins(OsmandApplication app) { + Set enabledPlugins = app.getSettings().getEnabledPlugins(); for (OsmandPlugin plugin : allPlugins) { if (enabledPlugins.contains(plugin.getId()) || plugin.isActive()) { initPlugin(app, plugin); @@ -171,6 +172,7 @@ public abstract class OsmandPlugin { } public static void updateActivatedPlugins(OsmandApplication app) { + checkMarketPlugins(app); Set enabledPlugins = app.getSettings().getEnabledPlugins(); for (OsmandPlugin plugin : allPlugins) { if (enabledPlugins.contains(plugin.getId())) { @@ -192,8 +194,15 @@ public abstract class OsmandPlugin { } } - private static void checkMarketPlugin(@NonNull OsmandApplication app, @NonNull Set enabledPlugins, - @NonNull OsmandPlugin plugin, boolean paid, String id, String id2) { + private static void checkMarketPlugin(@NonNull OsmandApplication app, @NonNull OsmandPlugin plugin, + boolean paid, String id, String id2) { + if (updateMarketPlugin(app, plugin, paid, id, id2)) { + allPlugins.add(plugin); + } + } + + private static boolean updateMarketPlugin(@NonNull OsmandApplication app, @NonNull OsmandPlugin plugin, + boolean paid, String id, String id2) { boolean marketEnabled = Version.isMarketEnabled(app); boolean pckg = checkPackage(app, id, id2); if ((Version.isDeveloperVersion(app) || !Version.isProductionVersion(app)) && !paid) { @@ -202,53 +211,62 @@ public abstract class OsmandPlugin { } if (pckg || (!marketEnabled && !paid)) { if (pckg && !app.getSettings().getPlugins().contains("-" + plugin.getId())) { + app.getSettings().enablePlugin(plugin.getId(), true); plugin.setActive(true); } - allPlugins.add(plugin); + plugin.setInstallURL(null); + return true; } else { if (marketEnabled) { plugin.setActive(false); if (!app.getSettings().getPlugins().contains("-" + plugin.getId())) { - enabledPlugins.remove(plugin.getId()); + app.getSettings().enablePlugin(plugin.getId(), false); } plugin.setInstallURL(Version.getUrlWithUtmRef(app, id)); - allPlugins.add(plugin); + return true; } } + return false; + } + + private static void checkMarketPlugins(@NonNull OsmandApplication app) { + SRTMPlugin srtmPlugin = OsmandPlugin.getPlugin(SRTMPlugin.class); + if (srtmPlugin != null) { + updateMarketPlugin(app, srtmPlugin, true, SRTM_PLUGIN_COMPONENT_PAID, SRTM_PLUGIN_COMPONENT); + } + NauticalMapsPlugin nauticalMapsPlugin = OsmandPlugin.getPlugin(NauticalMapsPlugin.class); + if (nauticalMapsPlugin != null) { + updateMarketPlugin(app, nauticalMapsPlugin, false, NauticalMapsPlugin.COMPONENT, null); + } + SkiMapsPlugin skiMapsPlugin = OsmandPlugin.getPlugin(SkiMapsPlugin.class); + if (skiMapsPlugin != null) { + updateMarketPlugin(app, skiMapsPlugin, false, SkiMapsPlugin.COMPONENT, null); + } + ParkingPositionPlugin parkingPlugin = OsmandPlugin.getPlugin(ParkingPositionPlugin.class); + if (parkingPlugin != null) { + updateMarketPlugin(app, parkingPlugin, false, ParkingPositionPlugin.PARKING_PLUGIN_COMPONENT, null); + } } public static void checkInstalledMarketPlugins(@NonNull OsmandApplication app, @Nullable Activity activity) { - List installedPlugins = new ArrayList(); - for (OsmandPlugin osmandPlugin : OsmandPlugin.getAvailablePlugins()) { - if (osmandPlugin.getInstallURL() != null) { + for (OsmandPlugin plugin : OsmandPlugin.getAvailablePlugins()) { + if (plugin.getInstallURL() != null) { boolean pckg = false; - if (osmandPlugin instanceof SRTMPlugin) { - pckg = checkPackage(app, OsmandPlugin.SRTM_PLUGIN_COMPONENT_PAID, OsmandPlugin.SRTM_PLUGIN_COMPONENT); - } else if (osmandPlugin instanceof NauticalMapsPlugin) { + if (plugin instanceof SRTMPlugin) { + pckg = checkPackage(app, SRTM_PLUGIN_COMPONENT_PAID, SRTM_PLUGIN_COMPONENT); + } else if (plugin instanceof NauticalMapsPlugin) { pckg = checkPackage(app, NauticalMapsPlugin.COMPONENT, null); - } else if (osmandPlugin instanceof SkiMapsPlugin) { + } else if (plugin instanceof SkiMapsPlugin) { pckg = checkPackage(app, SkiMapsPlugin.COMPONENT, null); - } else if (osmandPlugin instanceof ParkingPositionPlugin) { + } else if (plugin instanceof ParkingPositionPlugin) { pckg = checkPackage(app, ParkingPositionPlugin.PARKING_PLUGIN_COMPONENT, null); } if (pckg) { - installedPlugins.add(osmandPlugin); + plugin.onInstall(app, activity); + initPlugin(app, plugin); } } } - for (OsmandPlugin osmandPlugin : installedPlugins) { - osmandPlugin.setInstallURL(null); - osmandPlugin.onInstall(app, activity); - - String pluginId = osmandPlugin.getId(); - for (ApplicationMode mode : ApplicationMode.allPossibleValues()) { - Set plugins = app.getSettings().getPluginsForMode(mode); - if (!plugins.contains("-" + pluginId) && !plugins.contains(pluginId)) { - app.getSettings().enablePluginForMode(pluginId, true, mode); - } - } - initPlugin(app, osmandPlugin); - } } private static boolean checkPackage(OsmandApplication app, String id, String id2) { diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 0ed7a70b97..08fdbd3758 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -798,7 +798,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven readLocationToShow(); - OsmandPlugin.checkInstalledMarketPlugins(app,this); + OsmandPlugin.checkInstalledMarketPlugins(app, this); OsmandPlugin.onMapActivityResume(this); boolean showOsmAndWelcomeScreen = true; diff --git a/OsmAnd/src/net/osmand/plus/activities/PluginActivity.java b/OsmAnd/src/net/osmand/plus/activities/PluginActivity.java index d74e00a699..650719f748 100644 --- a/OsmAnd/src/net/osmand/plus/activities/PluginActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/PluginActivity.java @@ -110,7 +110,7 @@ public class PluginActivity extends OsmandActionBarActivity { protected void onResume() { super.onResume(); OsmandApplication app = getMyApplication(); - OsmandPlugin.checkInstalledMarketPlugins(app,this); + OsmandPlugin.checkInstalledMarketPlugins(app, this); updateState(); } diff --git a/OsmAnd/src/net/osmand/plus/activities/PluginsActivity.java b/OsmAnd/src/net/osmand/plus/activities/PluginsActivity.java index 02dfaffe3d..134e495db8 100644 --- a/OsmAnd/src/net/osmand/plus/activities/PluginsActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/PluginsActivity.java @@ -58,7 +58,7 @@ public class PluginsActivity extends OsmandListActivity { protected void onResume() { super.onResume(); OsmandApplication app = getMyApplication(); - OsmandPlugin.checkInstalledMarketPlugins(app,this); + OsmandPlugin.checkInstalledMarketPlugins(app, this); getListAdapter().notifyDataSetChanged(); }