diff --git a/OsmAnd-java/src/net/osmand/osm/MapPoiTypes.java b/OsmAnd-java/src/net/osmand/osm/MapPoiTypes.java index ebaf045f02..e0ead0cce8 100644 --- a/OsmAnd-java/src/net/osmand/osm/MapPoiTypes.java +++ b/OsmAnd-java/src/net/osmand/osm/MapPoiTypes.java @@ -3,7 +3,12 @@ package net.osmand.osm; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.text.Collator; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -61,6 +66,33 @@ public class MapPoiTypes { return otherCategory; } + public List getTopVisibleFilters() { + List lf = new ArrayList(); + for(PoiCategory pc : categories) { + if(pc.isTopVisible()) { + lf.add(pc); + } + for(PoiFilter p : pc.getPoiFilters()) { + if(p.isTopVisible()) { + lf.add(p); + } + } + } + sortList(lf); + return lf; + } + + + private void sortList(List lf) { + final Collator instance = Collator.getInstance(); + Collections.sort(lf, new Comparator() { + @Override + public int compare(PoiFilter object1, PoiFilter object2) { + return instance.compare(object1.getTranslation(), object2.getTranslation()); + } + }); + } + public PoiCategory getUserDefinedCategory() { return otherCategory; } @@ -128,6 +160,8 @@ public class MapPoiTypes { public void setPoiTranslator(PoiTranslator poiTranslator) { this.poiTranslator = poiTranslator; + sortList(categories); + } @@ -151,10 +185,12 @@ public class MapPoiTypes { String name = parser.getName(); if (name.equals("poi_category")) { lastCategory = new PoiCategory(this, parser.getAttributeValue("","name"), categories.size()); + lastCategory.setTopVisible(Boolean.parseBoolean(parser.getAttributeValue("","top"))); categories.add(lastCategory); } else if (name.equals("poi_filter")) { PoiFilter tp = new PoiFilter(this, lastCategory, parser.getAttributeValue("", "name")); + tp.setTopVisible(Boolean.parseBoolean(parser.getAttributeValue("","top"))); lastFilter = tp; lastCategory.addPoiType(tp); } else if(name.equals("poi_reference")){ @@ -249,7 +285,17 @@ public class MapPoiTypes { public static void main(String[] args) { DEFAULT_INSTANCE = new MapPoiTypes("/Users/victorshcherb/osmand/repos/resources/poi/poi_types.xml"); DEFAULT_INSTANCE.init(); - print(DEFAULT_INSTANCE) ; +// print(DEFAULT_INSTANCE) ; +// System.out.println("-----------------"); + List lf = DEFAULT_INSTANCE.getTopVisibleFilters(); + for(PoiFilter l : lf) { + System.out.println("----------------- " + l.getKeyName()); + print("", l); + Map> m = + l.putTypes(new LinkedHashMap>()); + System.out.println(m); + } + } public String getTranslation(AbstractPoiType abstractPoiType) { diff --git a/OsmAnd-java/src/net/osmand/osm/PoiCategory.java b/OsmAnd-java/src/net/osmand/osm/PoiCategory.java index 1314d48255..12ddef97ff 100644 --- a/OsmAnd-java/src/net/osmand/osm/PoiCategory.java +++ b/OsmAnd-java/src/net/osmand/osm/PoiCategory.java @@ -1,7 +1,9 @@ package net.osmand.osm; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; public class PoiCategory extends PoiFilter { @@ -33,6 +35,13 @@ public class PoiCategory extends PoiFilter { public void setDefaultTag(String defaultTag) { this.defaultTag = defaultTag; } + + public Map> putTypes( + Map> acceptedTypes) { + acceptedTypes.put(this, null); + addReferenceTypes(acceptedTypes); + return acceptedTypes; + } public boolean isWiki() { diff --git a/OsmAnd-java/src/net/osmand/osm/PoiFilter.java b/OsmAnd-java/src/net/osmand/osm/PoiFilter.java index 884e8f6d2d..c5cc17d56f 100644 --- a/OsmAnd-java/src/net/osmand/osm/PoiFilter.java +++ b/OsmAnd-java/src/net/osmand/osm/PoiFilter.java @@ -2,12 +2,14 @@ package net.osmand.osm; import java.util.ArrayList; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; public class PoiFilter extends AbstractPoiType { private PoiCategory pc; + private boolean topVisible; private List poiTypes = new ArrayList(); private Map map = new LinkedHashMap(); @@ -28,6 +30,40 @@ public class PoiFilter extends AbstractPoiType { if(!map.containsKey(type.getName())) { poiTypes.add(type); map.put(type.getName(), type); + } else { + PoiType prev = map.get(type.getName()); + if(prev.isReference()) { + poiTypes.remove(prev); + poiTypes.add(type); + map.put(type.getName(), type); + } + } + } + + public Map> putTypes(Map> acceptedTypes) { + if (!acceptedTypes.containsKey(pc)) { + acceptedTypes.put(pc, new LinkedHashSet()); + } + LinkedHashSet set = acceptedTypes.get(pc); + for (PoiType pt : poiTypes) { + set.add(pt.getKeyName()); + } + addReferenceTypes(acceptedTypes); + return acceptedTypes; + } + + protected void addReferenceTypes(Map> acceptedTypes) { + for (PoiType pt : getPoiTypes()) { + if (pt.isReference()) { + PoiCategory refCat = pt.getReferenceType().getCategory(); + if (!acceptedTypes.containsKey(refCat)) { + acceptedTypes.put(refCat, new LinkedHashSet()); + } + LinkedHashSet ls = acceptedTypes.get(refCat); + if (ls != null) { + ls.add(pt.getKeyName()); + } + } } } @@ -39,5 +75,12 @@ public class PoiFilter extends AbstractPoiType { return keyName; } + public void setTopVisible(boolean topVisible) { + this.topVisible = topVisible; + } + + public boolean isTopVisible() { + return topVisible; + } } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java index 438c6421c9..ec20ddea43 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java @@ -259,16 +259,13 @@ public class MapActivityLayers { OsmandApplication app = (OsmandApplication)getApplication(); final PoiFiltersHelper poiFilters = app.getPoiFilters(); final ContextMenuAdapter adapter = new ContextMenuAdapter(activity); - - Item is = adapter.item(getString(R.string.any_poi)); - if(RenderingIcons.containsBigIcon("null")) { - is.icon(RenderingIcons.getBigIconResourceId("null")); - } - is.reg(); - // 2nd custom - adapter.item(getString(R.string.poi_filter_custom_filter)).icon(RenderingIcons.getBigIconResourceId("user_defined")).reg(); - - for (PoiLegacyFilter f : poiFilters.getUserDefinedPoiFilters()) { + final List list = new ArrayList(); + for (PoiLegacyFilter f : poiFilters.getTopDefinedPoiFilters()) { + if(PoiLegacyFilter.BY_NAME_FILTER_ID.equals(f.getFilterId()) || + PoiLegacyFilter.NAME_FINDER_FILTER_ID.equals(f.getFilterId())) { + continue; + } + list.add(f); Item it = adapter.item(f.getName()); if (RenderingIcons.containsBigIcon(f.getSimplifiedId())) { it.icon(RenderingIcons.getBigIconResourceId(f.getSimplifiedId())); @@ -278,22 +275,15 @@ public class MapActivityLayers { it.reg(); userDefined.add(f); } - final List categories = getApplication().getPoiTypes().getCategories(); - for(PoiCategory t : categories){ - Item it = adapter.item(t.getTranslation()); - if(RenderingIcons.containsBigIcon(t.getKeyName())) { - it.icon(RenderingIcons.getBigIconResourceId(t.getKeyName())); - } - it.reg(); - } Builder builder = new AlertDialog.Builder(activity); - ListAdapter listAdapter =adapter.createListAdapter(activity, app.getSettings().isLightContent()); + ListAdapter listAdapter = adapter.createListAdapter(activity, app.getSettings().isLightContent()); builder.setAdapter(listAdapter, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { - if(which == 1){ - String filterId = PoiLegacyFilter.CUSTOM_FILTER_ID; + PoiLegacyFilter pf = list.get(which); + String filterId = pf.getFilterId(); + if(filterId.equals(PoiLegacyFilter.CUSTOM_FILTER_ID)){ getApplication().getSettings().setPoiFilterForMap(filterId); Intent newIntent = new Intent(activity, EditPOIFilterActivity.class); newIntent.putExtra(EditPOIFilterActivity.AMENITY_FILTER, filterId); @@ -301,23 +291,15 @@ public class MapActivityLayers { newIntent.putExtra(EditPOIFilterActivity.SEARCH_LON, mapView.getLongitude()); activity.startActivity(newIntent); } else { - String filterId; - if (which == 0) { - filterId = PoiFiltersHelper.getOsmDefinedFilterId(null); - } else if (which <= userDefined.size() + 1) { - filterId = userDefined.get(which - 2).getFilterId(); - } else { - filterId = PoiFiltersHelper.getOsmDefinedFilterId(categories.get(which - userDefined.size() - 2)); - } getApplication().getSettings().setPoiFilterForMap(filterId); - PoiLegacyFilter f = poiFilters.getFilterById(filterId); - if (f != null) { - f.clearNameFilter(); + pf = poiFilters.getFilterById(filterId); + if (pf != null && pf.isStandardFilter()) { + pf.clearNameFilter(); } - poiMapLayer.setFilter(f); + poiMapLayer.setFilter(pf); mapView.refreshMap(); if(selected != null && selected.length > 0) { - selected[0] = f; + selected[0] = pf; } } } diff --git a/OsmAnd/src/net/osmand/plus/activities/search/SearchPoiFilterFragment.java b/OsmAnd/src/net/osmand/plus/activities/search/SearchPoiFilterFragment.java index 59971ab985..369fddca0c 100644 --- a/OsmAnd/src/net/osmand/plus/activities/search/SearchPoiFilterFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/search/SearchPoiFilterFragment.java @@ -71,10 +71,7 @@ public class SearchPoiFilterFragment extends ListFragment implements SearchActiv public void refreshPoiListAdapter() { PoiFiltersHelper poiFilters = getApp().getPoiFilters(); List filters = new ArrayList() ; - filters.addAll(poiFilters.getTopStandardFilters()); - filters.addAll(poiFilters.getUserDefinedPoiFilters()); - filters.addAll(poiFilters.getOsmDefinedPoiFilters()); - filters.add(poiFilters.getNameFinderPOIFilter()); + filters.addAll(poiFilters.getTopDefinedPoiFilters()); setListAdapter(new AmenityAdapter(filters)); } diff --git a/OsmAnd/src/net/osmand/plus/poi/NameFinderPoiFilter.java b/OsmAnd/src/net/osmand/plus/poi/NameFinderPoiFilter.java index 2f9a53ca2e..e03c8ff206 100644 --- a/OsmAnd/src/net/osmand/plus/poi/NameFinderPoiFilter.java +++ b/OsmAnd/src/net/osmand/plus/poi/NameFinderPoiFilter.java @@ -24,7 +24,7 @@ import org.xmlpull.v1.XmlPullParserException; public class NameFinderPoiFilter extends PoiLegacyFilter { - public static final String FILTER_ID = "name_finder"; //$NON-NLS-1$ + public static final String FILTER_ID = NAME_FINDER_FILTER_ID; //$NON-NLS-1$ private static final Log log = PlatformUtil.getLog(NameFinderPoiFilter.class); private static final int LIMIT = 300; diff --git a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java index 7539f984ea..2bb9e92503 100644 --- a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java +++ b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java @@ -12,6 +12,7 @@ import java.util.Map; import net.osmand.osm.MapPoiTypes; import net.osmand.osm.PoiCategory; +import net.osmand.osm.PoiFilter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; @@ -23,8 +24,6 @@ public class PoiFiltersHelper { private NameFinderPoiFilter nameFinderPOIFilter; private List cacheTopStandardFilters; - private List cacheUserDefinedFilters; - private List cacheOsmDefinedFilters; private static final String UDF_CAR_AID = "car_aid"; private static final String UDF_FOR_TOURISTS = "for_tourists"; @@ -37,7 +36,10 @@ public class PoiFiltersHelper { private static final String UDF_RESTAURANTS = "restaurants"; private static final String UDF_PARKING = "parking"; - private static final String[] DEL = new String[] {}; + private static final String[] DEL = new String[] { + UDF_CAR_AID, UDF_FOR_TOURISTS, UDF_FOOD_SHOP, UDF_FUEL, UDF_SIGHTSEEING, UDF_EMERGENCY, + UDF_PUBLIC_TRANSPORT, UDF_ACCOMMODATION, UDF_RESTAURANTS, UDF_PARKING + }; public PoiFiltersHelper(OsmandApplication application){ this.application = application; @@ -50,202 +52,96 @@ public class PoiFiltersHelper { } - private PoiLegacyFilter findPoiFilter(String filterId, List... collections) { - for(List c : collections) { - for(PoiLegacyFilter f : c) { - if(f.getFilterId().equals(filterId)){ - return f; - } + + public PoiLegacyFilter getFilterById(String filterId){ + if(filterId == null){ + return null; + } + for(PoiLegacyFilter f : getTopDefinedPoiFilters()) { + if(f.getFilterId().equals(filterId)){ + return f; } } return null; } - @SuppressWarnings("unchecked") - public PoiLegacyFilter getFilterById(String filterId){ - if(filterId == null){ - return null; - } - if(filterId.equals(NameFinderPoiFilter.FILTER_ID)){ - return getNameFinderPOIFilter(); - } - return findPoiFilter(filterId, getUserDefinedPoiFilters(), getTopStandardFilters(), getOsmDefinedPoiFilters()); - } - - - private void putAll(MapPoiTypes poiTypes, Map> types, String tp) { - types.put(poiTypes.getPoiCategoryByName(tp), null); - } - - private void putValues(MapPoiTypes poiTypes, Map> types, String p, String... vls) { - LinkedHashSet list = new LinkedHashSet(); - Collections.addAll(list, vls); - PoiCategory pc = poiTypes.getPoiCategoryByName(p); - if(pc == null) { - throw new NullPointerException("Category " + p + " was not found"); - } - types.put(poiTypes.getPoiCategoryByName(p), list); - } - - private List getUserDefinedDefaultFilters(MapPoiTypes poiTypes) { - List filters = new ArrayList(); - filters.add(new PoiLegacyFilter(application.getString(R.string.poi_filter_accomodation), PoiLegacyFilter.USER_PREFIX + UDF_ACCOMMODATION, - configureDefaultUserDefinedFilter(poiTypes, UDF_ACCOMMODATION), application)); - filters.add(new PoiLegacyFilter(application.getString(R.string.poi_filter_car_aid), PoiLegacyFilter.USER_PREFIX + UDF_CAR_AID, - configureDefaultUserDefinedFilter(poiTypes, UDF_CAR_AID), application)); - filters.add(new PoiLegacyFilter(application.getString(R.string.poi_filter_food_shop), PoiLegacyFilter.USER_PREFIX + UDF_FOOD_SHOP, - configureDefaultUserDefinedFilter(poiTypes, UDF_FOOD_SHOP), application)); - filters.add(new PoiLegacyFilter(application.getString(R.string.poi_filter_for_tourists), PoiLegacyFilter.USER_PREFIX + UDF_FOR_TOURISTS, - configureDefaultUserDefinedFilter(poiTypes, UDF_FOR_TOURISTS), application)); - filters.add(new PoiLegacyFilter(application.getString(R.string.poi_filter_fuel), PoiLegacyFilter.USER_PREFIX + UDF_FUEL, - configureDefaultUserDefinedFilter(poiTypes, UDF_FUEL), application)); - filters.add(new PoiLegacyFilter(application.getString(R.string.poi_filter_parking), PoiLegacyFilter.USER_PREFIX + UDF_PARKING, - configureDefaultUserDefinedFilter(poiTypes, UDF_PARKING), application)); - filters.add(new PoiLegacyFilter(application.getString(R.string.poi_filter_public_transport), - PoiLegacyFilter.USER_PREFIX + UDF_PUBLIC_TRANSPORT, configureDefaultUserDefinedFilter(poiTypes, UDF_PUBLIC_TRANSPORT), application)); - filters.add(new PoiLegacyFilter(application.getString(R.string.poi_filter_restaurants), PoiLegacyFilter.USER_PREFIX + UDF_RESTAURANTS, - configureDefaultUserDefinedFilter(poiTypes, UDF_RESTAURANTS), application)); - filters.add(new PoiLegacyFilter(application.getString(R.string.poi_filter_sightseeing), PoiLegacyFilter.USER_PREFIX + UDF_SIGHTSEEING, - configureDefaultUserDefinedFilter(poiTypes, UDF_SIGHTSEEING), application)); - // UDF_EMERGENCY = "emergency"; - // UDF_ENTERTAINMENT = "entertainment"; - return filters; - } - - private Map> configureDefaultUserDefinedFilter(MapPoiTypes poiTypes, String key) { - Map> types = new LinkedHashMap>(); - if(UDF_ACCOMMODATION.equals(key)){ - putValues(poiTypes, types, "tourism", "camp_site", - "caravan_site","picnic_site","alpine_hut", "chalet","guest_house", - "hostel", "hotel","motel"); - } else if (UDF_CAR_AID.equals(key)) { - putValues(poiTypes, types, "transportation", "fuel", "car_wash", "car_repair","car", "car_sharing"); - putValues(poiTypes, types, "shop", "fuel", "car_wash", "car_repair","car", "car_parts"); - } else if (UDF_FOOD_SHOP.equals(key)) { - putValues(poiTypes, types, "shop", "alcohol", "bakery", "beverages", "butcher", "convenience", "department_store", - "convenience", "farm", "general", "ice_cream", "kiosk", "seafood", "supermarket", "variety_store"); - } else if(UDF_FOR_TOURISTS.equals(key)){ - putAll(poiTypes, types, "tourism"); - putAll(poiTypes, types, "finance"); - putAll(poiTypes, types, "osmwiki"); - } else if(UDF_FUEL.equals(key)){ - putValues(poiTypes, types, "transportation", "fuel"); - } else if (UDF_PARKING.equals(key)) { - putValues(poiTypes, types, "transportation", "parking", - "bicycle_parking"); - } else if (UDF_PUBLIC_TRANSPORT.equals(key)) { - putValues(poiTypes, types, "transportation", "public_transport_stop_position", "public_transport_platform", - "public_transport_station", - // railway - "railway_platform", "railway_station", "halt", "tram_stop", "subway_entrance", "railway_buffer_stop", - // bus, cars, bicycle - "bus_stop", "platform", "ferry_terminal", "taxi", "bicycle_rental", "bus_station", "car_rental", "car_sharing", - // aero - "airport", "aerodrome", "terminal", "gate", - // aerial ways ( hide ways) - // "aerialway_cable_car", "aerialway_gondola", "aerialway_chair_lift", "aerialway_mixed_lift", "aerialway_drag_lift", "aerialway_goods", - "aerialway_station" - // ways (hide ways) - // "rail", "tram", "light_rail", "subway", "railway_narrow_gauge", "railway_monorail", "railway_funicular" - ); - } else if (UDF_RESTAURANTS.equals(key)) { - putValues(poiTypes, types, "sustenance", "restaurant_not_found", - "cafe", "food_court", "pub", "bar", "biergarten"); - } else if (UDF_SIGHTSEEING.equals(key)) { - // TODO filter only sightseeing from tourimsm -// putValues(types, "tourism", "...","..."); - putAll(poiTypes, types, "tourism"); - putAll(poiTypes, types, "osmwiki"); - } else if (UDF_EMERGENCY.equals(key)) { - putAll(poiTypes, types, "healthcare"); - putAll(poiTypes, types, "emergency"); - } - return types; - } public void reloadAllPoiFilters() { - cacheOsmDefinedFilters = null; cacheTopStandardFilters = null; - cacheUserDefinedFilters = null; - getUserDefinedPoiFilters(); - getTopStandardFilters(); - getOsmDefinedPoiFilters(); + getTopDefinedPoiFilters(); + } + + + private List getUserDefinedPoiFilters() { + ArrayList userDefinedFilters = new ArrayList(); + PoiFilterDbHelper helper = openDbHelper(); + if (helper != null) { + List userDefined = helper.getFilters(helper.getReadableDatabase()); + userDefinedFilters.addAll(userDefined); + helper.close(); + } + return userDefinedFilters; + } + + public void sortListOfFilters(List list) { + final Collator instance = Collator.getInstance(); + Collections.sort(list, new Comparator() { + private int getRank(PoiLegacyFilter lf) { + if(PoiLegacyFilter.BY_NAME_FILTER_ID.equals(lf.getFilterId())) { + return 0; + } else if(lf.areAllTypesAccepted()) { + return 3; + } else if(PoiLegacyFilter.CUSTOM_FILTER_ID.equals(lf.getFilterId())) { + return 4; + } else if(PoiLegacyFilter.NAME_FINDER_FILTER_ID.equals(lf.getFilterId())) { + return 5; + } else if(lf.isStandardFilter()) { + return 2; + } + return 1; + } + + @Override + public int compare(PoiLegacyFilter lhs, PoiLegacyFilter rhs) { + int lr = getRank(lhs); + int rr = getRank(rhs); + if(lr != rr) { + return lr < rr ? -1 : 1; + } + return instance.compare(lhs.getName(), rhs.getName()); + } + }); } - - public List getUserDefinedPoiFilters(){ - if(cacheUserDefinedFilters == null){ - cacheUserDefinedFilters = new ArrayList(); - PoiFilterDbHelper helper = openDbHelper(); - if (helper != null) { - List userDefined = helper.getFilters(helper.getReadableDatabase()); - sortListOfFiltersByName(userDefined); - cacheUserDefinedFilters.addAll(userDefined); - helper.close(); - } - } - return Collections.unmodifiableList(cacheUserDefinedFilters); - } - private void sortListOfFiltersByName(List userDefined) { - final Collator instance = Collator.getInstance(); - Collections.sort(userDefined, new Comparator() { - @Override - public int compare(PoiLegacyFilter object1, PoiLegacyFilter object2) { - return instance.compare(object1.getName(), object2.getName()); - } - }); - } - - public List getTopStandardFilters() { + public List getTopDefinedPoiFilters() { if (cacheTopStandardFilters == null) { cacheTopStandardFilters = new ArrayList(); - + // by name PoiLegacyFilter filter = new SearchByNameFilter(application); filter.setStandardFilter(true); cacheTopStandardFilters.add(filter); - + // custom filter = new PoiLegacyFilter(application.getString(R.string.poi_filter_custom_filter), PoiLegacyFilter.CUSTOM_FILTER_ID, new LinkedHashMap>(), application); //$NON-NLS-1$ filter.setStandardFilter(true); cacheTopStandardFilters.add(filter); - + // all cacheTopStandardFilters.add(new PoiLegacyFilter(null, application)); + // name finder + cacheTopStandardFilters.add(getNameFinderPOIFilter()); + // user defined + cacheTopStandardFilters.addAll(getUserDefinedPoiFilters()); + // default + MapPoiTypes poiTypes = application.getPoiTypes(); + for (PoiFilter t : poiTypes.getTopVisibleFilters()) { + cacheTopStandardFilters.add(new PoiLegacyFilter(t, application)); + } + sortListOfFilters(cacheTopStandardFilters); } return Collections.unmodifiableList(cacheTopStandardFilters); } - public static String getOsmDefinedFilterId(PoiCategory t){ - return PoiLegacyFilter.STD_PREFIX + (t == null ? null : t.getKeyName()); - } - - public void updateFilters(boolean onlyAddFilters){ - PoiFilterDbHelper helper = openDbHelper(); - if(helper != null ) { - helper.upgradeFilters(helper.getWritableDatabase(), onlyAddFilters); - helper.close(); - } - } - - - public List getOsmDefinedPoiFilters(){ - if (cacheOsmDefinedFilters == null) { - cacheOsmDefinedFilters = new ArrayList(); - MapPoiTypes poiTypes = application.getPoiTypes(); - for (PoiCategory t : poiTypes.getCategories()) { - cacheOsmDefinedFilters.add(new PoiLegacyFilter(t, application)); - } - final Collator instance = Collator.getInstance(); - Collections.sort(cacheOsmDefinedFilters, new Comparator() { - @Override - public int compare(PoiLegacyFilter object1, PoiLegacyFilter object2) { - return instance.compare(object1.getName(), object2.getName()); - } - }); - } - return Collections.unmodifiableList(cacheOsmDefinedFilters); - } - private PoiFilterDbHelper openDbHelper(){ if(!application.getPoiTypes().isInit()) { return null; @@ -265,7 +161,7 @@ public class PoiFiltersHelper { } boolean res = helper.deleteFilter(helper.getWritableDatabase(), filter); if(res){ - cacheUserDefinedFilters.remove(filter); + cacheTopStandardFilters.remove(filter); } helper.close(); return res; @@ -278,8 +174,8 @@ public class PoiFiltersHelper { } boolean res = helper.addFilter(filter, helper.getWritableDatabase(), false); if(res){ - cacheUserDefinedFilters.add(filter); - sortListOfFiltersByName(cacheUserDefinedFilters); + cacheTopStandardFilters.add(filter); + sortListOfFilters(cacheTopStandardFilters); } helper.close(); return res; @@ -305,7 +201,7 @@ public class PoiFiltersHelper { public class PoiFilterDbHelper { public static final String DATABASE_NAME = "poi_filters"; //$NON-NLS-1$ - private static final int DATABASE_VERSION = 4; + private static final int DATABASE_VERSION = 5; private static final String FILTER_NAME = "poi_filters"; //$NON-NLS-1$ private static final String FILTER_COL_NAME = "name"; //$NON-NLS-1$ private static final String FILTER_COL_ID = "id"; //$NON-NLS-1$ @@ -365,45 +261,23 @@ public class PoiFiltersHelper { public void onCreate(SQLiteConnection conn) { conn.execSQL(FILTER_TABLE_CREATE); conn.execSQL(CATEGORIES_TABLE_CREATE); - upgradeFilters(conn, true); } - public void upgradeFilters(SQLiteConnection conn, boolean onlyAdd) { - List filters = PoiFilterDbHelper.this.getFilters( conn); - List def = getUserDefinedDefaultFilters(mapPoiTypes); - for(PoiLegacyFilter f : filters){ - PoiLegacyFilter std = null; - for(PoiLegacyFilter d : def){ - if(f.getFilterId().equals(d.getFilterId())){ - std = d; - break; - } - } - for(String toDel : DEL) { - if(f.getFilterId().equals(toDel)) { - deleteFilter(conn, f); - } - } - if(std != null){ - if(!onlyAdd){ - editFilter(conn, std); - } else { - updateName(conn, std); - } - def.remove(std); - } - } - for(PoiLegacyFilter d : def){ - addFilter(d, conn, false); - } - } public void onUpgrade(SQLiteConnection conn, int oldVersion, int newVersion) { - upgradeFilters(conn, false); + if(newVersion <= 5) { + deleteOldFilters(conn); + } conn.setVersion(newVersion); } - protected boolean addFilter(PoiLegacyFilter p, SQLiteConnection db, boolean addOnlyCategories){ + private void deleteOldFilters(SQLiteConnection conn) { + for (String toDel : DEL) { + deleteFilter(conn, "user_" + toDel); + } + } + + protected boolean addFilter(PoiLegacyFilter p, SQLiteConnection db, boolean addOnlyCategories){ if(db != null){ if(!addOnlyCategories){ db.execSQL("INSERT INTO " + FILTER_NAME + " VALUES (?, ?, ?)",new Object[]{p.getName(), p.getFilterId(), p.getFilterByName()}); //$NON-NLS-1$ //$NON-NLS-2$ @@ -493,13 +367,19 @@ public class PoiFiltersHelper { } protected boolean deleteFilter(SQLiteConnection db, PoiLegacyFilter p){ - if(db != null){ - db.execSQL("DELETE FROM " + FILTER_NAME + " WHERE " +FILTER_COL_ID + " = ?",new Object[]{p.getFilterId()}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - db.execSQL("DELETE FROM " + CATEGORIES_NAME + " WHERE " +CATEGORIES_FILTER_ID + " = ?", new Object[]{p.getFilterId()}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - return true; - } - return false; + String key = p.getFilterId(); + return deleteFilter(db, key); } + + private boolean deleteFilter(SQLiteConnection db, String key) { + if (db != null) { + db.execSQL("DELETE FROM " + FILTER_NAME + " WHERE " + FILTER_COL_ID + " = ?", new Object[] { key }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + db.execSQL( + "DELETE FROM " + CATEGORIES_NAME + " WHERE " + CATEGORIES_FILTER_ID + " = ?", new Object[] { key }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + return true; + } + return false; + } diff --git a/OsmAnd/src/net/osmand/plus/poi/PoiLegacyFilter.java b/OsmAnd/src/net/osmand/plus/poi/PoiLegacyFilter.java index 3e49761650..2a9a0b7706 100644 --- a/OsmAnd/src/net/osmand/plus/poi/PoiLegacyFilter.java +++ b/OsmAnd/src/net/osmand/plus/poi/PoiLegacyFilter.java @@ -15,7 +15,6 @@ import net.osmand.data.Amenity; import net.osmand.osm.MapPoiTypes; import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiFilter; -import net.osmand.osm.PoiType; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; @@ -28,6 +27,7 @@ public class PoiLegacyFilter { public final static String USER_PREFIX = "user_"; //$NON-NLS-1$ public final static String CUSTOM_FILTER_ID = USER_PREFIX + "custom_id"; //$NON-NLS-1$ public final static String BY_NAME_FILTER_ID = USER_PREFIX + "by_name"; //$NON-NLS-1$ + public static final String NAME_FINDER_FILTER_ID = "name_finder"; private Map> acceptedTypes = new LinkedHashMap>(); @@ -47,34 +47,20 @@ public class PoiLegacyFilter { // constructor for standard filters - public PoiLegacyFilter(PoiCategory type, OsmandApplication application){ + public PoiLegacyFilter(PoiFilter type, OsmandApplication application) { this.app = application; isStandardFilter = true; - filterId = STD_PREFIX + (type == null? null: type.getName()); + filterId = STD_PREFIX + (type == null ? null : type.getName()); poiTypes = application.getPoiTypes(); name = type == null ? application.getString(R.string.poi_filter_closest_poi) : type.getTranslation(); //$NON-NLS-1$ if (type == null) { initSearchAll(); } else { - acceptedTypes.put(type, null); - addReferenceTypes(type); + type.putTypes(acceptedTypes); } } - private void addReferenceTypes(PoiFilter type) { - for(PoiType pt : type.getPoiTypes()) { - if (pt.isReference()) { - PoiCategory refCat = pt.getReferenceType().getCategory(); - if (!acceptedTypes.containsKey(refCat)) { - acceptedTypes.put(refCat, new LinkedHashSet()); - } - LinkedHashSet ls = acceptedTypes.get(refCat); - if (ls != null) { - ls.add(pt.getKeyName()); - } - } - } - } + // constructor for user defined filters public PoiLegacyFilter(String name, String filterId, Map> acceptedTypes, OsmandApplication app){ diff --git a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java index ada8c62d1b..c4ca2dc6b8 100644 --- a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java @@ -466,7 +466,6 @@ public class ResourceManager { boolean isFirstInstall = context.getSettings().PREVIOUS_INSTALLED_VERSION.get().equals(""); unpackBundledAssets(assetManager, applicationDataDir, progress, isFirstInstall); context.getSettings().PREVIOUS_INSTALLED_VERSION.set(Version.getFullVersion(context)); - context.getPoiFilters().updateFilters(false); copyRegionsBoundaries(); } catch (SQLiteException e) { log.error(e.getMessage(), e);