From 89e14101b195ee89d943d33b09bf0ee66f8d9daf Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Tue, 5 Jul 2016 18:00:21 +0300 Subject: [PATCH] Formatting --- .../net/osmand/plus/poi/PoiFiltersHelper.java | 285 +++++++++--------- 1 file changed, 142 insertions(+), 143 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java index a05b36f279..1dad6050c5 100644 --- a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java +++ b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java @@ -32,10 +32,10 @@ import java.util.TreeSet; public class PoiFiltersHelper { private final OsmandApplication application; - + private NominatimPoiFilter nominatimPOIFilter; private NominatimPoiFilter nominatimAddressFilter; - + private PoiUIFilter searchByNamePOIFilter; private PoiUIFilter customPOIFilter; private PoiUIFilter showAllPOIFilter; @@ -58,36 +58,36 @@ public class PoiFiltersHelper { 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){ + + public PoiFiltersHelper(OsmandApplication application) { this.application = application; } - + public NominatimPoiFilter getNominatimPOIFilter() { - if(nominatimPOIFilter == null){ + if (nominatimPOIFilter == null) { nominatimPOIFilter = new NominatimPoiFilter(application, false); } return nominatimPOIFilter; } - + public NominatimPoiFilter getNominatimAddressFilter() { - if(nominatimAddressFilter == null){ + if (nominatimAddressFilter == null) { nominatimAddressFilter = new NominatimPoiFilter(application, true); } return nominatimAddressFilter; } - + public PoiUIFilter getSearchByNamePOIFilter() { - if(searchByNamePOIFilter == null){ + if (searchByNamePOIFilter == null) { PoiUIFilter filter = new SearchByNameFilter(application); filter.setStandardFilter(true); searchByNamePOIFilter = filter; } return searchByNamePOIFilter; } - + public PoiUIFilter getCustomPOIFilter() { - if (customPOIFilter == null){ + if (customPOIFilter == null) { PoiUIFilter filter = new PoiUIFilter(application.getString(R.string.poi_filter_custom_filter), PoiUIFilter.CUSTOM_FILTER_ID, new LinkedHashMap>(), application); //$NON-NLS-1$ filter.setStandardFilter(true); @@ -95,32 +95,32 @@ public class PoiFiltersHelper { } return customPOIFilter; } - + public PoiUIFilter getLocalWikiPOIFilter() { - if (localWikiPoiFilter == null){ + if (localWikiPoiFilter == null) { PoiType place = application.getPoiTypes().getPoiTypeByKey("wiki_place"); if (place != null && !Algorithms.isEmpty(application.getLanguage())) { PoiUIFilter filter = new PoiUIFilter(place, application, " " + application.getLangTranslation(application.getLanguage())); //$NON-NLS-1$ - filter.setSavedFilterByName("wiki:lang:"+application.getLanguage()); + filter.setSavedFilterByName("wiki:lang:" + application.getLanguage()); filter.setStandardFilter(true); localWikiPoiFilter = filter; } } return localWikiPoiFilter; } - + public PoiUIFilter getShowAllPOIFilter() { - if (showAllPOIFilter == null){ + if (showAllPOIFilter == null) { PoiUIFilter filter = new PoiUIFilter(null, application, ""); //$NON-NLS-1$ filter.setStandardFilter(true); showAllPOIFilter = filter; } return showAllPOIFilter; } - - - private PoiUIFilter getFilterById(String filterId, PoiUIFilter... filters){ + + + private PoiUIFilter getFilterById(String filterId, PoiUIFilter... filters) { for (PoiUIFilter pf : filters) { if (pf.getFilterId().equals(filterId)) { return pf; @@ -129,8 +129,8 @@ public class PoiFiltersHelper { return null; } - public PoiUIFilter getFilterById(String filterId){ - if (filterId == null){ + public PoiUIFilter getFilterById(String filterId) { + if (filterId == null) { return null; } for (PoiUIFilter f : getTopDefinedPoiFilters()) { @@ -219,15 +219,15 @@ public class PoiFiltersHelper { result.add(getShowAllPOIFilter()); return result; } - - private PoiFilterDbHelper openDbHelper(){ - if(!application.getPoiTypes().isInit()) { + + private PoiFilterDbHelper openDbHelper() { + if (!application.getPoiTypes().isInit()) { return null; } return new PoiFilterDbHelper(application.getPoiTypes(), application); } - - public boolean removePoiFilter(PoiUIFilter filter){ + + public boolean removePoiFilter(PoiUIFilter filter) { if (filter.getFilterId().equals(PoiUIFilter.CUSTOM_FILTER_ID) || filter.getFilterId().equals(PoiUIFilter.BY_NAME_FILTER_ID) || filter.getFilterId().startsWith(PoiUIFilter.STD_PREFIX)) { @@ -246,21 +246,21 @@ public class PoiFiltersHelper { helper.close(); return res; } - - public boolean createPoiFilter(PoiUIFilter filter){ + + public boolean createPoiFilter(PoiUIFilter filter) { PoiFilterDbHelper helper = openDbHelper(); - if(helper == null){ + if (helper == null) { return false; } boolean res = helper.deleteFilter(helper.getWritableDatabase(), filter); Iterator it = cacheTopStandardFilters.iterator(); - while(it.hasNext()) { - if(it.next().getFilterId().equals(filter.getFilterId())) { + while (it.hasNext()) { + if (it.next().getFilterId().equals(filter.getFilterId())) { it.remove(); } } res = helper.addFilter(filter, helper.getWritableDatabase(), false); - if(res){ + if (res) { ArrayList copy = new ArrayList<>(cacheTopStandardFilters); copy.add(filter); Collections.sort(copy); @@ -329,7 +329,7 @@ public class PoiFiltersHelper { } public boolean isPoiFilterSelected(String filterId) { - for (PoiUIFilter filter: selectedPoiFilters) { + for (PoiUIFilter filter : selectedPoiFilters) { if (filter.filterId.equals(filterId)) { return true; } @@ -339,60 +339,60 @@ public class PoiFiltersHelper { public void loadSelectedPoiFilters() { Set filters = application.getSettings().getSelectedPoiFilters(); - for (String f: filters) { + for (String f : filters) { selectedPoiFilters.add(getFilterById(f)); } } public void saveSelectedPoiFilters() { Set filters = new HashSet<>(); - for (PoiUIFilter f: selectedPoiFilters) { + for (PoiUIFilter f : selectedPoiFilters) { filters.add(f.filterId); } application.getSettings().setSelectedPoiFilters(filters); } - public class PoiFilterDbHelper { + public class PoiFilterDbHelper { public static final String DATABASE_NAME = "poi_filters"; //$NON-NLS-1$ - 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$ - private static final String FILTER_COL_FILTERBYNAME = "filterbyname"; //$NON-NLS-1$ - private static final String FILTER_TABLE_CREATE = "CREATE TABLE " + FILTER_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$ - FILTER_COL_NAME + ", " + FILTER_COL_ID + ", " + FILTER_COL_FILTERBYNAME + ");"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + 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$ + private static final String FILTER_COL_FILTERBYNAME = "filterbyname"; //$NON-NLS-1$ + private static final String FILTER_TABLE_CREATE = "CREATE TABLE " + FILTER_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$ + FILTER_COL_NAME + ", " + FILTER_COL_ID + ", " + FILTER_COL_FILTERBYNAME + ");"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - private static final String CATEGORIES_NAME = "categories"; //$NON-NLS-1$ - private static final String CATEGORIES_FILTER_ID = "filter_id"; //$NON-NLS-1$ - private static final String CATEGORIES_COL_CATEGORY = "category"; //$NON-NLS-1$ - private static final String CATEGORIES_COL_SUBCATEGORY = "subcategory"; //$NON-NLS-1$ - private static final String CATEGORIES_TABLE_CREATE = "CREATE TABLE " + CATEGORIES_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$ - CATEGORIES_FILTER_ID + ", " + CATEGORIES_COL_CATEGORY + ", " + CATEGORIES_COL_SUBCATEGORY + ");"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + private static final String CATEGORIES_NAME = "categories"; //$NON-NLS-1$ + private static final String CATEGORIES_FILTER_ID = "filter_id"; //$NON-NLS-1$ + private static final String CATEGORIES_COL_CATEGORY = "category"; //$NON-NLS-1$ + private static final String CATEGORIES_COL_SUBCATEGORY = "subcategory"; //$NON-NLS-1$ + private static final String CATEGORIES_TABLE_CREATE = "CREATE TABLE " + CATEGORIES_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$ + CATEGORIES_FILTER_ID + ", " + CATEGORIES_COL_CATEGORY + ", " + CATEGORIES_COL_SUBCATEGORY + ");"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ private OsmandApplication context; private SQLiteConnection conn; private MapPoiTypes mapPoiTypes; - PoiFilterDbHelper(MapPoiTypes mapPoiTypes, OsmandApplication context) { + PoiFilterDbHelper(MapPoiTypes mapPoiTypes, OsmandApplication context) { this.mapPoiTypes = mapPoiTypes; this.context = context; - } + } - public SQLiteConnection getWritableDatabase() { - return openConnection(false); + public SQLiteConnection getWritableDatabase() { + return openConnection(false); } public void close() { - if(conn != null) { + if (conn != null) { conn.close(); conn = null; } } public SQLiteConnection getReadableDatabase() { - return openConnection(true); - } + return openConnection(true); + } private SQLiteConnection openConnection(boolean readonly) { conn = context.getSQLiteAPI().getOrCreateDatabase(DATABASE_NAME, readonly); @@ -413,101 +413,101 @@ public class PoiFiltersHelper { } public void onCreate(SQLiteConnection conn) { - conn.execSQL(FILTER_TABLE_CREATE); - conn.execSQL(CATEGORIES_TABLE_CREATE); - } + conn.execSQL(FILTER_TABLE_CREATE); + conn.execSQL(CATEGORIES_TABLE_CREATE); + } + - public void onUpgrade(SQLiteConnection conn, int oldVersion, int newVersion) { - if(newVersion <= 5) { + if (newVersion <= 5) { deleteOldFilters(conn); } conn.setVersion(newVersion); } - private void deleteOldFilters(SQLiteConnection conn) { + private void deleteOldFilters(SQLiteConnection conn) { for (String toDel : DEL) { deleteFilter(conn, "user_" + toDel); - } + } } - protected boolean addFilter(PoiUIFilter 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$ - } - Map> types = p.getAcceptedTypes(); - SQLiteStatement insertCategories = db.compileStatement("INSERT INTO " + CATEGORIES_NAME + " VALUES (?, ?, ?)"); //$NON-NLS-1$ //$NON-NLS-2$ - for(PoiCategory a : types.keySet()){ - if(types.get(a) == null){ - insertCategories.bindString(1, p.getFilterId()); + protected boolean addFilter(PoiUIFilter 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$ + } + Map> types = p.getAcceptedTypes(); + SQLiteStatement insertCategories = db.compileStatement("INSERT INTO " + CATEGORIES_NAME + " VALUES (?, ?, ?)"); //$NON-NLS-1$ //$NON-NLS-2$ + for (PoiCategory a : types.keySet()) { + if (types.get(a) == null) { + insertCategories.bindString(1, p.getFilterId()); insertCategories.bindString(2, a.getKeyName()); insertCategories.bindNull(3); - insertCategories.execute(); - } else { - for(String s : types.get(a)){ - insertCategories.bindString(1, p.getFilterId()); - insertCategories.bindString(2, a.getKeyName()); - insertCategories.bindString(3, s); - insertCategories.execute(); - } - } - } - insertCategories.close(); - return true; - } - return false; - } + insertCategories.execute(); + } else { + for (String s : types.get(a)) { + insertCategories.bindString(1, p.getFilterId()); + insertCategories.bindString(2, a.getKeyName()); + insertCategories.bindString(3, s); + insertCategories.execute(); + } + } + } + insertCategories.close(); + return true; + } + return false; + } - protected List getFilters(SQLiteConnection conn){ - ArrayList list = new ArrayList(); - if(conn != null){ - SQLiteCursor query = conn.rawQuery("SELECT " + CATEGORIES_FILTER_ID +", " + CATEGORIES_COL_CATEGORY +"," + CATEGORIES_COL_SUBCATEGORY +" FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ - CATEGORIES_NAME, null); - Map>> map = new LinkedHashMap>>(); - if(query.moveToFirst()){ - do { - String filterId = query.getString(0); - if(!map.containsKey(filterId)){ - map.put(filterId, new LinkedHashMap>()); - } - Map> m = map.get(filterId); - PoiCategory a = mapPoiTypes.getPoiCategoryByName(query.getString(1).toLowerCase(), false); - String subCategory = query.getString(2); - if(subCategory == null){ - m.put(a, null); - } else { - if(m.get(a) == null){ - m.put(a, new LinkedHashSet()); - } - m.get(a).add(subCategory); - } - } while(query.moveToNext()); - } - query.close(); - - query = conn.rawQuery("SELECT " + FILTER_COL_ID +", " + FILTER_COL_NAME +"," + FILTER_COL_FILTERBYNAME +" FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ - FILTER_NAME, null); - if(query.moveToFirst()){ - do { - String filterId = query.getString(0); - if(map.containsKey(filterId)){ - PoiUIFilter filter = new PoiUIFilter(query.getString(1), filterId, - map.get(filterId), application); - filter.setSavedFilterByName(query.getString(2)); - list.add(filter); - } - } while(query.moveToNext()); - } - query.close(); - } - return list; - } + protected List getFilters(SQLiteConnection conn) { + ArrayList list = new ArrayList(); + if (conn != null) { + SQLiteCursor query = conn.rawQuery("SELECT " + CATEGORIES_FILTER_ID + ", " + CATEGORIES_COL_CATEGORY + "," + CATEGORIES_COL_SUBCATEGORY + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + CATEGORIES_NAME, null); + Map>> map = new LinkedHashMap>>(); + if (query.moveToFirst()) { + do { + String filterId = query.getString(0); + if (!map.containsKey(filterId)) { + map.put(filterId, new LinkedHashMap>()); + } + Map> m = map.get(filterId); + PoiCategory a = mapPoiTypes.getPoiCategoryByName(query.getString(1).toLowerCase(), false); + String subCategory = query.getString(2); + if (subCategory == null) { + m.put(a, null); + } else { + if (m.get(a) == null) { + m.put(a, new LinkedHashSet()); + } + m.get(a).add(subCategory); + } + } while (query.moveToNext()); + } + query.close(); - protected boolean editFilter(SQLiteConnection conn, PoiUIFilter filter) { + query = conn.rawQuery("SELECT " + FILTER_COL_ID + ", " + FILTER_COL_NAME + "," + FILTER_COL_FILTERBYNAME + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + FILTER_NAME, null); + if (query.moveToFirst()) { + do { + String filterId = query.getString(0); + if (map.containsKey(filterId)) { + PoiUIFilter filter = new PoiUIFilter(query.getString(1), filterId, + map.get(filterId), application); + filter.setSavedFilterByName(query.getString(2)); + list.add(filter); + } + } while (query.moveToNext()); + } + query.close(); + } + return list; + } + + protected boolean editFilter(SQLiteConnection conn, PoiUIFilter filter) { if (conn != null) { conn.execSQL("DELETE FROM " + CATEGORIES_NAME + " WHERE " + CATEGORIES_FILTER_ID + " = ?", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - new Object[] { filter.getFilterId() }); + new Object[]{filter.getFilterId()}); addFilter(filter, conn, true); updateName(conn, filter); return true; @@ -517,26 +517,25 @@ public class PoiFiltersHelper { private void updateName(SQLiteConnection db, PoiUIFilter filter) { db.execSQL("UPDATE " + FILTER_NAME + " SET " + FILTER_COL_FILTERBYNAME + " = ?, " + FILTER_COL_NAME + " = ? " + " WHERE " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ - + FILTER_COL_ID + "= ?", new Object[] { filter.getFilterByName(), filter.getName(), filter.getFilterId() }); //$NON-NLS-1$ + + FILTER_COL_ID + "= ?", new Object[]{filter.getFilterByName(), filter.getName(), filter.getFilterId()}); //$NON-NLS-1$ } - protected boolean deleteFilter(SQLiteConnection db, PoiUIFilter p){ - String key = p.getFilterId(); - return deleteFilter(db, key); - } + protected boolean deleteFilter(SQLiteConnection db, PoiUIFilter p) { + 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 " + 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$ + "DELETE FROM " + CATEGORIES_NAME + " WHERE " + CATEGORIES_FILTER_ID + " = ?", new Object[]{key}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ return true; } return false; } - }