diff --git a/OsmAnd-java/src/net/osmand/data/FavouritePoint.java b/OsmAnd-java/src/net/osmand/data/FavouritePoint.java index 1659f02d20..b92a2e9e9f 100644 --- a/OsmAnd-java/src/net/osmand/data/FavouritePoint.java +++ b/OsmAnd-java/src/net/osmand/data/FavouritePoint.java @@ -9,7 +9,7 @@ public class FavouritePoint implements Serializable { private double latitude; private double longitude; private int color; - + private boolean visible = true; public FavouritePoint(){ } @@ -28,6 +28,15 @@ public class FavouritePoint implements Serializable { public void setColor(int color) { this.color = color; } + + public boolean isVisible() { + return visible; + } + + public void setVisible(boolean visible) { + this.visible = visible; + } + public double getLatitude() { return latitude; @@ -65,4 +74,5 @@ public class FavouritePoint implements Serializable { public String toString() { return "Favourite " + getName(); //$NON-NLS-1$ } + } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java index 95ebb492e7..28af617ffe 100644 --- a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java @@ -3,9 +3,12 @@ package net.osmand.plus; import java.io.File; import java.text.Collator; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.TreeMap; +import java.util.Set; import net.osmand.PlatformUtil; import net.osmand.data.FavouritePoint; @@ -16,8 +19,360 @@ import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; public class FavouritesDbHelper { - private static final int DATABASE_VERSION = 2; private static final org.apache.commons.logging.Log log = PlatformUtil.getLog(FavouritesDbHelper.class); + + public static final String FILE_TO_SAVE = "favourites.gpx"; //$NON-NLS-1$ + public static final String FILE_TO_BACKUP = "favourites_bak.gpx"; //$NON-NLS-1$ + + private List cachedFavoritePoints = new ArrayList(); + private List favoriteGroups = new ArrayList(); + private Map flatGroups = new LinkedHashMap(); + private final OsmandApplication context; + protected static final String HIDDEN = "HIDDEN"; + private static final String DELIMETER = "__"; + + + public FavouritesDbHelper(OsmandApplication context) { + this.context = context; + } + + public static class FavoriteGroup { + public String name; + public boolean visible = true; + public int color; + public List points = new ArrayList(); + } + + public void loadFavorites() { + flatGroups.clear(); + favoriteGroups.clear(); + + File internalFile = getInternalFile(); + if(!internalFile.exists()) { + File dbPath = context.getDatabasePath(FAVOURITE_DB_NAME); + if(dbPath.exists()) { + loadAndCheckDatabasePoints(); + saveCurrentPointsIntoFile(); + } + //createDefaultCategories(); + } + Map points = new LinkedHashMap(); + Map extPoints = new LinkedHashMap(); + loadGPXFile(internalFile, points); + loadGPXFile(getExternalFile(), extPoints); + boolean changed = merge(extPoints, points); + + for(FavouritePoint pns : points.values()) { + FavoriteGroup group = getOrCreateGroup(pns, 0); + group.points.add(pns); + } + sortAll(); + recalculateCachedFavPoints(); + if(changed) { + saveCurrentPointsIntoFile(); + } + + } + + + + private boolean merge(Map source, Map destination) { + boolean changed = false; + for(String ks : source.keySet()) { + if(!destination.containsKey(ks)) { + changed = true; + destination.put(ks, source.get(ks)); + } + } + return changed; + } + + + + private File getInternalFile() { + return context.getFileStreamPath(FILE_TO_BACKUP); + } + + public void delete(Set groupsToDelete, Set favoritesSelected) { + if (favoritesSelected != null) { + for (FavouritePoint p : favoritesSelected) { + FavoriteGroup group = flatGroups.get(p.getCategory()); + if (group != null) { + group.points.remove(p); + } + cachedFavoritePoints.remove(p); + } + } + if (groupsToDelete != null) { + for (FavoriteGroup g : groupsToDelete) { + flatGroups.remove(g.name); + favoriteGroups.remove(g); + cachedFavoritePoints.removeAll(g.points); + } + } + saveCurrentPointsIntoFile(); + } + + + public boolean deleteFavourite(FavouritePoint p) { + if (p != null) { + FavoriteGroup group = flatGroups.get(p.getCategory()); + if (group != null) { + group.points.remove(p); + } + cachedFavoritePoints.remove(p); + } + saveCurrentPointsIntoFile(); + return true; + } + + + public boolean addFavourite(FavouritePoint p) { + if (p.getName().equals("") && flatGroups.containsKey(p.getCategory())) { + return true; + } + FavoriteGroup group = getOrCreateGroup(p, 0); + if (!p.getName().equals("")) { + p.setVisible(group.visible); + p.setColor(group.color); + group.points.add(p); + cachedFavoritePoints.add(p); + } + saveCurrentPointsIntoFile(); + return true; + } + + + + public boolean editFavouriteName(FavouritePoint p, String newName, String category) { + String oldCategory = p.getCategory(); + p.setName(newName); + p.setCategory(category); + if (!oldCategory.equals(category)) { + FavoriteGroup old = flatGroups.get(oldCategory); + if (old != null) { + old.points.remove(p); + } + FavoriteGroup pg = getOrCreateGroup(p, 0); + p.setVisible(pg.visible); + p.setColor(pg.color); + pg.points.add(p); + } + sortAll(); + saveCurrentPointsIntoFile(); + return true; + } + + + public boolean editFavourite(FavouritePoint p, double lat, double lon) { + p.setLatitude(lat); + p.setLongitude(lon); + saveCurrentPointsIntoFile(); + return true; + } + + private void saveCurrentPointsIntoFile() { + try { + Map ex = new LinkedHashMap(); + loadGPXFile(getInternalFile(), ex); + for(FavouritePoint fp : cachedFavoritePoints) { + ex.remove(getKey(fp)); + } + saveFile(cachedFavoritePoints, getInternalFile()); + saveExternalFile(ex.keySet()); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + + public String exportFavorites() { + return saveExternalFile(null); + } + + + + private String saveExternalFile(Set deleted) { + Map ex = new LinkedHashMap(); + loadGPXFile(getExternalFile(), ex); + List favoritePoints = new ArrayList(cachedFavoritePoints); + if(deleted != null) { + for(String key : deleted) { + ex.remove(key); + } + } + for(FavouritePoint p : favoritePoints) { + ex.remove(getKey(p)); + } + favoritePoints.addAll(ex.values()); + return saveFile(favoritePoints, getExternalFile()); + } + + + + private String getKey(FavouritePoint p) { + return p.getName() + DELIMETER + p.getCategory(); + } + + + + public boolean deleteGroup(FavoriteGroup group) { + boolean remove = favoriteGroups.remove(group); + if (remove) { + flatGroups.remove(group.name); + saveCurrentPointsIntoFile(); + return true; + } + return false; + } + + private File getExternalFile() { + return new File(context.getAppPath(null), FILE_TO_SAVE); + } + + public String saveFile(List favoritePoints, File f) { + GPXFile gpx = asGpxFile(favoritePoints); + return GPXUtilities.writeGpxFile(f, gpx, context); + } + + + public GPXFile asGpxFile() { + return asGpxFile(cachedFavoritePoints); + } + + private GPXFile asGpxFile(List favoritePoints) { + GPXFile gpx = new GPXFile(); + for (FavouritePoint p : favoritePoints) { + WptPt pt = new WptPt(); + pt.lat = p.getLatitude(); + pt.lon = p.getLongitude(); + if(!p.isVisible()) { + pt.getExtensionsToWrite().put(HIDDEN, "true"); + } + if(p.getColor() != 0) { + pt.getColor(p.getColor()); + } + pt.name = p.getName(); + if (p.getCategory().length() > 0) + pt.category = p.getCategory(); + gpx.points.add(pt); + } + return gpx; + } + + + private void addEmptyCategory(String name) { + FavoriteGroup group = new FavoriteGroup(); + group.name = name; + favoriteGroups.add(group); + flatGroups.put(name, group); + } + + public List getFavouritePoints() { + return cachedFavoritePoints; + } + + + public List getFavoriteGroups() { + return favoriteGroups; + } + + + private FavouritePoint findFavoriteByAllProperties(String category, String name, double lat, double lon){ + if (flatGroups.containsKey(category)) { + FavoriteGroup fg = flatGroups.get(category); + for (FavouritePoint fv : fg.points) { + if (name.equals(fv.getName()) && (lat == fv.getLatitude()) && (lon == fv.getLongitude())) { + return fv; + } + } + } + return null; + } + + + + private void recalculateCachedFavPoints(){ + ArrayList temp = new ArrayList(); + for(FavoriteGroup f : favoriteGroups){ + temp.addAll(f.points); + } + cachedFavoritePoints = temp; + } + + private void sortAll() { + final Collator collator = Collator.getInstance(); + collator.setStrength(Collator.SECONDARY); + Collections.sort(favoriteGroups, new Comparator() { + + @Override + public int compare(FavoriteGroup lhs, FavoriteGroup rhs) { + return collator.compare(lhs.name, rhs.name); + } + }); + Comparator favoritesComparator = new Comparator() { + + @Override + public int compare(FavouritePoint object1, FavouritePoint object2) { + return collator.compare(object1.getName(), object2.getName()); + } + }; + for(FavoriteGroup g : favoriteGroups) { + Collections.sort(g.points, favoritesComparator); + } + } + + + private String loadGPXFile(File file, Map points) { + GPXFile res = GPXUtilities.loadGPXFile(context, file); + if (res.warning != null) { + return res.warning; + } + for (WptPt p : res.points) { + int c; + String name = p.name; + String categoryName = p.category != null ? p.category : ""; + if (name == null) { + name = ""; + } + // old way to store the category, in name. + if ("".equals(categoryName.trim()) && (c = p.name.lastIndexOf('_')) != -1) { + categoryName = p.name.substring(c + 1); + name = p.name.substring(0, c); + } + FavouritePoint fp = new FavouritePoint(p.lat, p.lon, name, categoryName); + fp.setColor(p.getColor(0)); + fp.setVisible(!p.getExtensionsToRead().containsKey(HIDDEN)); + points.put(getKey(fp), fp); + } + return null; + } + + protected void createDefaultCategories() { + addEmptyCategory(context.getString(R.string.favorite_home_category)); + addEmptyCategory(context.getString(R.string.favorite_friends_category)); + addEmptyCategory(context.getString(R.string.favorite_places_category)); + addEmptyCategory(context.getString(R.string.favorite_default_category)); + } + + private FavoriteGroup getOrCreateGroup(FavouritePoint p, int defColor) { + if (flatGroups.containsKey(p.getCategory())) { + return flatGroups.get(p.getCategory()); + } + FavoriteGroup group = new FavoriteGroup(); + group.name = p.getCategory(); + group.visible = p.isVisible(); + group.color = p.getColor(); + flatGroups.put(group.name, group); + favoriteGroups.add(group); + if (group.color == 0) { + group.color = defColor; + } + return group; + } + + + /// Deprecated sqlite db + private static final int DATABASE_VERSION = 2; public static final String FAVOURITE_DB_NAME = "favourite"; //$NON-NLS-1$ private static final String FAVOURITE_TABLE_NAME = "favourite"; //$NON-NLS-1$ private static final String FAVOURITE_COL_NAME = "name"; //$NON-NLS-1$ @@ -27,18 +382,8 @@ public class FavouritesDbHelper { private static final String FAVOURITE_TABLE_CREATE = "CREATE TABLE " + FAVOURITE_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$ FAVOURITE_COL_NAME + " TEXT, " + FAVOURITE_COL_CATEGORY + " TEXT, " + //$NON-NLS-1$ //$NON-NLS-2$ FAVOURITE_COL_LAT + " double, " + FAVOURITE_COL_LON + " double);"; //$NON-NLS-1$ //$NON-NLS-2$ - - public static final String FILE_TO_SAVE = "favourites.gpx"; //$NON-NLS-1$ - public static final String FILE_TO_BACKUP = "favourites_bak.gpx"; //$NON-NLS-1$ - - private List cachedFavoritePoints = new ArrayList(); - private Map> favoriteGroups = null; - private final OsmandApplication context; private SQLiteConnection conn; - - public FavouritesDbHelper(OsmandApplication context) { - this.context = context; - } + private SQLiteConnection openConnection(boolean readonly) { conn = context.getSQLiteAPI().getOrCreateDatabase(FAVOURITE_DB_NAME, readonly); @@ -60,73 +405,104 @@ public class FavouritesDbHelper { public void onCreate(SQLiteConnection db) { db.execSQL(FAVOURITE_TABLE_CREATE); - createCategories(db); } public void onUpgrade(SQLiteConnection db, int oldVersion, int newVersion) { if(oldVersion == 1){ db.execSQL("ALTER TABLE " + FAVOURITE_TABLE_NAME + " ADD " + FAVOURITE_COL_CATEGORY + " text"); - createCategories(db); - db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET category = ?", new Object[] { context.getString(R.string.favorite_default_category)}); //$NON-NLS-1$ //$NON-NLS-2$ + db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET category = ?", new Object[] { "" }); //$NON-NLS-1$ //$NON-NLS-2$ } } - public void backupSilently() { - try { - exportFavorites(FILE_TO_BACKUP); - } catch (Exception e) { - log.error(e.getMessage(), e); + private void loadAndCheckDatabasePoints(){ + if (favoriteGroups == null) { + SQLiteConnection db = openConnection(true); + if (db != null) { + try { + SQLiteCursor query = db + .rawQuery( + "SELECT " + FAVOURITE_COL_NAME + ", " + FAVOURITE_COL_CATEGORY + ", " + FAVOURITE_COL_LAT + "," + FAVOURITE_COL_LON + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + FAVOURITE_TABLE_NAME, null); + cachedFavoritePoints.clear(); + if (query.moveToFirst()) { + do { + String name = query.getString(0); + String cat = query.getString(1); + + FavouritePoint p = new FavouritePoint(); + p.setName(name); + p.setCategory(cat); + FavoriteGroup group = getOrCreateGroup(p, 0); + if (!name.equals("")) { + p.setLatitude(query.getDouble(2)); + p.setLongitude(query.getDouble(3)); + group.points.add(p); + } + } while (query.moveToNext()); + } + query.close(); + } finally { + db.close(); + } + sortAll(); + } + recalculateCachedFavPoints(); } } - public String exportFavorites() { - return exportFavorites(FILE_TO_SAVE); - } - - public String exportFavorites(String fileName) { - File f = new File(context.getAppPath(null), fileName); - GPXFile gpx = asGpxFile(); - return GPXUtilities.writeGpxFile(f, gpx, context); - } - - public GPXFile asGpxFile() { - GPXFile gpx = new GPXFile(); - for (FavouritePoint p : getFavouritePoints()) { - WptPt pt = new WptPt(); - pt.lat = p.getLatitude(); - pt.lon = p.getLongitude(); - pt.name = p.getName(); - if (p.getCategory().length() > 0) - pt.category = p.getCategory(); - gpx.points.add(pt); + public boolean deleteFavouriteDB(FavouritePoint p) { + SQLiteConnection db = openConnection(false); + if (db != null) { + try { + db.execSQL( + "DELETE FROM " + FAVOURITE_TABLE_NAME + " WHERE category = ? AND " + whereNameLatLon(), new Object[] { p.getCategory(), p.getName(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$ + FavouritePoint fp = findFavoriteByAllProperties(p.getCategory(), p.getName(), p.getLatitude(), p.getLongitude()); + if (fp != null) { + FavoriteGroup group = flatGroups.get(p.getCategory()); + if(group != null) { + group.points.remove(fp); + } + cachedFavoritePoints.remove(fp); + } + saveCurrentPointsIntoFile(); + } finally{ + db.close(); + } + return true; } - return gpx; + return false; } - private void createCategories(SQLiteConnection db){ - addCategoryQuery(context.getString(R.string.favorite_home_category), db); - addCategoryQuery(context.getString(R.string.favorite_friends_category), db); - addCategoryQuery(context.getString(R.string.favorite_places_category), db); - addCategoryQuery(context.getString(R.string.favorite_default_category), db); - } - - - - - public List getFavouritePoints() { - checkFavoritePoints(); - return cachedFavoritePoints; + public boolean addFavouriteDB(FavouritePoint p) { + if(p.getName().equals("") && flatGroups.containsKey(p.getCategory())){ + return true; + } + SQLiteConnection db = openConnection(false); + if (db != null) { + try { + db.execSQL( + "INSERT INTO " + FAVOURITE_TABLE_NAME + " (" + FAVOURITE_COL_NAME + ", " + FAVOURITE_COL_CATEGORY + ", " + + FAVOURITE_COL_LAT + ", " + FAVOURITE_COL_LON + ")" + " VALUES (?, ?, ?, ?)", new Object[] { p.getName(), p.getCategory(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$ + FavoriteGroup group = getOrCreateGroup(p, 0); + if (!p.getName().equals("")) { + p.setVisible(group.visible); + p.setColor(group.color); + group.points.add(p); + cachedFavoritePoints.add(p); + } + saveCurrentPointsIntoFile(); + } finally { + db.close(); + } + return true; + } + return false; } - public Map> getFavoriteGroups() { - checkFavoritePoints(); - return favoriteGroups; - } - public boolean editFavouriteName(FavouritePoint p, String newName, String category) { - checkFavoritePoints(); + public boolean editFavouriteNameDB(FavouritePoint p, String newName, String category) { SQLiteConnection db = openConnection(false); if (db != null) { try { @@ -136,14 +512,35 @@ public class FavouritesDbHelper { p.setName(newName); p.setCategory(category); if (!oldCategory.equals(category)) { - favoriteGroups.get(oldCategory).remove(p); - if (!favoriteGroups.containsKey(category)) { - addCategoryQuery(category, db); - favoriteGroups.put(category, new ArrayList()); + FavoriteGroup old = flatGroups.get(oldCategory); + if (old != null) { + old.points.remove(p); } - favoriteGroups.get(category).add(p); + FavoriteGroup pg = getOrCreateGroup(p, 0); + p.setVisible(pg.visible); + p.setColor(pg.color); + pg.points.add(p); } - }finally { + sortAll(); + } finally { + db.close(); + } + return true; + } + return false; + } + + + public boolean editFavouriteDB(FavouritePoint p, double lat, double lon) { + SQLiteConnection db = openConnection(false); + if (db != null) { + try { + db.execSQL( + "UPDATE " + FAVOURITE_TABLE_NAME + " SET latitude = ?, longitude = ? WHERE " + whereNameLatLon(), new Object[] { lat, lon, p.getName(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$ + p.setLatitude(lat); + p.setLongitude(lon); + saveCurrentPointsIntoFile(); + } finally { db.close(); } return true; @@ -156,147 +553,4 @@ public class FavouritesDbHelper { return singleFavourite; } - public boolean editFavourite(FavouritePoint p, double lat, double lon) { - checkFavoritePoints(); - SQLiteConnection db = openConnection(false); - if (db != null) { - try { - db.execSQL( - "UPDATE " + FAVOURITE_TABLE_NAME + " SET latitude = ?, longitude = ? WHERE " + whereNameLatLon(), new Object[] { lat, lon, p.getName(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$ - p.setLatitude(lat); - p.setLongitude(lon); - backupSilently(); - } finally { - db.close(); - } - return true; - } - return false; - } - - private FavouritePoint findFavoriteByAllProperties(String category, String name, double lat, double lon){ - if (favoriteGroups.containsKey(category)) { - for (FavouritePoint fv : favoriteGroups.get(category)) { - if (name.equals(fv.getName()) && (lat == fv.getLatitude()) && (lon == fv.getLongitude())) { - return fv; - } - } - } - return null; - } - - public boolean deleteFavourite(FavouritePoint p) { - checkFavoritePoints(); - SQLiteConnection db = openConnection(false); - if (db != null) { - try { - db.execSQL( - "DELETE FROM " + FAVOURITE_TABLE_NAME + " WHERE category = ? AND " + whereNameLatLon(), new Object[] { p.getCategory(), p.getName(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$ - FavouritePoint fp = findFavoriteByAllProperties(p.getCategory(), p.getName(), p.getLatitude(), p.getLongitude()); - if (fp != null) { - favoriteGroups.get(p.getCategory()).remove(fp); - cachedFavoritePoints.remove(fp); - } - backupSilently(); - } finally{ - db.close(); - } - return true; - } - return false; - } - - public boolean deleteGroup(String group){ - checkFavoritePoints(); - FavouritePoint fp = new FavouritePoint(0, 0, "", group); - if(deleteFavourite(fp)){ - favoriteGroups.remove(group); - backupSilently(); - } - return false; - } - - public boolean addFavourite(FavouritePoint p) { - checkFavoritePoints(); - if(p.getName().equals("") && favoriteGroups.containsKey(p.getCategory())){ - return true; - } - SQLiteConnection db = openConnection(false); - if (db != null) { - try { - db.execSQL( - "INSERT INTO " + FAVOURITE_TABLE_NAME + " (" + FAVOURITE_COL_NAME + ", " + FAVOURITE_COL_CATEGORY + ", " - + FAVOURITE_COL_LAT + ", " + FAVOURITE_COL_LON + ")" + " VALUES (?, ?, ?, ?)", new Object[] { p.getName(), p.getCategory(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$ - if (!favoriteGroups.containsKey(p.getCategory())) { - favoriteGroups.put(p.getCategory(), new ArrayList()); - if (!p.getName().equals("")) { - addFavourite(new FavouritePoint(0, 0, "", p.getCategory())); - } - } - if (!p.getName().equals("")) { - favoriteGroups.get(p.getCategory()).add(p); - cachedFavoritePoints.add(p); - } - backupSilently(); - } finally { - db.close(); - } - return true; - } - return false; - } - - private void addCategoryQuery(String category, SQLiteConnection db) { - db.execSQL("INSERT INTO " + FAVOURITE_TABLE_NAME + - " (" +FAVOURITE_COL_NAME +", " +FAVOURITE_COL_CATEGORY +", " +FAVOURITE_COL_LAT +", " +FAVOURITE_COL_LON + ")" + - " VALUES (?, ?, ?, ?)", new Object[] { "", category, 0f, 0f }); //$NON-NLS-1$ //$NON-NLS-2$ - } - - - private void recalculateCachedFavPoints(){ - ArrayList temp = new ArrayList(); - for(List f : favoriteGroups.values()){ - temp.addAll(f); - } - cachedFavoritePoints = temp; - } - - private void checkFavoritePoints(){ - if(favoriteGroups == null){ - favoriteGroups = new TreeMap>(Collator.getInstance()); - SQLiteConnection db = openConnection(true); - if (db != null) { - try { - SQLiteCursor query = db.rawQuery("SELECT " + FAVOURITE_COL_NAME + ", " + FAVOURITE_COL_CATEGORY + ", " + FAVOURITE_COL_LAT + "," + FAVOURITE_COL_LON + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ - FAVOURITE_TABLE_NAME, null); - cachedFavoritePoints.clear(); - if (query.moveToFirst()) { - do { - String name = query.getString(0); - String cat = query.getString(1); - if(!favoriteGroups.containsKey(cat)){ - favoriteGroups.put(cat, new ArrayList()); - } - if (!name.equals("")) { - FavouritePoint p = new FavouritePoint(); - p.setName(name); - p.setCategory(cat); - p.setLatitude(query.getDouble(2)); - p.setLongitude(query.getDouble(3)); - favoriteGroups.get(cat).add(p); - } - } while (query.moveToNext()); - } - query.close(); - } finally { - db.close(); - } - } - recalculateCachedFavPoints(); - } - } - - - - } diff --git a/OsmAnd/src/net/osmand/plus/GPXUtilities.java b/OsmAnd/src/net/osmand/plus/GPXUtilities.java index 262d21147d..4318f63c3a 100644 --- a/OsmAnd/src/net/osmand/plus/GPXUtilities.java +++ b/OsmAnd/src/net/osmand/plus/GPXUtilities.java @@ -29,6 +29,7 @@ import java.util.TimeZone; import net.osmand.Location; import net.osmand.PlatformUtil; +import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; import org.xmlpull.v1.XmlPullParser; @@ -66,6 +67,10 @@ public class GPXUtilities { } return defColor; } + + public void setColor(int color) { + getExtensionsToWrite().put("color", Algorithms.colorToString(color)); + } public Map getExtensionsToWrite() { if (extensions == null) { @@ -753,6 +758,10 @@ public class GPXUtilities { fis = new FileInputStream(f); GPXFile file = loadGPXFile(ctx, fis); file.path = f.getAbsolutePath(); + try { + fis.close(); + } catch (IOException e) { + } return file; } catch (FileNotFoundException e) { GPXFile res = new GPXFile(); diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index 68f226a255..00cd74f8e3 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -149,6 +149,7 @@ public class OsmandApplication extends Application { savingTrackHelper = new SavingTrackHelper(this); liveMonitoringHelper = new LiveMonitoringHelper(this); selectedGpxHelper = new GpxSelectionHelper(this); + favorites = new FavouritesDbHelper(this); uiHandler = new Handler(); rendererRegistry = new RendererRegistry(); targetPointsHelper = new TargetPointsHelper(this); @@ -234,9 +235,6 @@ public class OsmandApplication extends Application { } public FavouritesDbHelper getFavorites() { - if (favorites == null) { - favorites = new FavouritesDbHelper(this); - } return favorites; } @@ -506,6 +504,7 @@ public class OsmandApplication extends Application { private void startApplicationBackground() { List warnings = new ArrayList(); try { + favorites.loadFavorites(); try { SpecialPhrases.setLanguage(this, osmandSettings); } catch (IOException e) { diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index fed1eedf40..e594758614 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -91,6 +91,7 @@ public class OsmandSettings { // These settings are stored in SharedPreferences private static final String SHARED_PREFERENCES_NAME = "net.osmand.settings"; //$NON-NLS-1$ + /// Settings variables private final OsmandApplication ctx; @@ -612,6 +613,9 @@ public class OsmandSettings { public final OsmandPreference AVAILABLE_APP_MODES = new StringPreference("available_application_modes", "car,bicycle,pedestrian,").makeGlobal().cache(); + public final OsmandPreference LAST_FAV_CATEGORY_ENTERED = new StringPreference("last_fav_category", "").makeGlobal(); + + public final OsmandPreference DEFAULT_APPLICATION_MODE = new CommonPreference("default_application_mode_string", ApplicationMode.DEFAULT) { { makeGlobal(); diff --git a/OsmAnd/src/net/osmand/plus/activities/FavouritesTreeFragment.java b/OsmAnd/src/net/osmand/plus/activities/FavouritesTreeFragment.java index 6da9373458..0d01d3d991 100644 --- a/OsmAnd/src/net/osmand/plus/activities/FavouritesTreeFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/FavouritesTreeFragment.java @@ -1,20 +1,14 @@ package net.osmand.plus.activities; import java.io.File; -import java.text.Collator; import java.text.MessageFormat; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; -import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; -import java.util.TreeMap; import net.londatiga.android.ActionItem; import net.londatiga.android.QuickAction; @@ -22,9 +16,9 @@ import net.osmand.access.AccessibleToast; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.plus.FavouritesDbHelper; +import net.osmand.plus.FavouritesDbHelper.FavoriteGroup; import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities.GPXFile; -import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; @@ -61,14 +55,15 @@ import com.actionbarsherlock.view.ActionMode.Callback; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; +import com.actionbarsherlock.view.MenuItem.OnActionExpandListener; import com.actionbarsherlock.widget.SearchView; import com.actionbarsherlock.widget.SearchView.OnQueryTextListener; public class FavouritesTreeFragment extends OsmandExpandableListFragment { public static final int SEARCH_ID = -1; - public static final int EXPORT_ID = 0; - public static final int IMPORT_ID = 1; +// public static final int EXPORT_ID = 0; + // public static final int IMPORT_ID = 1; public static final int DELETE_ID = 2; public static final int DELETE_ACTION_ID = 3; public static final int SHARE_ID = 4; @@ -80,29 +75,20 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { private boolean selectionMode = false; private Set favoritesSelected = new LinkedHashSet(); - private Set groupsToDelete = new LinkedHashSet(); - private Comparator favoritesComparator; + private Set groupsToDelete = new LinkedHashSet(); private ActionMode actionMode; private SearchView searchView; protected boolean hideActionBar; + private int defColor; @Override public void onAttach(Activity activity) { super.onAttach(activity); - final Collator collator = Collator.getInstance(); - collator.setStrength(Collator.SECONDARY); - favoritesComparator = new Comparator() { - - @Override - public int compare(FavouritePoint object1, FavouritePoint object2) { - return collator.compare(object1.getName(), object2.getName()); - } - }; - helper = getMyApplication().getFavorites(); favouritesAdapter = new FavouritesAdapter(); - favouritesAdapter.setFavoriteGroups(helper.getFavoriteGroups()); + defColor = getResources().getColor(R.color.color_favorite); + favouritesAdapter.synchronizeGroups(); setAdapter(favouritesAdapter); } @@ -118,31 +104,12 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { protected void onPostExecute(String result) { hideProgressBar(); favouritesAdapter.synchronizeGroups(); - favouritesAdapter.sort(favoritesComparator); - } - - @Override - protected void onProgressUpdate(Object... values) { - for (Object o : values) { - if (o instanceof FavouritePoint) { - favouritesAdapter.deleteFavoritePoint((FavouritePoint) o); - } else if (o instanceof String) { - favouritesAdapter.deleteCategory((String) o); - } - } } @Override protected String doInBackground(Void... params) { - for (FavouritePoint fp : favoritesSelected) { - helper.deleteFavourite(fp); - publishProgress(fp); - } + helper.delete(groupsToDelete, favoritesSelected); favoritesSelected.clear(); - for (String group : groupsToDelete) { - helper.deleteGroup(group); - publishProgress(group); - } groupsToDelete.clear(); return getString(R.string.favourites_delete_multiple_succesful); } @@ -157,8 +124,6 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { // final LatLon mapLocation = getMyApplication().getSettings().getLastKnownMapLocation(); favouritesAdapter.synchronizeGroups(); - // Sort Favs by distance on Search tab, but sort alphabetically here - favouritesAdapter.sort(favoritesComparator); if(favouritesAdapter.getGroupCount() > 0 && "".equals(favouritesAdapter.getGroup(0))) { getListView().expandGroup(0); @@ -240,17 +205,20 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { editText.setText(point.getName()); cat.setText(point.getCategory()); cat.setThreshold(1); - cat.setAdapter(new ArrayAdapter(getActivity(), R.layout.list_textview, helper.getFavoriteGroups() - .keySet().toArray(new String[] {}))); + List gs = helper.getFavoriteGroups(); + String[] list = new String[gs.size()]; + for(int i = 0; i < list.length; i++) { + list[i] =gs.get(i).name; + } + cat.setAdapter(new ArrayAdapter(getActivity(), R.layout.list_textview, list)); builder.setNegativeButton(R.string.default_buttons_cancel, null); builder.setPositiveButton(R.string.default_buttons_apply, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - boolean editied = helper.editFavouriteName(point, editText.getText().toString().trim(), cat.getText() + boolean edited = helper.editFavouriteName(point, editText.getText().toString().trim(), cat.getText() .toString()); - if (editied) { + if (edited) { favouritesAdapter.synchronizeGroups(); - favouritesAdapter.sort(favoritesComparator); } } @@ -275,7 +243,6 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { MessageFormat.format(resources.getString(R.string.favourites_remove_dialog_success), point.getName()), Toast.LENGTH_SHORT).show(); favouritesAdapter.synchronizeGroups(); - favouritesAdapter.sort(favoritesComparator); } } @@ -286,13 +253,11 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { @Override public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) { - if (item.getItemId() == EXPORT_ID) { - export(); - return true; - } else if (item.getItemId() == IMPORT_ID) { - importFile(); - return true; - } else if (item.getItemId() == SELECT_DESTINATIONS_ID) { +// if (item.getItemId() == EXPORT_ID) { +// export(); +// return true; +// } else + if (item.getItemId() == SELECT_DESTINATIONS_ID) { selectDestinations(); return true; } else if (item.getItemId() == SHARE_ID) { @@ -355,18 +320,21 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { return true; } }); -// mi.setOnActionExpandListener(new OnActionExpandListener() { -// -// @Override -// public boolean onMenuItemActionExpand(MenuItem item) { -// return true; -// } -// -// @Override -// public boolean onMenuItemActionCollapse(MenuItem item) { -// return true; -// } -// }); + mi.setOnActionExpandListener(new OnActionExpandListener() { + + @Override + public boolean onMenuItemActionExpand(MenuItem item) { + return true; + } + + @Override + public boolean onMenuItemActionCollapse(MenuItem item) { + favouritesAdapter.setFilterResults(null); + favouritesAdapter.synchronizeGroups(); + favouritesAdapter.notifyDataSetChanged(); + return true; + } + }); if (!mi.isActionViewExpanded()) { createMenuItem(menu, SHARE_ID, R.string.share_fav, R.drawable.ic_action_gshare_light, R.drawable.ic_action_gshare_dark, MenuItem.SHOW_AS_ACTION_IF_ROOM); @@ -374,11 +342,8 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { R.drawable.ic_action_flage_dark, MenuItem.SHOW_AS_ACTION_IF_ROOM); createMenuItem(menu, DELETE_ID, R.string.default_buttons_delete, R.drawable.ic_action_delete_light, R.drawable.ic_action_delete_dark, MenuItem.SHOW_AS_ACTION_IF_ROOM); - createMenuItem(menu, EXPORT_ID, R.string.export_fav, R.drawable.ic_action_gsave_light, - R.drawable.ic_action_gsave_dark, MenuItem.SHOW_AS_ACTION_IF_ROOM); - createMenuItem(menu, IMPORT_ID, R.string.import_fav, R.drawable.ic_action_grefresh_light, - R.drawable.ic_action_grefresh_dark, MenuItem.SHOW_AS_ACTION_IF_ROOM - | MenuItem.SHOW_AS_ACTION_WITH_TEXT); +// createMenuItem(menu, EXPORT_ID, R.string.export_fav, R.drawable.ic_action_gsave_light, +// R.drawable.ic_action_gsave_dark, MenuItem.SHOW_AS_ACTION_IF_ROOM); } } @@ -485,6 +450,11 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { }); } + + protected void openChangeGroupDialog() { + // TODO Auto-generated method stub + + } private void deleteFavoritesAction() { if (groupsToDelete.size() + favoritesSelected.size() > 0) { @@ -513,49 +483,13 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { Toast.LENGTH_LONG).show(); } else { new AsyncTask() { + @Override protected String doInBackground(Void... params) { - Set existedPoints = new LinkedHashSet(); - if (!favouritesAdapter.isEmpty()) { - for (FavouritePoint fp : helper.getFavouritePoints()) { - existedPoints.add(fp.getName() + "_" + fp.getCategory()); - } - } - GPXFile res = GPXUtilities.loadGPXFile(getMyApplication(), tosave); - if (res.warning != null) { - return res.warning; - } - for (WptPt p : res.points) { - if (existedPoints.contains(p.name) || existedPoints.contains(p.name + "_" + p.category)) { - continue; - } - int c; - String name = p.name; - String categoryName = p.category != null ? p.category : ""; - if (name == null) { - name = ""; - } - // old way to store the category, in name. - if ("".equals(categoryName.trim()) && (c = p.name.lastIndexOf('_')) != -1) { - categoryName = p.name.substring(c + 1); - name = p.name.substring(0, c); - } - FavouritePoint fp = new FavouritePoint(p.lat, p.lon, name, categoryName); - fp.setColor(p.getColor(0)); - if (helper.addFavourite(fp)) { - publishProgress(fp); - } - } - return null; + // helper.loadGPXFile(getMyApplication(), tosave, this); + return null ; } - @Override - protected void onProgressUpdate(FavouritePoint... values) { - for (FavouritePoint p : values) { - favouritesAdapter.addFavoritePoint(p); - } - }; - @Override protected void onPreExecute() { showProgressBar(); @@ -571,7 +505,6 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { AccessibleToast.makeText(getActivity(), warning, Toast.LENGTH_LONG).show(); } favouritesAdapter.synchronizeGroups(); - favouritesAdapter.sort(favoritesComparator); }; }.execute(); @@ -657,36 +590,15 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { } } } + class FavouritesAdapter extends OsmandBaseExpandableListAdapter implements Filterable { - Map> sourceFavoriteGroups; - Map> favoriteGroups = new LinkedHashMap>(); - List groups = new ArrayList(); + Map> favoriteGroups = new LinkedHashMap>(); + List groups = new ArrayList(); Filter myFilter; + private Set filter; - - public void setFavoriteGroups(Map> favoriteGroups) { - this.sourceFavoriteGroups = favoriteGroups; - synchronizeGroups(); - } - - public void sort(Comparator comparator) { - for (List ps : favoriteGroups.values()) { - Collections.sort(ps, comparator); - } - } - - public void addFavoritePoint(FavouritePoint p) { - - if (!favoriteGroups.containsKey(p.getCategory())) { - favoriteGroups.put(p.getCategory(), new ArrayList()); - groups.add(p.getCategory()); - } - favoriteGroups.get(p.getCategory()).add(p); - notifyDataSetChanged(); - } - public void deleteFavoritePoint(FavouritePoint p) { if (favoriteGroups.containsKey(p.getCategory())) { favoriteGroups.get(p.getCategory()).remove(p); @@ -703,9 +615,26 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { public void synchronizeGroups() { favoriteGroups.clear(); groups.clear(); - for (String key : sourceFavoriteGroups.keySet()) { - groups.add(key); - favoriteGroups.put(key, new ArrayList(sourceFavoriteGroups.get(key))); + List gs = helper.getFavoriteGroups(); + Set flt = filter; + for (FavoriteGroup key : gs) { + boolean empty = true; + if (flt == null || flt.contains(key)) { + empty = false; + favoriteGroups.put(key, new ArrayList(key.points)); + } else { + ArrayList list = new ArrayList(); + for (FavouritePoint p : key.points) { + if (flt.contains(p)) { + list.add(p); + empty = false; + } + } + favoriteGroups.put(key, list); + } + if(!empty) { + groups.add(key); + } } notifyDataSetChanged(); } @@ -726,7 +655,7 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { } @Override - public String getGroup(int groupPosition) { + public FavoriteGroup getGroup(int groupPosition) { return groups.get(groupPosition); } @@ -753,18 +682,22 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View row = convertView; - if (row == null) { + boolean checkBox = row != null && row.findViewById(R.id.check_item) instanceof CheckBox; + boolean same = (selectionMode && checkBox) || (!selectionMode && !checkBox); + if (row == null || !same) { LayoutInflater inflater = getActivity().getLayoutInflater(); - row = inflater.inflate(R.layout.expandable_list_item_category, parent, false); + row = inflater.inflate( + selectionMode ? R.layout.expandable_list_item_category : + R.layout.expandable_list_item_category_btn, parent, false); fixBackgroundRepeat(row); } adjustIndicator(groupPosition, isExpanded, row); TextView label = (TextView) row.findViewById(R.id.category_name); - final String model = getGroup(groupPosition); - label.setText(model.length() == 0? getString(R.string.favourites_activity) : model); - final CheckBox ch = (CheckBox) row.findViewById(R.id.check_item); + final FavoriteGroup model = getGroup(groupPosition); + label.setText(model.name.length() == 0? getString(R.string.favourites_activity) : model.name); if (selectionMode) { + final CheckBox ch = (CheckBox) row.findViewById(R.id.check_item); ch.setVisibility(View.VISIBLE); ch.setChecked(groupsToDelete.contains(model)); @@ -773,7 +706,7 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { public void onClick(View v) { if (ch.isChecked()) { groupsToDelete.add(model); - List fvs = helper.getFavoriteGroups().get(model); + List fvs = model.points; if (fvs != null) { favoritesSelected.addAll(fvs); } @@ -785,11 +718,23 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { } }); } else { - ch.setVisibility(View.GONE); + final ImageView ch = (ImageView) row.findViewById(R.id.check_item); + ch.setVisibility(View.VISIBLE); + ch.setImageDrawable(getActivity().getResources().getDrawable( + getMyApplication().getSettings().isLightContent() ? R.drawable.ic_action_settings_light + : R.drawable.ic_action_settings_dark)); + ch.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + openChangeGroupDialog(); + } + + }); } return row; } + @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { @@ -847,6 +792,11 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { } return myFilter; } + + public void setFilterResults(Set values) { + this.filter = values; + + } } public class FavoritesFilter extends Filter { @@ -859,33 +809,20 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); if (constraint == null || constraint.length() == 0) { - results.values = helper.getFavoriteGroups(); + results.values = null; results.count = 1; } else { - TreeMap> filter = new TreeMap>(helper.getFavoriteGroups()); - TreeMap> filterLists = new TreeMap>(); + Set filter = new HashSet(); String cs = constraint.toString().toLowerCase(); - Iterator>> ti = filter.entrySet().iterator(); - while(ti.hasNext()) { - Entry> next = ti.next(); - if(next.getKey().toLowerCase().indexOf(cs) == -1) { - ti.remove(); - filterLists.put(next.getKey(), next.getValue()); - } - } - ti = filterLists.entrySet().iterator(); - while(ti.hasNext()) { - Entry> next = ti.next(); - final List list = next.getValue(); - LinkedList ll = new LinkedList(); - for(FavouritePoint l : list) { - if(l.getName().toLowerCase().indexOf(cs) != -1) { - ll.add(l); + for(FavoriteGroup g : helper.getFavoriteGroups()) { + if(g.name.toLowerCase().indexOf(cs) != -1) { + filter.add(g); + } else { + for(FavouritePoint fp : g.points) { + if(fp.getName().toLowerCase().indexOf(cs) != -1) { + filter.add(fp); + } } - - } - if(ll.size() > 0) { - filter.put(next.getKey(), ll); } } results.values = filter; @@ -894,11 +831,11 @@ public class FavouritesTreeFragment extends OsmandExpandableListFragment { return results; } - @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { synchronized (favouritesAdapter) { - favouritesAdapter.setFavoriteGroups((Map>) results.values); + favouritesAdapter.setFilterResults((Set) results.values); + favouritesAdapter.synchronizeGroups(); } favouritesAdapter.notifyDataSetChanged(); if(constraint != null && constraint.length() > 1) { diff --git a/OsmAnd/src/net/osmand/plus/activities/MainMenuActivity.java b/OsmAnd/src/net/osmand/plus/activities/MainMenuActivity.java index b8e495ccc0..608cce7f28 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MainMenuActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MainMenuActivity.java @@ -322,10 +322,10 @@ public class MainMenuActivity extends Activity { } if(netOsmandWasInstalled){ - Builder builder = new AccessibleAlertBuilder(this); - builder.setMessage(R.string.osmand_net_previously_installed); - builder.setPositiveButton(R.string.default_buttons_ok, null); - builder.show(); +// Builder builder = new AccessibleAlertBuilder(this); +// builder.setMessage(R.string.osmand_net_previously_installed); +// builder.setPositiveButton(R.string.default_buttons_ok, null); +// builder.show(); } else { Builder builder = new AccessibleAlertBuilder(this); builder.setMessage(R.string.first_time_msg); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 3966a35c91..36e8db11d2 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -10,7 +10,6 @@ import java.util.Date; import java.util.Iterator; import java.util.List; -import android.view.WindowManager; import net.londatiga.android.ActionItem; import net.londatiga.android.QuickAction; import net.osmand.AndroidUtils; @@ -27,6 +26,7 @@ import net.osmand.plus.ApplicationMode; import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick; import net.osmand.plus.FavouritesDbHelper; +import net.osmand.plus.FavouritesDbHelper.FavoriteGroup; import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; @@ -59,6 +59,7 @@ import android.os.Build; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; +import android.view.WindowManager; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; @@ -120,8 +121,8 @@ public class MapActivityActions implements DialogProvider { if(name == null) { name = resources.getString(R.string.add_favorite_dialog_default_favourite_name); } - final FavouritePoint point = new FavouritePoint(lat, lon, name, - resources.getString(R.string.favorite_default_category)); + OsmandApplication app = (OsmandApplication) activity.getApplication(); + final FavouritePoint point = new FavouritePoint(lat, lon, name, app.getSettings().LAST_FAV_CATEGORY_ENTERED.get()); args.putSerializable(KEY_FAVORITE, point); final EditText editText = (EditText) dialog.findViewById(R.id.Name); editText.setText(point.getName()); @@ -140,7 +141,12 @@ public class MapActivityActions implements DialogProvider { builder.setView(v); final EditText editText = (EditText) v.findViewById(R.id.Name); final AutoCompleteTextView cat = (AutoCompleteTextView) v.findViewById(R.id.Category); - cat.setAdapter(new ArrayAdapter(activity, R.layout.list_textview, helper.getFavoriteGroups().keySet().toArray(new String[] {}))); + List gs = helper.getFavoriteGroups(); + String[] list = new String[gs.size()]; + for (int i = 0; i < list.length; i++) { + list[i] = gs.get(i).name; + } + cat.setAdapter(new ArrayAdapter(activity, R.layout.list_textview, list)); builder.setNegativeButton(R.string.default_buttons_cancel, null); builder.setNeutralButton(R.string.update_existing, new DialogInterface.OnClickListener(){ @@ -160,9 +166,12 @@ public class MapActivityActions implements DialogProvider { @Override public void onClick(DialogInterface dialog, int which) { FavouritePoint point = (FavouritePoint) args.getSerializable(KEY_FAVORITE); - final FavouritesDbHelper helper = ((OsmandApplication) activity.getApplication()).getFavorites(); + OsmandApplication app = (OsmandApplication) activity.getApplication(); + String categoryStr = cat.getText().toString().trim(); + final FavouritesDbHelper helper = app.getFavorites(); + app.getSettings().LAST_FAV_CATEGORY_ENTERED.set(categoryStr); point.setName(editText.getText().toString().trim()); - point.setCategory(cat.getText().toString().trim()); + point.setCategory(categoryStr); boolean added = helper.addFavourite(point); if (added) { AccessibleToast.makeText(activity, MessageFormat.format( diff --git a/OsmAnd/src/net/osmand/plus/sherpafy/TourViewActivity.java b/OsmAnd/src/net/osmand/plus/sherpafy/TourViewActivity.java index 8057c2439e..f5482e49c5 100644 --- a/OsmAnd/src/net/osmand/plus/sherpafy/TourViewActivity.java +++ b/OsmAnd/src/net/osmand/plus/sherpafy/TourViewActivity.java @@ -13,7 +13,6 @@ import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.DownloadIndexActivity; -import net.osmand.plus.activities.MapActivity; import net.osmand.plus.sherpafy.TourInformation.StageInformation; import android.app.Activity; import android.app.AlertDialog;