From 9c08188485105444851afb8a4620baf6e0d0f557 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Sun, 15 Mar 2020 23:31:31 +0200 Subject: [PATCH] Save custom plugins first part --- .../net/osmand/plus/CustomOsmandPlugin.java | 14 ++++ OsmAnd/src/net/osmand/plus/OsmandPlugin.java | 64 +++++++++++++++++++ .../net/osmand/plus/helpers/ImportHelper.java | 8 ++- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java index b10cda729f..0e25e4e0fb 100644 --- a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java @@ -1,5 +1,8 @@ package net.osmand.plus; +import org.json.JSONException; +import org.json.JSONObject; + public class CustomOsmandPlugin extends OsmandPlugin { public String pluginId; @@ -48,4 +51,15 @@ public class CustomOsmandPlugin extends OsmandPlugin { public int getLogoResourceId() { return R.drawable.ic_action_skiing; } + + public String toJson() throws JSONException { + JSONObject json = new JSONObject(); + + json.put("type", SettingsHelper.SettingsItemType.PLUGIN.name()); + json.put("pluginId", getId()); + json.put("name", getName()); + json.put("Description", getDescription()); + + return json.toString(); + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java index db7f587d3d..c7e95494d4 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java @@ -22,6 +22,7 @@ import net.osmand.PlatformUtil; import net.osmand.access.AccessibilityPlugin; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.TabActivity.TabItem; +import net.osmand.plus.api.SettingsAPI; import net.osmand.plus.audionotes.AudioVideoNotesPlugin; import net.osmand.plus.dashboard.tools.DashFragmentData; import net.osmand.plus.development.OsmandDevelopmentPlugin; @@ -41,8 +42,12 @@ import net.osmand.plus.settings.BaseSettingsFragment; import net.osmand.plus.skimapsplugin.SkiMapsPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.plus.views.OsmandMapTileView; +import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import java.util.ArrayList; import java.util.Collection; @@ -55,6 +60,9 @@ public abstract class OsmandPlugin { public static final String PLUGIN_ID_KEY = "plugin_id"; + private static final String PLUGINS_PREFERENCES_NAME = "net.osmand.plugins"; + private static final String CUSTOM_PLUGINS_KEY = "custom_plugins"; + private static final Log LOG = PlatformUtil.getLog(OsmandPlugin.class); private static List allPlugins = new ArrayList(); @@ -243,6 +251,7 @@ public abstract class OsmandPlugin { allPlugins.add(new OsmEditingPlugin(app)); allPlugins.add(new OsmandDevelopmentPlugin(app)); + loadCustomPlugins(app); activatePlugins(app, enabledPlugins); } @@ -256,6 +265,51 @@ public abstract class OsmandPlugin { plugin.onInstall(app, activity); } initPlugin(app, plugin); + saveCustomPlugins(app); + } + + private static void loadCustomPlugins(@NonNull OsmandApplication app) { + SettingsAPI settingsAPI = app.getSettings().getSettingsAPI(); + Object pluginPrefs = settingsAPI.getPreferenceObject(PLUGINS_PREFERENCES_NAME); + String customPluginsJson = settingsAPI.getString(pluginPrefs, CUSTOM_PLUGINS_KEY, ""); + if (!Algorithms.isEmpty(customPluginsJson)) { + try { + JSONArray jArray = new JSONArray(customPluginsJson); + for (int i = 0; i < jArray.length(); i++) { + JSONObject json = jArray.getJSONObject(i); + + String pluginId = json.getString("pluginId"); + String name = json.getString("name"); + String description = json.getString("Description"); + + CustomOsmandPlugin plugin = new CustomOsmandPlugin(app); + plugin.pluginId = pluginId; + plugin.name = name; + plugin.description = description; + allPlugins.add(plugin); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + } + + private static void saveCustomPlugins(OsmandApplication app) { + List customOsmandPlugins = getCustomPlugins(); + if (!customOsmandPlugins.isEmpty()) { + SettingsAPI settingsAPI = app.getSettings().getSettingsAPI(); + Object pluginPrefs = settingsAPI.getPreferenceObject(PLUGINS_PREFERENCES_NAME); + JSONArray itemsJson = new JSONArray(); + for (CustomOsmandPlugin plugin : customOsmandPlugins) { + try { + String json = plugin.toJson(); + itemsJson.put(new JSONObject(json)); + } catch (JSONException e) { + e.printStackTrace(); + } + } + settingsAPI.edit(pluginPrefs).putString(CUSTOM_PLUGINS_KEY, itemsJson.toString()).commit(); + } } private static void activatePlugins(OsmandApplication app, Set enabledPlugins) { @@ -530,6 +584,16 @@ public abstract class OsmandPlugin { return lst; } + public static List getCustomPlugins() { + ArrayList lst = new ArrayList(allPlugins.size()); + for (OsmandPlugin plugin : allPlugins) { + if (plugin instanceof CustomOsmandPlugin) { + lst.add((CustomOsmandPlugin) plugin); + } + } + return lst; + } + @SuppressWarnings("unchecked") public static T getEnabledPlugin(Class clz) { for (OsmandPlugin lr : getEnabledPlugins()) { diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index 5f80b15e6a..bcefdda8d9 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -805,8 +805,12 @@ public class ImportHelper { final CustomOsmandPlugin plugin = pluginItem.getPlugin(); OsmandPlugin.addCustomPlugin(app, activity, plugin); - if (!Algorithms.isEmpty(pluginItem.getPluginItems())) { - app.getSettingsHelper().importSettings(file, pluginItem.getPluginItems(), "", 1, new SettingsHelper.SettingsImportListener() { + List pluginItems = pluginItem.getPluginItems(); + if (!Algorithms.isEmpty(pluginItems)) { + for (SettingsHelper.SettingsItem item : pluginItems) { + item.setShouldReplace(true); + } + app.getSettingsHelper().importSettings(file, pluginItems, "", 1, new SettingsHelper.SettingsImportListener() { @Override public void onSettingsImportFinished(boolean succeed, @NonNull List items) { app.showShortToastMessage(app.getString(R.string.file_imported_successfully, plugin.getName()));