From 21bcfd5c538114c0c94c89f085086d006a5ec1d2 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 9 Apr 2020 16:19:14 +0300 Subject: [PATCH] Read suggested downloads --- .../net/osmand/plus/CustomOsmandPlugin.java | 58 ++++++- .../src/net/osmand/plus/SettingsHelper.java | 147 ++++++++++++++++-- 2 files changed, 187 insertions(+), 18 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java index 88c9e53042..6d855f06f0 100644 --- a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java @@ -20,6 +20,10 @@ import net.osmand.plus.SettingsHelper.ProfileSettingsItem; import net.osmand.plus.SettingsHelper.QuickActionsSettingsItem; import net.osmand.plus.SettingsHelper.SettingsCollectListener; import net.osmand.plus.SettingsHelper.SettingsItem; +import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.DownloadIndexesThread; +import net.osmand.plus.download.DownloadResources; +import net.osmand.plus.download.IndexItem; import net.osmand.plus.helpers.AvoidSpecificRoads; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.quickaction.QuickAction; @@ -56,6 +60,7 @@ public class CustomOsmandPlugin extends OsmandPlugin { private List rendererNames = new ArrayList<>(); private List routerNames = new ArrayList<>(); + private List suggestedDownloadItems = new ArrayList<>(); public CustomOsmandPlugin(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { super(app); @@ -93,9 +98,7 @@ public class CustomOsmandPlugin extends OsmandPlugin { ApplicationMode.changeProfileAvailability(savedMode, true, app); } iterator.remove(); - } else if (item instanceof PluginSettingsItem) { - iterator.remove(); - } else { + } else if (!(item instanceof PluginSettingsItem)) { item.setShouldReplace(true); } } @@ -374,12 +377,16 @@ public class CustomOsmandPlugin extends OsmandPlugin { public void addRouter(String fileName) { String routerName = Algorithms.getFileWithoutDirs(fileName); - routerNames.add(routerName); + if (!routerNames.contains(routerName)) { + routerNames.add(routerName); + } } public void addRenderer(String fileName) { String rendererName = Algorithms.getFileWithoutDirs(fileName); - rendererNames.add(rendererName); + if (!rendererNames.contains(rendererName)) { + rendererNames.add(rendererName); + } } public void loadResources() { @@ -422,9 +429,50 @@ public class CustomOsmandPlugin extends OsmandPlugin { return image; } + public void updateSuggestedDownloads(List items) { + suggestedDownloadItems = new ArrayList<>(items); + } + + @Override + public List getSuggestedMaps() { + List suggestedMaps = new ArrayList<>(); + return suggestedMaps; + } + public interface PluginItemsListener { void onItemsRemoved(); } + + public static class SuggestedDownloadItem { + + private String scopeId; + private String searchType; + private List names; + private int limit; + + public SuggestedDownloadItem(String scopeId, String searchType, List names, int limit) { + this.scopeId = scopeId; + this.limit = limit; + this.searchType = searchType; + this.names = names; + } + + public String getScopeId() { + return scopeId; + } + + public String getSearchType() { + return searchType; + } + + public List getNames() { + return names; + } + + public int getLimit() { + return limit; + } + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index 3a5da6894c..2ff27bbb65 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -20,6 +20,7 @@ import net.osmand.osm.MapPoiTypes; import net.osmand.osm.PoiCategory; import net.osmand.plus.ApplicationMode.ApplicationModeBean; import net.osmand.plus.ApplicationMode.ApplicationModeBuilder; +import net.osmand.plus.CustomOsmandPlugin.SuggestedDownloadItem; import net.osmand.plus.OsmandSettings.OsmandPreference; import net.osmand.plus.helpers.AvoidSpecificRoads; import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo; @@ -131,7 +132,8 @@ public class SettingsHelper { QUICK_ACTIONS, POI_UI_FILTERS, MAP_SOURCES, - AVOID_ROADS + AVOID_ROADS, + SUGGESTED_DOWNLOADS } public abstract static class SettingsItem { @@ -400,20 +402,27 @@ public class SettingsHelper { @Override public void apply() { if (shouldReplace || !exists()) { - for (SettingsHelper.SettingsItem item : pluginDependentItems) { - if (item instanceof SettingsHelper.FileSettingsItem) { - FileSettingsItem fileItem = (FileSettingsItem) item; - if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.RENDERING_STYLE) { - plugin.addRenderer(fileItem.getName()); - } else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.ROUTING_CONFIG) { - plugin.addRouter(fileItem.getName()); - } else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.OTHER) { - plugin.setResourceDirName(item.getFileName()); - } - } - } OsmandPlugin.addCustomPlugin(app, plugin); } + CustomOsmandPlugin customPlugin = (CustomOsmandPlugin) OsmandPlugin.getPlugin(getPluginId()); + updatePluginItems(customPlugin); + } + + private void updatePluginItems(CustomOsmandPlugin customPlugin) { + for (SettingsHelper.SettingsItem item : pluginDependentItems) { + if (item instanceof SettingsHelper.FileSettingsItem) { + FileSettingsItem fileItem = (FileSettingsItem) item; + if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.RENDERING_STYLE) { + customPlugin.addRenderer(fileItem.getName()); + } else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.ROUTING_CONFIG) { + customPlugin.addRouter(fileItem.getName()); + } else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.OTHER) { + customPlugin.setResourceDirName(item.getFileName()); + } + } else if (item instanceof SuggestedDownloadsItem) { + customPlugin.updateSuggestedDownloads(((SuggestedDownloadsItem) item).items); + } + } } @Override @@ -441,6 +450,115 @@ public class SettingsHelper { } } + public static class SuggestedDownloadsItem extends SettingsItem { + + private List items; + + SuggestedDownloadsItem(@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.SUGGESTED_DOWNLOADS; + + } + + @NonNull + @Override + public String getName() { + return "suggested_downloads"; + } + + @NonNull + @Override + public String getPublicName(@NonNull Context ctx) { + return "suggested_downloads"; + } + + 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"); + String searchType = object.optString("search-type"); + int limit = object.optInt("limit", -1); + + List names = new ArrayList<>(); + if (object.has("names")) { + JSONArray namesArray = object.getJSONArray("names"); + for (int j = 0; j < namesArray.length(); j++) { + names.add(namesArray.getString(j)); + } + } + SuggestedDownloadItem suggestedDownload = new SuggestedDownloadItem(scopeId, searchType, names, limit); + items.add(suggestedDownload); + } + } 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 (SuggestedDownloadItem downloadItem : items) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("scope-id", downloadItem.getScopeId()); + if (downloadItem.getLimit() != -1) { + jsonObject.put("limit", downloadItem.getLimit()); + } + if (!Algorithms.isEmpty(downloadItem.getSearchType())) { + jsonObject.put("search-type", downloadItem.getSearchType()); + } + if (!Algorithms.isEmpty(downloadItem.getNames())) { + JSONArray namesArray = new JSONArray(); + for (String downloadName : downloadItem.getNames()) { + namesArray.put(downloadName); + } + jsonObject.put("names", namesArray); + } + 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 abstract static class CollectionSettingsItem extends SettingsItem { protected List items; @@ -2086,6 +2204,9 @@ public class SettingsHelper { case AVOID_ROADS: item = new AvoidRoadsSettingsItem(app, json); break; + case SUGGESTED_DOWNLOADS: + item = new SuggestedDownloadsItem(app, json); + break; } return item; }