Search in sources :

Example 11 with ResultMatcher

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();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) TileSourceTemplate(net.osmand.map.TileSourceManager.TileSourceTemplate) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) ResultMatcher(net.osmand.ResultMatcher) OsmandSettings(net.osmand.plus.OsmandSettings) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) OsmandRasterMapsPlugin(net.osmand.plus.rastermaps.OsmandRasterMapsPlugin)

Aggregations

ResultMatcher (net.osmand.ResultMatcher)11 ArrayList (java.util.ArrayList)7 TIntArrayList (gnu.trove.list.array.TIntArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 City (net.osmand.data.City)3 DialogInterface (android.content.DialogInterface)2 Message (android.os.Message)2 AlertDialog (android.support.v7.app.AlertDialog)2 LinkedHashMap (java.util.LinkedHashMap)2 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)2 TagValuePair (net.osmand.binary.BinaryMapIndexReader.TagValuePair)2 MapObject (net.osmand.data.MapObject)2 QuadRect (net.osmand.data.QuadRect)2 Street (net.osmand.data.Street)2 TileSourceTemplate (net.osmand.map.TileSourceManager.TileSourceTemplate)2 OsmandSettings (net.osmand.plus.OsmandSettings)2 Paint (android.graphics.Paint)1 Path (android.graphics.Path)1 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)1