From 862286fce62701af7b5f6190d21e7716b8867334 Mon Sep 17 00:00:00 2001 From: cepprice Date: Sun, 10 Jan 2021 06:13:56 +0500 Subject: [PATCH] Refactor --- .../src/net/osmand/plus/poi/PoiUIFilter.java | 247 +++++++++++------- 1 file changed, 150 insertions(+), 97 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java b/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java index 37b36fd08c..0d837f560a 100644 --- a/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java +++ b/OsmAnd/src/net/osmand/plus/poi/PoiUIFilter.java @@ -383,116 +383,169 @@ public class PoiUIFilter implements SearchPoiTypeFilter, Comparable return getNameFilterInternal(unknownFilters, allTime, open, poiAdditionalsFilter); } - private AmenityNameFilter getNameFilterInternal(final List unknownFilters, - final boolean allTime, final boolean open, final List poiAdditionals) { + private AmenityNameFilter getNameFilterInternal( + final List unknownFilters, final boolean shouldBeAllTime, + final boolean shouldBeOpened, final List selectedFilters + ) { return new AmenityNameFilter() { @Override - public boolean accept(Amenity a) { - CollatorStringMatcher sm = null; + public boolean accept(Amenity amenity) { + if (shouldBeAllTime) { + if (!"24/7".equalsIgnoreCase(amenity.getOpeningHours()) && + !"Mo-Su 00:00-24:00".equalsIgnoreCase(amenity.getOpeningHours())) { + return false; + } + } - if (unknownFilters != null) { - StringBuilder nameFilter = new StringBuilder(); - for (String filter : unknownFilters) { - String formattedFilter = filter.replace(':', '_').toLowerCase(); - if (a.getAdditionalInfo(formattedFilter) == null) { - nameFilter.append(filter).append(" "); - } - } - if (nameFilter.length() > 0) { - sm = new CollatorStringMatcher(nameFilter.toString().trim(), StringMatcherMode.CHECK_CONTAINS); - } + if (shouldBeOpened && !isOpened(amenity)) { + return false; } - - if (sm != null) { - List names = OsmAndFormatter.getPoiStringsWithoutType(a, - app.getSettings().MAP_PREFERRED_LOCALE.get(), app.getSettings().MAP_TRANSLITERATE_NAMES.get()); - boolean match = false; - for (String name : names) { - if (sm.matches(name)) { - match = true; - break; - } - } - if (!match) { - return false; - } - } - if (poiAdditionals != null) { - Map textPoiAdditionalsMap = new HashMap<>(); - Map> poiAdditionalCategoriesMap = new HashMap<>(); - for (PoiType pt : poiAdditionals) { - String category = pt.getPoiAdditionalCategory(); - List types = poiAdditionalCategoriesMap.get(category); - if (types == null) { - types = new ArrayList<>(); - poiAdditionalCategoriesMap.put(category, types); - } - types.add(pt); - String osmTag = pt.getOsmTag(); - if (osmTag.length() < pt.getKeyName().length()) { - PoiType textPoiType = poiTypes.getTextPoiAdditionalByKey(osmTag); - if (textPoiType != null) { - textPoiAdditionalsMap.put(pt, textPoiType); - } - } - } - for (List types : poiAdditionalCategoriesMap.values()) { - boolean acceptedAnyInCategory = false; - for (PoiType p : types) { - String inf = a.getAdditionalInfo(p.getKeyName()); - if (inf != null) { - acceptedAnyInCategory = true; - break; - } else { - PoiType textPoiType = textPoiAdditionalsMap.get(p); - if (textPoiType != null) { - inf = a.getAdditionalInfo(textPoiType.getKeyName()); - if (!Algorithms.isEmpty(inf)) { - String[] items = inf.split(";"); - String val = p.getOsmValue().trim().toLowerCase(); - for (String item : items) { - if (item.trim().toLowerCase().equals(val)) { - acceptedAnyInCategory = true; - break; - } - } - if (acceptedAnyInCategory) { - break; - } - } - } - } - } - if (!acceptedAnyInCategory) { - return false; - } - } + String nameFilter = extractNameFilter(amenity, unknownFilters); + if (!matchesAnyAmenityName(amenity, nameFilter)) { + return false; } - if (allTime) { - if (!"24/7".equalsIgnoreCase(a.getOpeningHours()) && !"Mo-Su 00:00-24:00".equalsIgnoreCase(a.getOpeningHours())) { - return false; - } - } - if (open) { - OpeningHours rs = OpeningHoursParser.parseOpenedHours(a.getOpeningHours()); - if (rs != null) { - Calendar inst = Calendar.getInstance(); - inst.setTimeInMillis(System.currentTimeMillis()); - boolean work = rs.isOpenedForTime(inst); - if (!work) { - return false; - } - } else { - return false; - } + + if (!acceptedAnyFilterOfEachCategory(amenity, selectedFilters)) { + return false; } + return true; } }; } + private boolean isOpened(Amenity amenity) { + OpeningHours openedHours = OpeningHoursParser.parseOpenedHours(amenity.getOpeningHours()); + if (openedHours == null) { + return false; + } + + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(System.currentTimeMillis()); + + return openedHours.isOpenedForTime(calendar); + } + + private String extractNameFilter(Amenity amenity, List unknownFilters) { + if (unknownFilters == null) { + return ""; + } + + StringBuilder nameFilter = new StringBuilder(); + for (String filter : unknownFilters) { + String formattedFilter = filter.replace(':', '_').toLowerCase(); + if (amenity.getAdditionalInfo(formattedFilter) == null) { + nameFilter.append(filter).append(" "); + } + } + + return nameFilter.toString(); + } + + private boolean matchesAnyAmenityName(Amenity amenity, String nameFilter) { + if (nameFilter.length() == 0) { + return true; + } + + final CollatorStringMatcher sm = + new CollatorStringMatcher(nameFilter.trim(), StringMatcherMode.CHECK_CONTAINS); + + List names = OsmAndFormatter.getPoiStringsWithoutType( + amenity, app.getSettings().MAP_PREFERRED_LOCALE.get(), + app.getSettings().MAP_TRANSLITERATE_NAMES.get()); + for (String name : names) { + if (sm.matches(name)) { + return true; + } + } + return false; + } + + private boolean acceptedAnyFilterOfEachCategory(Amenity amenity, List selectedFilters) { + if (selectedFilters == null) { + return true; + } + + Map> filterCategories = new HashMap<>(); + Map textFilters = new HashMap<>(); + + fillFilterCategories(selectedFilters, filterCategories, textFilters); + + for (List category : filterCategories.values()) { + if (!acceptedAnyFilterOfCategory(amenity, category, textFilters)) { + return false; + } + } + + return true; + } + + private void fillFilterCategories( + List selectedFilters, + Map> filterCategories, Map textFilters + ) { + for (PoiType filter : selectedFilters) { + String category = filter.getPoiAdditionalCategory(); + List filtersOfCategory = filterCategories.get(category); + if (filtersOfCategory == null) { + filtersOfCategory = new ArrayList<>(); + filterCategories.put(category, filtersOfCategory); + } + filtersOfCategory.add(filter); + + String osmTag = filter.getOsmTag(); + if (osmTag.length() < filter.getKeyName().length()) { + PoiType textFilter = poiTypes.getTextPoiAdditionalByKey(osmTag); + if (textFilter != null) { + textFilters.put(filter, textFilter); + } + } + } + } + + private boolean acceptedAnyFilterOfCategory( + Amenity amenity, List category, Map textFilters) { + for (PoiType filter : category) { + if (acceptedFilter(amenity, filter, textFilters)) { + return true; + } + } + + return false; + } + + private boolean acceptedFilter( + Amenity amenity, PoiType filter, Map textFilterCategories + ) { + String filterValue = amenity.getAdditionalInfo(filter.getKeyName()); + + if (filterValue != null) { + return true; + } + + PoiType textPoiType = textFilterCategories.get(filter); + if (textPoiType == null) { + return false; + } + + filterValue = amenity.getAdditionalInfo(textPoiType.getKeyName()); + if (Algorithms.isEmpty(filterValue)) { + return false; + } + + String[] items = filterValue.split(";"); + String val = filter.getOsmValue().trim().toLowerCase(); + for (String item : items) { + if (item.trim().toLowerCase().equals(val)) { + return true; + } + } + + return false; + } + public String getNameToken24H() { return app.getString(R.string.shared_string_is_open_24_7).replace(' ', '_').toLowerCase(); }