From 5cd64b816908d34546caa298e22c78ada599cdbc Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Mon, 23 Oct 2017 17:10:39 +0300 Subject: [PATCH 1/7] Start integrating online search in quick search --- .../net/osmand/search/core/ObjectType.java | 2 + .../search/QuickSearchDialogFragment.java | 21 ++++++++++ .../osmand/plus/search/QuickSearchHelper.java | 39 +++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/OsmAnd-java/src/net/osmand/search/core/ObjectType.java b/OsmAnd-java/src/net/osmand/search/core/ObjectType.java index a69dbd03e2..83378d47b5 100644 --- a/OsmAnd-java/src/net/osmand/search/core/ObjectType.java +++ b/OsmAnd-java/src/net/osmand/search/core/ObjectType.java @@ -9,6 +9,8 @@ public enum ObjectType { LOCATION(true), PARTIAL_LOCATION(false), // UI OBJECTS FAVORITE(true), FAVORITE_GROUP(false), WPT(true), RECENT_OBJ(true), + // ONLINE + ONLINE_POI(true), ONLINE_ADDRESS(true), REGION(true), diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index 18568a0bc8..8e921176ee 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -1079,6 +1079,16 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC for (SearchResult sr : res.getCurrentSearchResults()) { rows.add(new QuickSearchListItem(app, sr)); } + rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_world_globe_dark, + app.getString(R.string.search_online_address), new OnClickListener() { + @Override + public void onClick(View view) { + startOnlinePoiSearch(); + mainSearchFragment.getAdapter().clear(); + updateTabbarVisibility(false); + openKeyboard(); + } + })); rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_action_search_dark, app.getString(R.string.custom_search), new OnClickListener() { @Override @@ -1320,6 +1330,17 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC } } + private void startOnlinePoiSearch() { + SearchSettings settings = searchUICore.getSearchSettings() + .setSearchTypes(ObjectType.ONLINE_POI) + .setEmptyQueryAllowed(false) + .setAddressSearch(false) + .setSortByName(true) + .setRadiusLevel(1); + + searchUICore.updateSettings(settings); + } + private void startAddressSearch() { SearchSettings settings = searchUICore.getSearchSettings() .setEmptyQueryAllowed(true) diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java index 88947203f9..ca2a8d1291 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java @@ -1,6 +1,7 @@ package net.osmand.plus.search; import net.osmand.binary.BinaryMapIndexReader; +import net.osmand.data.Amenity; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.plus.FavouritesDbHelper; @@ -10,6 +11,7 @@ import net.osmand.plus.GpxSelectionHelper; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.helpers.SearchHistoryHelper; +import net.osmand.plus.poi.NominatimPoiFilter; import net.osmand.plus.poi.PoiFiltersHelper; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.resources.ResourceManager.ResourceListener; @@ -36,6 +38,7 @@ public class QuickSearchHelper implements ResourceListener { public static final int SEARCH_WPT_OBJECT_PRIORITY = 52; public static final int SEARCH_HISTORY_API_PRIORITY = 50; public static final int SEARCH_HISTORY_OBJECT_PRIORITY = 53; + public static final int SEARCH_ONLINE_PRIORITY = 53; private OsmandApplication app; private SearchUICore core; private SearchResultCollection resultCollection; @@ -78,6 +81,8 @@ public class QuickSearchHelper implements ResourceListener { core.registerAPI(new SearchWptAPI(app)); core.registerAPI(new SearchHistoryAPI(app)); + core.registerAPI(new SearchOnlineApi(app)); + refreshCustomPoiFilters(); } @@ -266,6 +271,40 @@ public class QuickSearchHelper implements ResourceListener { } } + public static class SearchOnlineApi extends SearchBaseAPI { + + private NominatimPoiFilter nominatimPoiFilter; + private OsmandApplication app; + + public SearchOnlineApi(OsmandApplication app) { + super(ObjectType.ONLINE_POI); + this.nominatimPoiFilter = app.getPoiFilters().getNominatimPOIFilter(); + this.app = app; + } + + @Override + public boolean search(SearchPhrase phrase, SearchUICore.SearchResultMatcher resultMatcher) throws IOException { + List amenities = nominatimPoiFilter.initializeNewSearch(app.getSettings().getLastKnownMapLocation().getLatitude(), app.getSettings().getLastKnownMapLocation().getLongitude(), -1, null); + int p = 0; + for (Amenity amenity : amenities) { + SearchResult sr = new SearchResult(phrase); + sr.localeName = amenity.getName(); + sr.object = amenity; + sr.priority = SEARCH_HISTORY_OBJECT_PRIORITY + (p++); + sr.objectType = ObjectType.RECENT_OBJ; + sr.location = amenity.getLocation(); + sr.preferredZoom = 17; + if (phrase.getUnknownSearchWordLength() <= 1 && phrase.isNoSelectedType()) { + resultMatcher.publish(sr); + } else if (phrase.getNameStringMatcher().matches(sr.localeName)) { + resultMatcher.publish(sr); + } + } + return true; + } + + } + public static class SearchHistoryAPI extends SearchBaseAPI { private OsmandApplication app; From 35e9fe255d3008b0d9bd9d7f8bbce6d74d258b9c Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Mon, 23 Oct 2017 17:37:32 +0300 Subject: [PATCH 2/7] Add address filter --- .../src/net/osmand/plus/search/QuickSearchHelper.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java index ca2a8d1291..c26a46962f 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java @@ -25,6 +25,7 @@ import net.osmand.search.core.SearchResult; import net.osmand.util.Algorithms; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -274,24 +275,28 @@ public class QuickSearchHelper implements ResourceListener { public static class SearchOnlineApi extends SearchBaseAPI { private NominatimPoiFilter nominatimPoiFilter; + private NominatimPoiFilter nominatimAddressFilter; private OsmandApplication app; public SearchOnlineApi(OsmandApplication app) { super(ObjectType.ONLINE_POI); this.nominatimPoiFilter = app.getPoiFilters().getNominatimPOIFilter(); + this.nominatimAddressFilter = app.getPoiFilters().getNominatimAddressFilter(); this.app = app; } @Override public boolean search(SearchPhrase phrase, SearchUICore.SearchResultMatcher resultMatcher) throws IOException { - List amenities = nominatimPoiFilter.initializeNewSearch(app.getSettings().getLastKnownMapLocation().getLatitude(), app.getSettings().getLastKnownMapLocation().getLongitude(), -1, null); + List amenities = new ArrayList<>(); + amenities.addAll(nominatimPoiFilter.initializeNewSearch(app.getSettings().getLastKnownMapLocation().getLatitude(), app.getSettings().getLastKnownMapLocation().getLongitude(), -1, null)); + amenities.addAll(nominatimAddressFilter.initializeNewSearch(app.getSettings().getLastKnownMapLocation().getLatitude(), app.getSettings().getLastKnownMapLocation().getLongitude(), -1, null)); int p = 0; for (Amenity amenity : amenities) { SearchResult sr = new SearchResult(phrase); sr.localeName = amenity.getName(); sr.object = amenity; - sr.priority = SEARCH_HISTORY_OBJECT_PRIORITY + (p++); - sr.objectType = ObjectType.RECENT_OBJ; + sr.priority = SEARCH_ONLINE_PRIORITY + (p++); + sr.objectType = ObjectType.ONLINE_POI; sr.location = amenity.getLocation(); sr.preferredZoom = 17; if (phrase.getUnknownSearchWordLength() <= 1 && phrase.isNoSelectedType()) { From a3e2982697b249379a23ef4edee4b231e3156389 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Tue, 24 Oct 2017 12:15:29 +0300 Subject: [PATCH 3/7] Fix NPE --- .../osmand/plus/search/QuickSearchHelper.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java index c26a46962f..9a07a80b0e 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java @@ -47,11 +47,11 @@ public class QuickSearchHelper implements ResourceListener { public QuickSearchHelper(OsmandApplication app) { this.app = app; - core = new SearchUICore(app.getPoiTypes(), app.getSettings().MAP_PREFERRED_LOCALE.get(), + core = new SearchUICore(app.getPoiTypes(), app.getSettings().MAP_PREFERRED_LOCALE.get(), app.getSettings().MAP_TRANSLITERATE_NAMES.get()); app.getResourceManager().addResourceListener(this); } - + public SearchUICore getCore() { if (mapsIndexed) { mapsIndexed = false; @@ -90,7 +90,7 @@ public class QuickSearchHelper implements ResourceListener { public void refreshCustomPoiFilters() { core.clearCustomSearchPoiFilters(); PoiFiltersHelper poiFilters = app.getPoiFilters(); - for(CustomSearchPoiFilter udf : poiFilters.getUserDefinedPoiFilters()) { + for (CustomSearchPoiFilter udf : poiFilters.getUserDefinedPoiFilters()) { core.addCustomSearchPoiFilter(udf, 0); } PoiUIFilter localWikiPoiFilter = poiFilters.getLocalWikiPOIFilter(); @@ -113,7 +113,7 @@ public class QuickSearchHelper implements ResourceListener { super(ObjectType.WPT); this.app = app; } - + @Override public boolean isSearchMoreAvailable(SearchPhrase phrase) { return false; @@ -151,7 +151,7 @@ public class QuickSearchHelper implements ResourceListener { @Override public int getSearchPriority(SearchPhrase p) { - if(!p.isNoSelectedType()) { + if (!p.isNoSelectedType()) { return -1; } return SEARCH_WPT_API_PRIORITY; @@ -265,7 +265,7 @@ public class QuickSearchHelper implements ResourceListener { if (p.isLastWord(ObjectType.FAVORITE_GROUP)) { return SEARCH_FAVORITE_API_PRIORITY; } - if(!p.isNoSelectedType() || !p.isUnknownSearchWordPresent()) { + if (!p.isNoSelectedType() || !p.isUnknownSearchWordPresent()) { return -1; } return SEARCH_FAVORITE_API_PRIORITY; @@ -288,8 +288,13 @@ public class QuickSearchHelper implements ResourceListener { @Override public boolean search(SearchPhrase phrase, SearchUICore.SearchResultMatcher resultMatcher) throws IOException { List amenities = new ArrayList<>(); - amenities.addAll(nominatimPoiFilter.initializeNewSearch(app.getSettings().getLastKnownMapLocation().getLatitude(), app.getSettings().getLastKnownMapLocation().getLongitude(), -1, null)); - amenities.addAll(nominatimAddressFilter.initializeNewSearch(app.getSettings().getLastKnownMapLocation().getLatitude(), app.getSettings().getLastKnownMapLocation().getLongitude(), -1, null)); + double lat = app.getSettings().getLastKnownMapLocation().getLatitude(); + double lon = app.getSettings().getLastKnownMapLocation().getLongitude(); + String text = phrase.getUnknownSearchPhrase(); + nominatimPoiFilter.setFilterByName(text); + nominatimAddressFilter.setFilterByName(text); + amenities.addAll(nominatimPoiFilter.initializeNewSearch(lat, lon, -1, null)); + amenities.addAll(nominatimAddressFilter.initializeNewSearch(lat, lon, -1, null)); int p = 0; for (Amenity amenity : amenities) { SearchResult sr = new SearchResult(phrase); @@ -307,7 +312,6 @@ public class QuickSearchHelper implements ResourceListener { } return true; } - } public static class SearchHistoryAPI extends SearchBaseAPI { @@ -318,7 +322,7 @@ public class QuickSearchHelper implements ResourceListener { super(ObjectType.RECENT_OBJ); this.app = app; } - + @Override public boolean isSearchMoreAvailable(SearchPhrase phrase) { return false; @@ -333,7 +337,7 @@ public class QuickSearchHelper implements ResourceListener { SearchResult sr = new SearchResult(phrase); sr.localeName = point.getName().getName(); sr.object = point; - sr.priority = SEARCH_HISTORY_OBJECT_PRIORITY + (p++); + sr.priority = SEARCH_HISTORY_OBJECT_PRIORITY + (p++); sr.objectType = ObjectType.RECENT_OBJ; sr.location = new LatLon(point.getLat(), point.getLon()); sr.preferredZoom = 17; @@ -350,7 +354,7 @@ public class QuickSearchHelper implements ResourceListener { public int getSearchPriority(SearchPhrase p) { if (!p.isEmpty()) { return -1; - } + } return SEARCH_HISTORY_API_PRIORITY; } } From be21c20bbec2a89220ed2244cb9ae7a66a60056e Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Tue, 24 Oct 2017 14:34:15 +0300 Subject: [PATCH 4/7] Improve online search --- .../net/osmand/search/core/ObjectType.java | 2 +- .../activities/search/SearchPOIActivity.java | 2 +- .../src/net/osmand/plus/poi/PoiUIFilter.java | 20 +++++--- .../search/QuickSearchDialogFragment.java | 2 +- .../osmand/plus/search/QuickSearchHelper.java | 51 ++++++++++--------- 5 files changed, 41 insertions(+), 36 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/search/core/ObjectType.java b/OsmAnd-java/src/net/osmand/search/core/ObjectType.java index 83378d47b5..a0dab84697 100644 --- a/OsmAnd-java/src/net/osmand/search/core/ObjectType.java +++ b/OsmAnd-java/src/net/osmand/search/core/ObjectType.java @@ -10,7 +10,7 @@ public enum ObjectType { // UI OBJECTS FAVORITE(true), FAVORITE_GROUP(false), WPT(true), RECENT_OBJ(true), // ONLINE - ONLINE_POI(true), ONLINE_ADDRESS(true), + ONLINE_SEARCH(true), ONLINE_ADDRESS(true), REGION(true), diff --git a/OsmAnd/src/net/osmand/plus/activities/search/SearchPOIActivity.java b/OsmAnd/src/net/osmand/plus/activities/search/SearchPOIActivity.java index ad106c37e8..da1e5a0762 100644 --- a/OsmAnd/src/net/osmand/plus/activities/search/SearchPOIActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/search/SearchPOIActivity.java @@ -685,7 +685,7 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa if (searchLocation != null) { if (requestType == NEW_SEARCH_INIT) { return filter.initializeNewSearch(searchLocation.getLatitude(), searchLocation.getLongitude(), - -1, this); + -1, this, -1); } else if (requestType == SEARCH_FURTHER) { return filter.searchFurther(searchLocation.getLatitude(), searchLocation.getLongitude(), this); } else if (requestType == SEARCH_AGAIN) { diff --git a/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java b/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java index 5e49704250..5210767983 100644 --- a/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java +++ b/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java @@ -202,15 +202,19 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable public void clearPreviousZoom() { distanceInd = 0; } - + public void clearCurrentResults() { if (currentSearchResult != null) { currentSearchResult = new ArrayList<>(); } } - public List initializeNewSearch(double lat, double lon, int firstTimeLimit, ResultMatcher matcher) { - clearPreviousZoom(); + public List initializeNewSearch(double lat, double lon, int firstTimeLimit, ResultMatcher matcher, int radius) { + if (radius < 0) { + clearPreviousZoom(); + } else if (radius < distanceToSearchValues.length) { + distanceInd = radius; + } List amenityList = searchAmenities(lat, lon, matcher); MapUtils.sortListOfMapObject(amenityList, lat, lon); if (firstTimeLimit > 0) { @@ -272,7 +276,7 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable protected List searchAmenitiesInternal(double lat, double lon, double topLatitude, double bottomLatitude, double leftLongitude, double rightLongitude, int zoom, final ResultMatcher matcher) { - return app.getResourceManager().searchAmenities(this, + return app.getResourceManager().searchAmenities(this, topLatitude, leftLongitude, bottomLatitude, rightLongitude, zoom, wrapResultMatcher(matcher)); } @@ -314,7 +318,7 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable return getNameFilterInternal(nmFilter, allTime, open, poiAdditionalsFilter); } - private AmenityNameFilter getNameFilterInternal(StringBuilder nmFilter, + private AmenityNameFilter getNameFilterInternal(StringBuilder nmFilter, final boolean allTime, final boolean open, final List poiAdditionals) { final CollatorStringMatcher sm = nmFilter.length() > 0 ? @@ -324,7 +328,7 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable @Override public boolean accept(Amenity a) { if (sm != null) { - String lower = OsmAndFormatter.getPoiStringWithoutType(a, + String lower = OsmAndFormatter.getPoiStringWithoutType(a, app.getSettings().MAP_PREFERRED_LOCALE.get(), app.getSettings().MAP_TRANSLITERATE_NAMES.get()); if (!sm.matches(lower)) { return false; @@ -417,7 +421,7 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable public Object getIconResource() { return getIconId(); } - + @Override public ResultMatcher wrapResultMatcher(final ResultMatcher matcher) { final AmenityNameFilter nm = getNameFilter(filterByName); @@ -699,7 +703,7 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable } return set.contains(subtype); } - + @Override public boolean isEmpty() { return acceptedTypes.isEmpty() && diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index 8e921176ee..f35d2d3a6b 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -1332,7 +1332,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC private void startOnlinePoiSearch() { SearchSettings settings = searchUICore.getSearchSettings() - .setSearchTypes(ObjectType.ONLINE_POI) + .setSearchTypes(ObjectType.ONLINE_SEARCH) .setEmptyQueryAllowed(false) .setAddressSearch(false) .setSortByName(true) diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java index 9a07a80b0e..30c3110d82 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java @@ -17,6 +17,7 @@ import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.resources.ResourceManager.ResourceListener; import net.osmand.search.SearchUICore; import net.osmand.search.SearchUICore.SearchResultCollection; +import net.osmand.search.SearchUICore.SearchResultMatcher; import net.osmand.search.core.CustomSearchPoiFilter; import net.osmand.search.core.ObjectType; import net.osmand.search.core.SearchCoreFactory.SearchBaseAPI; @@ -25,7 +26,6 @@ import net.osmand.search.core.SearchResult; import net.osmand.util.Algorithms; import java.io.IOException; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -120,7 +120,7 @@ public class QuickSearchHelper implements ResourceListener { } @Override - public boolean search(SearchPhrase phrase, SearchUICore.SearchResultMatcher resultMatcher) throws IOException { + public boolean search(SearchPhrase phrase, SearchResultMatcher resultMatcher) throws IOException { if (phrase.isEmpty()) { return false; } @@ -175,7 +175,7 @@ public class QuickSearchHelper implements ResourceListener { } @Override - public boolean search(SearchPhrase phrase, SearchUICore.SearchResultMatcher resultMatcher) throws IOException { + public boolean search(SearchPhrase phrase, SearchResultMatcher resultMatcher) throws IOException { String baseGroupName = app.getString(R.string.shared_string_favorites); List groups = app.getFavorites().getFavoriteGroups(); for (FavoriteGroup group : groups) { @@ -231,7 +231,7 @@ public class QuickSearchHelper implements ResourceListener { } @Override - public boolean search(SearchPhrase phrase, SearchUICore.SearchResultMatcher resultMatcher) throws IOException { + public boolean search(SearchPhrase phrase, SearchResultMatcher resultMatcher) throws IOException { List favList = app.getFavorites().getFavouritePoints(); for (FavouritePoint point : favList) { if (!point.isVisible()) { @@ -274,43 +274,44 @@ public class QuickSearchHelper implements ResourceListener { public static class SearchOnlineApi extends SearchBaseAPI { - private NominatimPoiFilter nominatimPoiFilter; - private NominatimPoiFilter nominatimAddressFilter; - private OsmandApplication app; + private NominatimPoiFilter poiFilter; + private NominatimPoiFilter addressFilter; + private int p; public SearchOnlineApi(OsmandApplication app) { - super(ObjectType.ONLINE_POI); - this.nominatimPoiFilter = app.getPoiFilters().getNominatimPOIFilter(); - this.nominatimAddressFilter = app.getPoiFilters().getNominatimAddressFilter(); - this.app = app; + super(ObjectType.ONLINE_SEARCH); + this.poiFilter = app.getPoiFilters().getNominatimPOIFilter(); + this.addressFilter = app.getPoiFilters().getNominatimAddressFilter(); } @Override - public boolean search(SearchPhrase phrase, SearchUICore.SearchResultMatcher resultMatcher) throws IOException { - List amenities = new ArrayList<>(); - double lat = app.getSettings().getLastKnownMapLocation().getLatitude(); - double lon = app.getSettings().getLastKnownMapLocation().getLongitude(); + public boolean search(SearchPhrase phrase, SearchResultMatcher matcher) throws IOException { + double lat = phrase.getSettings().getOriginalLocation().getLatitude(); + double lon = phrase.getSettings().getOriginalLocation().getLongitude(); String text = phrase.getUnknownSearchPhrase(); - nominatimPoiFilter.setFilterByName(text); - nominatimAddressFilter.setFilterByName(text); - amenities.addAll(nominatimPoiFilter.initializeNewSearch(lat, lon, -1, null)); - amenities.addAll(nominatimAddressFilter.initializeNewSearch(lat, lon, -1, null)); - int p = 0; + poiFilter.setFilterByName(text); + addressFilter.setFilterByName(text); + p = 0; + publishAmenities(phrase, matcher, poiFilter.initializeNewSearch(lat, lon, -1, null, phrase.getRadiusLevel()), true); + publishAmenities(phrase, matcher, addressFilter.initializeNewSearch(lat, lon, -1, null, -1), false); + return true; + } + + private void publishAmenities(SearchPhrase phrase, SearchResultMatcher matcher, List amenities, boolean poi) { for (Amenity amenity : amenities) { SearchResult sr = new SearchResult(phrase); sr.localeName = amenity.getName(); sr.object = amenity; sr.priority = SEARCH_ONLINE_PRIORITY + (p++); - sr.objectType = ObjectType.ONLINE_POI; + sr.objectType = poi ? ObjectType.POI : ObjectType.POI; // todo sr.location = amenity.getLocation(); sr.preferredZoom = 17; if (phrase.getUnknownSearchWordLength() <= 1 && phrase.isNoSelectedType()) { - resultMatcher.publish(sr); + matcher.publish(sr); } else if (phrase.getNameStringMatcher().matches(sr.localeName)) { - resultMatcher.publish(sr); + matcher.publish(sr); } } - return true; } } @@ -329,7 +330,7 @@ public class QuickSearchHelper implements ResourceListener { } @Override - public boolean search(SearchPhrase phrase, SearchUICore.SearchResultMatcher resultMatcher) throws IOException { + public boolean search(SearchPhrase phrase, SearchResultMatcher resultMatcher) throws IOException { SearchHistoryHelper helper = SearchHistoryHelper.getInstance(app); List points = helper.getHistoryEntries(); int p = 0; From 0eff2e785058155045c73159ef81c65990af9a0a Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Tue, 24 Oct 2017 16:03:54 +0300 Subject: [PATCH 5/7] Add small fixes --- .../src/net/osmand/plus/poi/PoiUIFilter.java | 18 ++++++++++-------- .../osmand/plus/search/QuickSearchHelper.java | 11 ++++------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java b/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java index 5210767983..d18feecd0e 100644 --- a/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java +++ b/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java @@ -57,7 +57,7 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable protected int distanceInd = 0; // in kilometers - protected double[] distanceToSearchValues = new double[] { 1, 2, 5, 10, 20, 50, 100, 200, 500 }; + protected double[] distanceToSearchValues = new double[]{1, 2, 5, 10, 20, 50, 100, 200, 500}; private final MapPoiTypes poiTypes; @@ -96,7 +96,7 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable // constructor for user defined filters public PoiUIFilter(String name, String filterId, - Map> acceptedTypes, OsmandApplication app) { + Map> acceptedTypes, OsmandApplication app) { this.app = app; isStandardFilter = false; poiTypes = app.getPoiTypes(); @@ -214,6 +214,8 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable clearPreviousZoom(); } else if (radius < distanceToSearchValues.length) { distanceInd = radius; + } else { + distanceInd = distanceToSearchValues.length - 1; } List amenityList = searchAmenities(lat, lon, matcher); MapUtils.sortListOfMapObject(amenityList, lat, lon); @@ -250,7 +252,7 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable } public List searchAmenities(double top, double left, double bottom, double right, int zoom, - ResultMatcher matcher) { + ResultMatcher matcher) { List results = new ArrayList(); List tempResults = currentSearchResult; if (tempResults != null) { @@ -275,7 +277,7 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable } protected List searchAmenitiesInternal(double lat, double lon, double topLatitude, - double bottomLatitude, double leftLongitude, double rightLongitude, int zoom, final ResultMatcher matcher) { + double bottomLatitude, double leftLongitude, double rightLongitude, int zoom, final ResultMatcher matcher) { return app.getResourceManager().searchAmenities(this, topLatitude, leftLongitude, bottomLatitude, rightLongitude, zoom, wrapResultMatcher(matcher)); } @@ -319,10 +321,10 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable } private AmenityNameFilter getNameFilterInternal(StringBuilder nmFilter, - final boolean allTime, final boolean open, final List poiAdditionals) { + final boolean allTime, final boolean open, final List poiAdditionals) { final CollatorStringMatcher sm = nmFilter.length() > 0 ? - new CollatorStringMatcher(nmFilter.toString().trim(), StringMatcherMode.CHECK_CONTAINS) : null; + new CollatorStringMatcher(nmFilter.toString().trim(), StringMatcherMode.CHECK_CONTAINS) : null; return new AmenityNameFilter() { @Override @@ -337,8 +339,8 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable if (poiAdditionals != null) { Map textPoiAdditionalsMap = new HashMap<>(); Map> poiAdditionalCategoriesMap = new HashMap<>(); - for (PoiType pt : poiAdditionals) { - String category = pt.getPoiAdditionalCategory(); + for (PoiType pt : poiAdditionals) { + String category = pt.getPoiAdditionalCategory(); List types = poiAdditionalCategoriesMap.get(category); if (types == null) { types = new ArrayList<>(); diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java index 30c3110d82..bab86527dd 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java @@ -39,7 +39,8 @@ public class QuickSearchHelper implements ResourceListener { public static final int SEARCH_WPT_OBJECT_PRIORITY = 52; public static final int SEARCH_HISTORY_API_PRIORITY = 50; public static final int SEARCH_HISTORY_OBJECT_PRIORITY = 53; - public static final int SEARCH_ONLINE_PRIORITY = 53; + public static final int SEARCH_ONLINE_AMENITY_PRIORITY = 700; + public static final int SEARCH_ONLINE_ADDRESS_PRIORITY = 500; private OsmandApplication app; private SearchUICore core; private SearchResultCollection resultCollection; @@ -302,15 +303,11 @@ public class QuickSearchHelper implements ResourceListener { SearchResult sr = new SearchResult(phrase); sr.localeName = amenity.getName(); sr.object = amenity; - sr.priority = SEARCH_ONLINE_PRIORITY + (p++); + sr.priority = poi ? SEARCH_ONLINE_AMENITY_PRIORITY : SEARCH_ONLINE_ADDRESS_PRIORITY + (p++); sr.objectType = poi ? ObjectType.POI : ObjectType.POI; // todo sr.location = amenity.getLocation(); sr.preferredZoom = 17; - if (phrase.getUnknownSearchWordLength() <= 1 && phrase.isNoSelectedType()) { - matcher.publish(sr); - } else if (phrase.getNameStringMatcher().matches(sr.localeName)) { - matcher.publish(sr); - } + matcher.publish(sr); } } } From 20f09756e3a759ab7f8f1d316e7ee32fc9606be8 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Tue, 24 Oct 2017 17:26:19 +0300 Subject: [PATCH 6/7] Add the correct types --- .../osmand/plus/search/QuickSearchHelper.java | 80 +++++++++++++++++-- .../plus/search/QuickSearchListFragment.java | 52 +----------- .../search/listitems/QuickSearchListItem.java | 12 ++- 3 files changed, 86 insertions(+), 58 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java index bab86527dd..1ec6a96fa5 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java @@ -1,13 +1,21 @@ package net.osmand.plus.search; +import android.support.annotation.NonNull; + import net.osmand.binary.BinaryMapIndexReader; +import net.osmand.binary.BinaryMapIndexReader.SearchPoiTypeFilter; import net.osmand.data.Amenity; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; +import net.osmand.data.QuadRect; +import net.osmand.osm.AbstractPoiType; +import net.osmand.osm.MapPoiTypes; +import net.osmand.osm.PoiCategory; import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.FavouritesDbHelper.FavoriteGroup; import net.osmand.plus.GPXUtilities; import net.osmand.plus.GpxSelectionHelper; +import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.helpers.SearchHistoryHelper; @@ -24,6 +32,7 @@ import net.osmand.search.core.SearchCoreFactory.SearchBaseAPI; import net.osmand.search.core.SearchPhrase; import net.osmand.search.core.SearchResult; import net.osmand.util.Algorithms; +import net.osmand.util.MapUtils; import java.io.IOException; import java.util.Arrays; @@ -106,6 +115,45 @@ public class QuickSearchHelper implements ResourceListener { core.getSearchSettings().setOfflineIndexes(Arrays.asList(binaryMapIndexReaderArray)); } + public Amenity findAmenity(String name, double lat, double lon, String lang, boolean transliterate) { + QuadRect rect = MapUtils.calculateLatLonBbox(lat, lon, 15); + List amenities = app.getResourceManager().searchAmenities( + new SearchPoiTypeFilter() { + @Override + public boolean accept(PoiCategory type, String subcategory) { + return true; + } + + @Override + public boolean isEmpty() { + return false; + } + }, rect.top, rect.left, rect.bottom, rect.right, -1, null); + + MapPoiTypes types = app.getPoiTypes(); + for (Amenity amenity : amenities) { + String poiSimpleFormat = OsmAndFormatter.getPoiStringWithoutType(amenity, lang, transliterate); + if (poiSimpleFormat.equals(name)) { + return amenity; + } + } + for (Amenity amenity : amenities) { + String amenityName = amenity.getName(lang, transliterate); + if (Algorithms.isEmpty(amenityName)) { + AbstractPoiType st = types.getAnyPoiTypeByKey(amenity.getSubType()); + if (st != null) { + amenityName = st.getTranslation(); + } else { + amenityName = amenity.getSubType(); + } + } + if (name.contains(amenityName)) { + return amenity; + } + } + return null; + } + public static class SearchWptAPI extends SearchBaseAPI { private OsmandApplication app; @@ -275,12 +323,14 @@ public class QuickSearchHelper implements ResourceListener { public static class SearchOnlineApi extends SearchBaseAPI { + OsmandApplication app; private NominatimPoiFilter poiFilter; private NominatimPoiFilter addressFilter; private int p; public SearchOnlineApi(OsmandApplication app) { super(ObjectType.ONLINE_SEARCH); + this.app = app; this.poiFilter = app.getPoiFilters().getNominatimPOIFilter(); this.addressFilter = app.getPoiFilters().getNominatimAddressFilter(); } @@ -300,16 +350,32 @@ public class QuickSearchHelper implements ResourceListener { private void publishAmenities(SearchPhrase phrase, SearchResultMatcher matcher, List amenities, boolean poi) { for (Amenity amenity : amenities) { - SearchResult sr = new SearchResult(phrase); - sr.localeName = amenity.getName(); - sr.object = amenity; - sr.priority = poi ? SEARCH_ONLINE_AMENITY_PRIORITY : SEARCH_ONLINE_ADDRESS_PRIORITY + (p++); - sr.objectType = poi ? ObjectType.POI : ObjectType.POI; // todo - sr.location = amenity.getLocation(); - sr.preferredZoom = 17; + SearchResult sr = getSearchResult(phrase, poi, amenity); + if (poi) { + LatLon latLon = amenity.getLocation(); + String lang = sr.requiredSearchPhrase.getSettings().getLang(); + boolean transliterate = sr.requiredSearchPhrase.getSettings().isTransliterate(); + Amenity a = app.getSearchUICore().findAmenity(amenity.getName(), latLon.getLatitude(), + latLon.getLongitude(), lang, transliterate); + if (a != null) { + sr = getSearchResult(phrase, true, a); + } + } matcher.publish(sr); } } + + @NonNull + private SearchResult getSearchResult(SearchPhrase phrase, boolean poi, Amenity amenity) { + SearchResult sr = new SearchResult(phrase); + sr.localeName = amenity.getName(); + sr.object = amenity; + sr.priority = poi ? SEARCH_ONLINE_AMENITY_PRIORITY : SEARCH_ONLINE_ADDRESS_PRIORITY + (p++); + sr.objectType = poi ? ObjectType.POI : ObjectType.ONLINE_ADDRESS; + sr.location = amenity.getLocation(); + sr.preferredZoom = 17; + return sr; + } } public static class SearchHistoryAPI extends SearchBaseAPI { diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchListFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchListFragment.java index 5d9c8e82d2..45c0a96614 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchListFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchListFragment.java @@ -10,17 +10,12 @@ import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; -import net.osmand.binary.BinaryMapIndexReader.SearchPoiTypeFilter; import net.osmand.data.Amenity; import net.osmand.data.City; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; -import net.osmand.data.QuadRect; import net.osmand.data.Street; -import net.osmand.osm.AbstractPoiType; -import net.osmand.osm.MapPoiTypes; -import net.osmand.osm.PoiCategory; import net.osmand.plus.GPXUtilities; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; @@ -38,7 +33,6 @@ import net.osmand.plus.search.listitems.QuickSearchTopShadowListItem; import net.osmand.search.core.ObjectType; import net.osmand.search.core.SearchResult; import net.osmand.util.Algorithms; -import net.osmand.util.MapUtils; import java.util.ArrayList; import java.util.List; @@ -177,18 +171,18 @@ public abstract class QuickSearchListFragment extends OsmAndListFragment { Amenity a = (Amenity) object; String poiSimpleFormat = OsmAndFormatter.getPoiStringWithoutType(a, lang, transliterate); pointDescription = new PointDescription(PointDescription.POINT_TYPE_POI, poiSimpleFormat); - pointDescription.setIconName(QuickSearchListItem.getAmenityIconName(a)); + pointDescription.setIconName(QuickSearchListItem.getAmenityIconName(app, a)); break; case RECENT_OBJ: HistoryEntry entry = (HistoryEntry) object; pointDescription = entry.getName(); if (pointDescription.isPoi()) { - Amenity amenity = findAmenity(entry.getName().getName(), entry.getLat(), entry.getLon(), lang, transliterate); + Amenity amenity = app.getSearchUICore().findAmenity(entry.getName().getName(), entry.getLat(), entry.getLon(), lang, transliterate); if (amenity != null) { object = amenity; pointDescription = new PointDescription(PointDescription.POINT_TYPE_POI, OsmAndFormatter.getPoiStringWithoutType(amenity, lang, transliterate)); - pointDescription.setIconName(QuickSearchListItem.getAmenityIconName(amenity)); + pointDescription.setIconName(QuickSearchListItem.getAmenityIconName(app, amenity)); } } else if (pointDescription.isFavorite()) { LatLon entryLatLon = new LatLon(entry.getLat(), entry.getLon()); @@ -291,46 +285,6 @@ public abstract class QuickSearchListFragment extends OsmAndListFragment { } } - private Amenity findAmenity(String name, double lat, double lon, String lang, boolean transliterate) { - OsmandApplication app = getMyApplication(); - QuadRect rect = MapUtils.calculateLatLonBbox(lat, lon, 15); - List amenities = app.getResourceManager().searchAmenities( - new SearchPoiTypeFilter() { - @Override - public boolean accept(PoiCategory type, String subcategory) { - return true; - } - - @Override - public boolean isEmpty() { - return false; - } - }, rect.top, rect.left, rect.bottom, rect.right, -1, null); - - MapPoiTypes types = app.getPoiTypes(); - for (Amenity amenity : amenities) { - String poiSimpleFormat = OsmAndFormatter.getPoiStringWithoutType(amenity, lang, transliterate); - if (poiSimpleFormat.equals(name)) { - return amenity; - } - } - for (Amenity amenity : amenities) { - String amenityName = amenity.getName(lang, transliterate); - if (Algorithms.isEmpty(amenityName)) { - AbstractPoiType st = types.getAnyPoiTypeByKey(amenity.getSubType()); - if (st != null) { - amenityName = st.getTranslation(); - } else { - amenityName = amenity.getSubType(); - } - } - if (name.contains(amenityName)) { - return amenity; - } - } - return null; - } - public MapActivity getMapActivity() { return (MapActivity) getActivity(); } diff --git a/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchListItem.java b/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchListItem.java index 31744e0482..ba58531c6b 100644 --- a/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchListItem.java +++ b/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchListItem.java @@ -242,6 +242,8 @@ public class QuickSearchListItem { sb.append(new File(gpx.path).getName()); } return sb.toString(); + case ONLINE_ADDRESS: + return app.getString(R.string.shared_string_address); case UNKNOWN_NAME_FILTER: break; } @@ -287,8 +289,11 @@ public class QuickSearchListItem { return null; } - public static String getAmenityIconName(Amenity amenity) { + public static String getAmenityIconName(OsmandApplication app, Amenity amenity) { PoiType st = amenity.getType().getPoiTypeByKeyName(amenity.getSubType()); + if (st == null) { + st = app.getPoiTypes().getPoiTypeByKey(amenity.getSubType()); + } if (st != null) { if (RenderingIcons.containsBigIcon(st.getIconKeyName())) { return st.getIconKeyName(); @@ -344,7 +349,7 @@ public class QuickSearchListItem { } case POI: Amenity amenity = (Amenity) searchResult.object; - String id = getAmenityIconName(amenity); + String id = getAmenityIconName(app, amenity); if (id != null) { iconId = RenderingIcons.getBigIconResourceId(id); if (iconId > 0) { @@ -392,6 +397,9 @@ public class QuickSearchListItem { case WPT: WptPt wpt = (WptPt) searchResult.object; return FavoriteImageDrawable.getOrCreate(app, wpt.getColor(), false); + case ONLINE_ADDRESS: + return app.getIconsCache().getIcon(R.drawable.ic_action_search_dark, + app.getSettings().isLightContent() ? R.color.osmand_orange : R.color.osmand_orange_dark); case UNKNOWN_NAME_FILTER: break; } From c70a19f4900f44d850514db3b82d3ff566676884 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Tue, 24 Oct 2017 17:35:07 +0300 Subject: [PATCH 7/7] Remove unnecessary field --- OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java index 1ec6a96fa5..f52a06b239 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java @@ -326,7 +326,6 @@ public class QuickSearchHelper implements ResourceListener { OsmandApplication app; private NominatimPoiFilter poiFilter; private NominatimPoiFilter addressFilter; - private int p; public SearchOnlineApi(OsmandApplication app) { super(ObjectType.ONLINE_SEARCH); @@ -342,7 +341,6 @@ public class QuickSearchHelper implements ResourceListener { String text = phrase.getUnknownSearchPhrase(); poiFilter.setFilterByName(text); addressFilter.setFilterByName(text); - p = 0; publishAmenities(phrase, matcher, poiFilter.initializeNewSearch(lat, lon, -1, null, phrase.getRadiusLevel()), true); publishAmenities(phrase, matcher, addressFilter.initializeNewSearch(lat, lon, -1, null, -1), false); return true; @@ -370,7 +368,7 @@ public class QuickSearchHelper implements ResourceListener { SearchResult sr = new SearchResult(phrase); sr.localeName = amenity.getName(); sr.object = amenity; - sr.priority = poi ? SEARCH_ONLINE_AMENITY_PRIORITY : SEARCH_ONLINE_ADDRESS_PRIORITY + (p++); + sr.priority = poi ? SEARCH_ONLINE_AMENITY_PRIORITY : SEARCH_ONLINE_ADDRESS_PRIORITY; sr.objectType = poi ? ObjectType.POI : ObjectType.ONLINE_ADDRESS; sr.location = amenity.getLocation(); sr.preferredZoom = 17;