From 69c3510fc0ee4db3432bc9bcd835a2145539af20 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 17 Mar 2020 14:49:16 +0200 Subject: [PATCH] Save custom plugins second part --- .../net/osmand/plus/CustomOsmandPlugin.java | 113 ++++++++++++++++++ .../src/net/osmand/plus/SettingsHelper.java | 40 +++---- .../net/osmand/plus/helpers/ImportHelper.java | 8 +- 3 files changed, 137 insertions(+), 24 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java index a19b30e5ed..b84b212f3e 100644 --- a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java @@ -1,14 +1,22 @@ package net.osmand.plus; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + import net.osmand.map.ITileSource; +import net.osmand.osm.PoiCategory; import net.osmand.plus.helpers.AvoidSpecificRoads; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.quickaction.QuickAction; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashSet; import java.util.List; public class CustomOsmandPlugin extends OsmandPlugin { @@ -76,9 +84,114 @@ public class CustomOsmandPlugin extends OsmandPlugin { json.put("name", getName()); json.put("Description", getDescription()); + saveAdditionalItemsToJson(json); + return json.toString(); } + public void saveAdditionalItemsToJson(JSONObject json) throws JSONException { + if (!appModes.isEmpty()) { + List appModesKeys = new ArrayList<>(); + for (ApplicationMode mode : appModes) { + appModesKeys.add(mode.getStringKey()); + } + JSONArray appModesJson = new JSONArray(appModesKeys); + json.put("appModes", appModesJson); + } + if (!rendererNames.isEmpty()) { + JSONArray rendererNamesJson = new JSONArray(rendererNames); + json.put("rendererNames", rendererNamesJson); + } + if (!routerNames.isEmpty()) { + JSONArray rendererNamesJson = new JSONArray(routerNames); + json.put("routerNames", rendererNamesJson); + } + + savePoiUIFiltersToJson(json); + saveMapSourcesToJson(json); + saveQuickActionsToJson(json); + saveAvoidRoadsToJson(json); + } + + private void savePoiUIFiltersToJson(JSONObject json) throws JSONException { + if (!poiUIFilters.isEmpty()) { + JSONArray jsonArray = new JSONArray(); + Gson gson = new Gson(); + Type type = new TypeToken>>() { + }.getType(); + for (PoiUIFilter filter : poiUIFilters) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("name", filter.getName()); + jsonObject.put("filterId", filter.getFilterId()); + jsonObject.put("acceptedTypes", gson.toJson(filter.getAcceptedTypes(), type)); + jsonArray.put(jsonObject); + } + json.put("poiUIFilters", jsonArray); + } + } + + private void saveMapSourcesToJson(JSONObject json) throws JSONException { + if (!mapSources.isEmpty()) { + JSONArray jsonArray = new JSONArray(); + for (ITileSource template : mapSources) { + JSONObject jsonObject = new JSONObject(); + boolean sql = template instanceof SQLiteTileSource; + jsonObject.put("sql", sql); + jsonObject.put("name", template.getName()); + jsonObject.put("minZoom", template.getMinimumZoomSupported()); + jsonObject.put("maxZoom", template.getMaximumZoomSupported()); + jsonObject.put("url", template.getUrlTemplate()); + jsonObject.put("randoms", template.getRandoms()); + jsonObject.put("ellipsoid", template.isEllipticYTile()); + jsonObject.put("inverted_y", template.isInvertedYTile()); + jsonObject.put("referer", template.getReferer()); + jsonObject.put("timesupported", template.isTimeSupported()); + jsonObject.put("expire", template.getExpirationTimeMillis()); + jsonObject.put("inversiveZoom", template.getInversiveZoom()); + jsonObject.put("ext", template.getTileFormat()); + jsonObject.put("tileSize", template.getTileSize()); + jsonObject.put("bitDensity", template.getBitDensity()); + jsonObject.put("avgSize", template.getAvgSize()); + jsonObject.put("rule", template.getRule()); + jsonArray.put(jsonObject); + } + json.put("mapSources", jsonArray); + } + } + + private void saveAvoidRoadsToJson(JSONObject json) throws JSONException { + if (!avoidRoadInfos.isEmpty()) { + JSONArray jsonArray = new JSONArray(); + for (AvoidSpecificRoads.AvoidRoadInfo avoidRoad : avoidRoadInfos) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("latitude", avoidRoad.latitude); + jsonObject.put("longitude", avoidRoad.longitude); + jsonObject.put("name", avoidRoad.name); + jsonObject.put("appModeKey", avoidRoad.appModeKey); + jsonArray.put(jsonObject); + } + json.put("avoidRoadInfos", jsonArray); + } + } + + private void saveQuickActionsToJson(JSONObject json) throws JSONException { + if (!quickActions.isEmpty()) { + JSONArray jsonArray = new JSONArray(); + Gson gson = new Gson(); + Type type = new TypeToken>() { + }.getType(); + + for (QuickAction action : quickActions) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("name", action.hasCustomName(app) ? action.getName(app) : ""); + jsonObject.put("type", action.getType()); + jsonObject.put("params", gson.toJson(action.getParams(), type)); + jsonArray.put(jsonObject); + } + json.put("quickActions", jsonArray); + } + } + @Override public List getRendererNames() { return rendererNames; diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index 41321c54e5..3641afd60b 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -229,6 +229,7 @@ public class SettingsHelper { PluginSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { super(SettingsItemType.PLUGIN, json); this.app = app; + readFromJson(app, json); } @NonNull @@ -257,8 +258,7 @@ public class SettingsHelper { return pluginItems; } - @Override - void readFromJson(@NonNull JSONObject json) throws JSONException { + void readFromJson(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { String pluginId = json.getString("pluginId"); String name = json.getString("name"); String description = json.getString("Description"); @@ -554,6 +554,7 @@ public class SettingsHelper { public ProfileSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { super(SettingsItemType.PROFILE, app.getSettings(), json); this.app = app; + readFromJson(app, json); appModeBeanPrefsIds = new HashSet<>(Arrays.asList(app.getSettings().appModeBeanPrefsIds)); } @@ -589,8 +590,7 @@ public class SettingsHelper { return "profile_" + getName() + ".json"; } - void readFromJson(@NonNull JSONObject json) throws JSONException { - super.readFromJson(json); + void readFromJson(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { String appModeJson = json.getString("appMode"); modeBean = ApplicationMode.fromJson(appModeJson); builder = ApplicationMode.fromModeBean(app, modeBean); @@ -1652,27 +1652,27 @@ public class SettingsHelper { Map> pluginItems = new HashMap<>(); for (int i = 0; i < itemsJson.length(); i++) { JSONObject itemJson = itemsJson.getJSONObject(i); - SettingsItem item = null; + SettingsItem item; try { item = createItem(itemJson); + if (item != null) { + items.add(item); + + if (itemJson.has("pluginId") && item.type != SettingsItemType.PLUGIN) { + String pluginId = itemJson.getString("pluginId"); + List items = pluginItems.get(pluginId); + if (items != null) { + items.add(item); + } else { + items = new ArrayList<>(); + items.add(item); + pluginItems.put(pluginId, items); + } + } + } } catch (IllegalArgumentException e) { LOG.error("Error creating item from json: " + itemJson, e); } - if (item != null) { - if (itemJson.has("pluginId") && item.type != SettingsItemType.PLUGIN) { - String pluginId = itemJson.getString("pluginId"); - List items = pluginItems.get(pluginId); - if (items != null) { - items.add(item); - } else { - items = new ArrayList<>(); - items.add(item); - pluginItems.put(pluginId, items); - } - } else { - items.add(item); - } - } } if (items.size() == 0) { throw new IllegalArgumentException("No items"); diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index bc64dd8557..73236bf546 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -802,9 +802,7 @@ public class ImportHelper { } } for (SettingsHelper.PluginSettingsItem pluginItem : pluginSettingsItems) { - final CustomOsmandPlugin plugin = pluginItem.getPlugin(); - OsmandPlugin.addCustomPlugin(app, activity, plugin); - + CustomOsmandPlugin plugin = pluginItem.getPlugin(); List pluginItems = pluginItem.getPluginItems(); if (!Algorithms.isEmpty(pluginItems)) { for (SettingsHelper.SettingsItem item : pluginItems) { @@ -825,10 +823,12 @@ public class ImportHelper { plugin.appModes.add(((SettingsHelper.ProfileSettingsItem) item).getAppMode()); } } + + OsmandPlugin.addCustomPlugin(app, activity, plugin); 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())); + app.showShortToastMessage(app.getString(R.string.file_imported_successfully, "")); } }); }