use of net.osmand.ResultMatcher in project Osmand by osmandapp.
the class MapActivityLayers method selectMapLayer.
public void selectMapLayer(final OsmandMapTileView mapView, final ContextMenuItem it, final ArrayAdapter<ContextMenuItem> adapter) {
if (OsmandPlugin.getEnabledPlugin(OsmandRasterMapsPlugin.class) == null) {
Toast.makeText(activity, R.string.map_online_plugin_is_not_installed, Toast.LENGTH_LONG).show();
return;
}
final OsmandSettings settings = getApplication().getSettings();
final LinkedHashMap<String, String> entriesMap = new LinkedHashMap<>();
final String layerOsmVector = "LAYER_OSM_VECTOR";
final String layerInstallMore = "LAYER_INSTALL_MORE";
final String layerEditInstall = "LAYER_EDIT";
entriesMap.put(layerOsmVector, getString(R.string.vector_data));
entriesMap.putAll(settings.getTileSourceEntries());
entriesMap.put(layerInstallMore, getString(R.string.install_more));
entriesMap.put(layerEditInstall, getString(R.string.maps_define_edit));
final List<Entry<String, String>> entriesMapList = new ArrayList<>(entriesMap.entrySet());
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
String selectedTileSourceKey = settings.MAP_TILE_SOURCES.get();
int selectedItem = -1;
if (!settings.MAP_ONLINE_DATA.get()) {
selectedItem = 0;
} else {
Entry<String, String> selectedEntry = null;
for (Entry<String, String> entry : entriesMap.entrySet()) {
if (entry.getKey().equals(selectedTileSourceKey)) {
selectedEntry = entry;
break;
}
}
if (selectedEntry != null) {
selectedItem = 0;
entriesMapList.remove(selectedEntry);
entriesMapList.add(0, selectedEntry);
}
}
final String[] items = new String[entriesMapList.size()];
int i = 0;
for (Entry<String, String> entry : entriesMapList) {
items[i++] = entry.getValue();
}
builder.setSingleChoiceItems(items, selectedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String layerKey = entriesMapList.get(which).getKey();
switch(layerKey) {
case layerOsmVector:
settings.MAP_ONLINE_DATA.set(false);
updateMapSource(mapView, null);
it.setDescription(null);
adapter.notifyDataSetChanged();
break;
case layerEditInstall:
OsmandRasterMapsPlugin.defineNewEditLayer(activity, new ResultMatcher<TileSourceTemplate>() {
@Override
public boolean publish(TileSourceTemplate object) {
settings.MAP_TILE_SOURCES.set(object.getName());
settings.MAP_ONLINE_DATA.set(true);
if (it != null) {
it.setDescription(object.getName());
}
updateMapSource(mapView, settings.MAP_TILE_SOURCES);
return true;
}
@Override
public boolean isCancelled() {
return false;
}
});
break;
case layerInstallMore:
OsmandRasterMapsPlugin.installMapLayers(activity, new ResultMatcher<TileSourceTemplate>() {
TileSourceTemplate template = null;
int count = 0;
@Override
public boolean publish(TileSourceTemplate object) {
if (object == null) {
if (count == 1) {
settings.MAP_TILE_SOURCES.set(template.getName());
settings.MAP_ONLINE_DATA.set(true);
it.setDescription(template.getName());
adapter.notifyDataSetChanged();
updateMapSource(mapView, settings.MAP_TILE_SOURCES);
} else {
selectMapLayer(mapView, it, adapter);
}
} else {
count++;
template = object;
}
return false;
}
@Override
public boolean isCancelled() {
return false;
}
});
break;
default:
settings.MAP_TILE_SOURCES.set(layerKey);
settings.MAP_ONLINE_DATA.set(true);
it.setDescription(layerKey);
adapter.notifyDataSetChanged();
updateMapSource(mapView, settings.MAP_TILE_SOURCES);
break;
}
dialog.dismiss();
}
});
builder.setNegativeButton(R.string.shared_string_dismiss, null);
builder.show();
}
Aggregations