Read suggested downloads

This commit is contained in:
Vitaliy 2020-04-09 16:19:14 +03:00
parent 0c7749d73d
commit 21bcfd5c53
2 changed files with 187 additions and 18 deletions

View file

@ -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<String> rendererNames = new ArrayList<>();
private List<String> routerNames = new ArrayList<>();
private List<SuggestedDownloadItem> 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<SuggestedDownloadItem> items) {
suggestedDownloadItems = new ArrayList<>(items);
}
@Override
public List<IndexItem> getSuggestedMaps() {
List<IndexItem> suggestedMaps = new ArrayList<>();
return suggestedMaps;
}
public interface PluginItemsListener {
void onItemsRemoved();
}
public static class SuggestedDownloadItem {
private String scopeId;
private String searchType;
private List<String> names;
private int limit;
public SuggestedDownloadItem(String scopeId, String searchType, List<String> 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<String> getNames() {
return names;
}
public int getLimit() {
return limit;
}
}
}

View file

@ -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<SuggestedDownloadItem> 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<SuggestedDownloadItem> 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<String> 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<T> extends SettingsItem {
protected List<T> 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;
}