From 2390c95a1b4319ef6187544be0bdd83e0a3fb738 Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Fri, 22 Apr 2016 23:31:29 +0300 Subject: [PATCH] POI multiselection: combine standard filters for faster filtering --- .../plus/activities/MapActivityLayers.java | 3 ++ .../net/osmand/plus/poi/PoiFiltersHelper.java | 36 +++++++++++++----- .../src/net/osmand/plus/poi/PoiUIFilter.java | 37 ++++++++++++++----- .../net/osmand/plus/views/POIMapLayer.java | 1 + 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java index f84f1a87ad..4f2cb6c9aa 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java @@ -271,6 +271,9 @@ public class MapActivityLayers { for (PoiUIFilter f : poiFilters.getTopDefinedPoiFilters()) { addFilterToList(adapter, list, f); } + for (PoiUIFilter f : poiFilters.getSearchPoiFilters()) { + addFilterToList(adapter, list, f); + } AlertDialog.Builder builder = new AlertDialog.Builder(activity); ListAdapter listAdapter = adapter.createListAdapter(activity, app.getSettings().isLightContent()); diff --git a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java index dd8bbf917c..ac806908f3 100644 --- a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java +++ b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java @@ -15,8 +15,10 @@ import net.osmand.util.Algorithms; import android.support.annotation.NonNull; import android.util.ArraySet; +import java.lang.reflect.Array; import java.text.Collator; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; @@ -126,7 +128,7 @@ public class PoiFiltersHelper { } return null; } - + public PoiUIFilter getFilterById(String filterId){ if (filterId == null){ return null; @@ -182,13 +184,23 @@ public class PoiFiltersHelper { } return userDefinedFilters; } - + + public List getSearchPoiFilters() { + List result = new ArrayList<>(); + List filters = Arrays.asList(getCustomPOIFilter(), // getShowAllPOIFilter(), + getSearchByNamePOIFilter(), getNominatimPOIFilter(), getNominatimAddressFilter()); + for (PoiUIFilter f : filters) + if (f != null && !f.isEmpty()) + result.add(f); + return result; + } + public List getTopDefinedPoiFilters() { if (cacheTopStandardFilters == null) { List top = new ArrayList(); // user defined top.addAll(getUserDefinedPoiFilters()); - if(getLocalWikiPOIFilter() != null) { + if (getLocalWikiPOIFilter() != null) { top.add(getLocalWikiPOIFilter()); } // default @@ -294,14 +306,20 @@ public class PoiFiltersHelper { saveSelectedPoiFilters(); } - public String getSelectedPoiFiltersName() { - if (!isShowingAnyPoi()) { + public String getFiltersName(Set filters) { + if (filters.isEmpty()) { return application.getResources().getString(R.string.shared_string_none); } - List names = new ArrayList(); - for (PoiUIFilter filter : selectedPoiFilters) - names.add(filter.getName()); - return android.text.TextUtils.join(", ", names); + else { + List names = new ArrayList<>(); + for (PoiUIFilter filter : filters) + names.add(filter.getName()); + return android.text.TextUtils.join(", ", names); + } + } + + public String getSelectedPoiFiltersName() { + return getFiltersName(selectedPoiFilters); } public boolean isPoiFilterSelected(PoiUIFilter filter) { diff --git a/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java b/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java index 616ec2807d..ed02927d67 100644 --- a/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java +++ b/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java @@ -11,7 +11,9 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Set; +import java.util.TreeSet; import net.osmand.CollatorStringMatcher; import net.osmand.CollatorStringMatcher.StringMatcherMode; @@ -353,8 +355,7 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable public String getNameTokenOpen() { return app.getString(R.string.shared_string_open); } - - + private ResultMatcher wrapResultMatcher(final ResultMatcher matcher) { final AmenityNameFilter nm = getNameFilter(filterByName); return new ResultMatcher() { @@ -488,14 +489,31 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable } } - public void replaceWithPoiFilter(PoiUIFilter f) { - acceptedTypes.clear(); + public void combineWithPoiFilter(PoiUIFilter f) { acceptedTypes.putAll(f.acceptedTypes); - poiAdditionals.clear(); poiAdditionals.putAll(f.poiAdditionals); } - - + + public static void combineStandardPoiFilters(Set filters, OsmandApplication app) { + Set standardFilters = new TreeSet<>(); + for (PoiUIFilter filter : filters) + if (filter.isStandardFilter() && filter.filterId.startsWith(PoiUIFilter.STD_PREFIX)) + standardFilters.add(filter); + if (!standardFilters.isEmpty()) { + PoiUIFilter standardFiltersCombined = new PoiUIFilter( + null, app, app.getPoiFilters().getFiltersName(standardFilters)); + for (PoiUIFilter f : standardFilters) + standardFiltersCombined.combineWithPoiFilter(f); + filters.removeAll(standardFilters); + filters.add(standardFiltersCombined); + } + } + + public void replaceWithPoiFilter(PoiUIFilter f) { + clearFilter(); + combineWithPoiFilter(f); + } + public Map> getAcceptedTypes(){ return new LinkedHashMap>(acceptedTypes); } @@ -563,7 +581,8 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable @Override public boolean isEmpty() { - return acceptedTypes.isEmpty(); + return acceptedTypes.isEmpty() && + (currentSearchResult == null || currentSearchResult.isEmpty()); } @Override @@ -579,6 +598,4 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable public boolean accept(Amenity a) ; } - - } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java b/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java index 02036a3f68..9591601f51 100644 --- a/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java @@ -122,6 +122,7 @@ public class POIMapLayer extends OsmandMapLayer implements ContextMenuLayer.ICon int z = (int) Math.floor(tileBox.getZoom() + Math.log(view.getSettings().MAP_DENSITY.get()) / Math.log(2)); List res = new ArrayList<>(); + PoiUIFilter.combineStandardPoiFilters(filters, app); for (PoiUIFilter filter : filters) res.addAll(filter.searchAmenities(latLonBounds.top, latLonBounds.left, latLonBounds.bottom, latLonBounds.right, z, new ResultMatcher() {