From 5de11ab32b6d28a5c48b7dae08d9cf6f8093f209 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 10 Apr 2020 19:24:46 +0300 Subject: [PATCH] Add custom downloads to settings helper --- .../net/osmand/plus/CustomOsmandPlugin.java | 11 + OsmAnd/src/net/osmand/plus/OsmandPlugin.java | 5 + .../src/net/osmand/plus/SettingsHelper.java | 213 +++++++++++++++++- 3 files changed, 228 insertions(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java index b7b64ddbf0..99cc07868b 100644 --- a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java @@ -14,6 +14,7 @@ import net.osmand.data.LatLon; import net.osmand.map.ITileSource; import net.osmand.map.TileSourceManager; import net.osmand.plus.SettingsHelper.AvoidRoadsSettingsItem; +import net.osmand.plus.SettingsHelper.DownloadDataContainer; import net.osmand.plus.SettingsHelper.MapSourcesSettingsItem; import net.osmand.plus.SettingsHelper.PluginSettingsItem; import net.osmand.plus.SettingsHelper.PoiUiFilterSettingsItem; @@ -64,6 +65,7 @@ public class CustomOsmandPlugin extends OsmandPlugin { private List rendererNames = new ArrayList<>(); private List routerNames = new ArrayList<>(); private List suggestedDownloadItems = new ArrayList<>(); + private List downloadDataContainers = new ArrayList<>(); public CustomOsmandPlugin(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { super(app); @@ -436,6 +438,15 @@ public class CustomOsmandPlugin extends OsmandPlugin { suggestedDownloadItems = new ArrayList<>(items); } + public void updateDownloadItems(List items) { + downloadDataContainers = new ArrayList<>(items); + } + + @Override + public List getDownloadMaps() { + return downloadDataContainers; + } + @Override public List getSuggestedMaps() { List suggestedMaps = new ArrayList<>(); diff --git a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java index a386303aba..59b69ca509 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java @@ -44,6 +44,7 @@ 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.plus.SettingsHelper.DownloadDataContainer; import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -182,6 +183,10 @@ public abstract class OsmandPlugin { return Collections.emptyList(); } + public List getDownloadMaps() { + return Collections.emptyList(); + } + public List getRendererNames() { return Collections.emptyList(); } diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index b70db7d894..138c1e2f4f 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -133,7 +133,8 @@ public class SettingsHelper { POI_UI_FILTERS, MAP_SOURCES, AVOID_ROADS, - SUGGESTED_DOWNLOADS + SUGGESTED_DOWNLOADS, + DOWNLOADS } public abstract static class SettingsItem { @@ -414,6 +415,8 @@ public class SettingsHelper { } } else if (item instanceof SuggestedDownloadsItem) { plugin.updateSuggestedDownloads(((SuggestedDownloadsItem) item).getItems()); + } else if (item instanceof DownloadsItem) { + plugin.updateDownloadItems(((DownloadsItem) item).getItems()); } } OsmandPlugin.addCustomPlugin(app, plugin); @@ -554,6 +557,211 @@ public class SettingsHelper { } } + public static class DownloadsItem extends SettingsItem { + + private List items; + + DownloadsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { + super(app, json); + } + + @Override + protected void init() { + items = new ArrayList<>(); + } + + @NonNull + @Override + public SettingsItemType getType() { + return SettingsItemType.DOWNLOADS; + + } + + @NonNull + @Override + public String getName() { + return "downloads"; + } + + @NonNull + @Override + public String getPublicName(@NonNull Context ctx) { + return "downloads"; + } + + @Override + public void apply() { + super.apply(); + } + + public List getItems() { + return items; + } + + @Override + void readItemsFromJson(@NonNull JSONObject json) throws IllegalArgumentException { + try { + if (!json.has("items")) { + return; + } + JSONArray jsonArray = json.getJSONArray("items"); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject object = jsonArray.getJSONObject(i); + String scopeId = object.optString("scope-id", null); + String path = object.optString("path", null); + String type = object.optString("type", null); + String folder = object.optString("folder", null); + + JSONObject namesJson = object.optJSONObject("name"); + Map names = new HashMap<>(); + if (namesJson != null) { + for (Iterator it = namesJson.keys(); it.hasNext(); ) { + String localeKey = it.next(); + String name = namesJson.getString(localeKey); + names.put(localeKey, name); + } + } + + JSONObject iconJson = object.optJSONObject("icon"); + Map icons = new HashMap<>(); + if (iconJson != null) { + for (Iterator it = iconJson.keys(); it.hasNext(); ) { + String localeKey = it.next(); + String name = iconJson.getString(localeKey); + icons.put(localeKey, name); + } + } + + JSONObject headerJson = object.optJSONObject("header"); + Map headers = new HashMap<>(); + if (headerJson != null) { + for (Iterator it = headerJson.keys(); it.hasNext(); ) { + String localeKey = it.next(); + String name = headerJson.getString(localeKey); + headers.put(localeKey, name); + } + } + + String headerButton = object.optString("header-button", null); + + JSONArray downloadItemsArray = object.optJSONArray("items"); + + DownloadDataContainer dataContainer = new DownloadDataContainer(scopeId, path, type, folder, downloadItemsArray, names, icons, headers, headerButton); + + items.add(dataContainer); + } + } catch (JSONException e) { + warnings.add(app.getString(R.string.settings_item_read_error, String.valueOf(getType()))); + throw new IllegalArgumentException("Json parse error", e); + } + } + + @Override + void writeItemsToJson(@NonNull JSONObject json) { + JSONArray jsonArray = new JSONArray(); + if (!items.isEmpty()) { + try { + for (DownloadDataContainer downloadItem : items) { + JSONObject jsonObject = new JSONObject(); + + if (!Algorithms.isEmpty(downloadItem.scopeId)) { + jsonObject.put("scope-id", downloadItem.scopeId); + } + if (!Algorithms.isEmpty(downloadItem.path)) { + jsonObject.put("path", downloadItem.path); + } + if (!Algorithms.isEmpty(downloadItem.type)) { + jsonObject.put("type", downloadItem.type); + } + if (!Algorithms.isEmpty(downloadItem.folder)) { + jsonObject.put("folder", downloadItem.folder); + } + if (!Algorithms.isEmpty(downloadItem.headerButton)) { + jsonObject.put("header-button", downloadItem.headerButton); + } + + if (!Algorithms.isEmpty(downloadItem.names)) { + JSONObject namesJson = new JSONObject(); + for (Map.Entry entry : downloadItem.names.entrySet()) { + namesJson.put(entry.getKey(), entry.getValue()); + } + jsonObject.put("name", namesJson); + } + + if (!Algorithms.isEmpty(downloadItem.icons)) { + JSONObject iconsJson = new JSONObject(); + for (Map.Entry entry : downloadItem.icons.entrySet()) { + iconsJson.put(entry.getKey(), entry.getValue()); + } + jsonObject.put("icon", iconsJson); + } + + if (!Algorithms.isEmpty(downloadItem.headers)) { + JSONObject headerJson = new JSONObject(); + for (Map.Entry entry : downloadItem.headers.entrySet()) { + headerJson.put(entry.getKey(), entry.getValue()); + } + jsonObject.put("header", headerJson); + } + + if (downloadItem.downloadItemsArray != null) { + jsonObject.put("items", downloadItem.downloadItemsArray); + } + + jsonArray.put(jsonObject); + } + json.put("items", jsonArray); + } catch (JSONException e) { + warnings.add(app.getString(R.string.settings_item_write_error, String.valueOf(getType()))); + LOG.error("Failed write to json", e); + } + } + } + + + @Nullable + @Override + SettingsItemReader getReader() { + return null; + } + + @Nullable + @Override + SettingsItemWriter getWriter() { + return null; + } + } + + public static class DownloadDataContainer { + + String scopeId; + String path; + String type; + String folder; + String headerButton; + JSONArray downloadItemsArray; + Map names; + Map icons; + Map headers; + + public DownloadDataContainer(String scopeId, String path, String type, String folder, + JSONArray downloadItemsArray, + Map names, + Map icons, + Map headers, + String headerButton) { + this.scopeId = scopeId; + this.path = path; + this.type = type; + this.folder = folder; + this.downloadItemsArray = downloadItemsArray; + this.names = names; + this.icons = icons; + this.headers = headers; + this.headerButton = headerButton; + } + } + public abstract static class CollectionSettingsItem extends SettingsItem { protected List items; @@ -2202,6 +2410,9 @@ public class SettingsHelper { case SUGGESTED_DOWNLOADS: item = new SuggestedDownloadsItem(app, json); break; + case DOWNLOADS: + item = new DownloadsItem(app, json); + break; } return item; }