Allow spread favorites into groups

This commit is contained in:
Victor Shcherb 2011-08-21 16:34:45 +02:00
parent c370b80a2c
commit 3ac8381c2c
6 changed files with 156 additions and 90 deletions

View file

@ -1,5 +1,10 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<string name="favorite_home_category">Home</string>
<string name="favorite_friends_category">Friends</string>
<string name="favorite_places_category">Places</string>
<string name="favorite_default_category">Others</string>
<string name="vector_map_not_needed">Not needed</string>
<string name="basemap_missing">Base world map is missing. Please consider to download it in order to have proper environment.</string>
<string name="vector_data_missing">On board data is missing on SD card. Please consider to download it in order to use maps offline.</string>

View file

@ -2,6 +2,7 @@ package net.osmand;
public class FavouritePoint {
private String name;
private String category = "";
private double latitude;
private double longitude;
private boolean stored = false;
@ -38,6 +39,14 @@ public class FavouritePoint {
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getName() {
return name;

View file

@ -1,6 +1,6 @@
package net.osmand.plus;
import java.util.Collection;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -13,21 +13,44 @@ import android.database.sqlite.SQLiteOpenHelper;
public class FavouritesDbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
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$
private static final String FAVOURITE_COL_CATEGORY = "category"; //$NON-NLS-1$
private static final String FAVOURITE_COL_LAT = "latitude"; //$NON-NLS-1$
private static final String FAVOURITE_COL_LON = "longitude"; //$NON-NLS-1$
private static final String FAVOURITE_TABLE_CREATE = "CREATE TABLE " + FAVOURITE_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$
FAVOURITE_COL_NAME + " TEXT, " + FAVOURITE_COL_LAT + " double, " + //$NON-NLS-1$ //$NON-NLS-2$
FAVOURITE_COL_LON + " double);"; //$NON-NLS-1$
// externalize ?
private static final String GPX_GROUP = "Gpx";
private List<FavouritePoint> favoritePointsFromGPXFile = null;
private Map<String, FavouritePoint> favoritePoints = null;
private List<FavouritePoint> cachedFavoritePoints = new ArrayList<FavouritePoint>();
private Map<String, List<FavouritePoint>> favoriteGroups = null;
private final Context context;
public FavouritesDbHelper(Context context) {
super(context, FAVOURITE_DB_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(FAVOURITE_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion == 1){
db.execSQL("ALTER TABLE " + FAVOURITE_TABLE_NAME + " ADD COLUMN (" + FAVOURITE_COL_CATEGORY + " text)");
db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET category = ?", new Object[] { context.getString(R.string.favorite_default_category)}); //$NON-NLS-1$ //$NON-NLS-2$
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);
}
}
public List<FavouritePoint> getFavoritePointsFromGPXFile() {
@ -36,63 +59,37 @@ public class FavouritesDbHelper extends SQLiteOpenHelper {
public void setFavoritePointsFromGPXFile(List<FavouritePoint> favoritePointsFromGPXFile) {
this.favoritePointsFromGPXFile = favoritePointsFromGPXFile;
}
public boolean addFavourite(FavouritePoint p) {
checkFavoritePoints();
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
// delete with same name before
deleteFavourite(p);
db.execSQL("INSERT INTO " + FAVOURITE_TABLE_NAME + " VALUES (?, ?, ?)", new Object[] { p.getName(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$
favoritePoints.put(p.getName(), p);
p.setStored(true);
return true;
}
return false;
}
private void checkFavoritePoints(){
if(favoritePoints == null){
favoritePoints = new LinkedHashMap<String, FavouritePoint>();
SQLiteDatabase db = getReadableDatabase();
if (db != null) {
Cursor query = db.rawQuery("SELECT " + FAVOURITE_COL_NAME + ", " + FAVOURITE_COL_LAT + "," + FAVOURITE_COL_LON + " FROM " + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
FAVOURITE_TABLE_NAME, null);
favoritePoints.clear();
if (query.moveToFirst()) {
do {
FavouritePoint p = new FavouritePoint();
p.setName(query.getString(0));
p.setStored(true);
p.setLatitude(query.getDouble(1));
p.setLongitude(query.getDouble(2));
favoritePoints.put(p.getName(), p);
} while (query.moveToNext());
}
query.close();
if(favoritePointsFromGPXFile == null){
favoriteGroups.remove(GPX_GROUP);
} else {
checkFavoritePoints();
for(FavouritePoint t : favoritePointsFromGPXFile){
t.setCategory(GPX_GROUP);
t.setStored(false);
}
favoriteGroups.put(GPX_GROUP, favoritePointsFromGPXFile);
}
}
public Collection<FavouritePoint> getFavouritePoints() {
checkFavoritePoints();
return favoritePoints.values();
recalculateCachedFavPoints();
}
public FavouritePoint getFavoritePointByName(String name){
public List<FavouritePoint> getFavouritePoints() {
checkFavoritePoints();
return favoritePoints.get(name);
return cachedFavoritePoints;
}
public boolean editFavouriteName(FavouritePoint p, String newName) {
public boolean editFavouriteName(FavouritePoint p, String newName, String category) {
checkFavoritePoints();
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET name = ? WHERE name = ?", new Object[] { newName, p.getName() }); //$NON-NLS-1$ //$NON-NLS-2$
favoritePoints.remove(p.getName());
String oldCategory = p.getCategory();
db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET name = ?, category = ? WHERE name = ?", new Object[] { newName, category, p.getName() }); //$NON-NLS-1$ //$NON-NLS-2$
p.setName(newName);
favoritePoints.put(newName, p);
p.setCategory(category);
if(!oldCategory.equals(category)){
favoriteGroups.get(oldCategory).remove(p);
favoriteGroups.get(category).add(p);
}
return true;
}
return false;
@ -109,14 +106,27 @@ public class FavouritesDbHelper extends SQLiteOpenHelper {
}
return false;
}
private FavouritePoint findFavoriteByName(String name, String category){
if (favoriteGroups.containsKey(category)) {
for (FavouritePoint fv : favoriteGroups.get(category)) {
if (name.equals(fv.getName())) {
return fv;
}
}
}
return null;
}
public boolean deleteFavourite(FavouritePoint p) {
checkFavoritePoints();
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
db.execSQL("DELETE FROM " + FAVOURITE_TABLE_NAME + " WHERE name = ?", new Object[] { p.getName() }); //$NON-NLS-1$ //$NON-NLS-2$
FavouritePoint fp = favoritePoints.remove(p.getName());
db.execSQL("DELETE FROM " + FAVOURITE_TABLE_NAME + " WHERE name = ? AND category = ? ", new Object[] { p.getName(), p.getCategory()}); //$NON-NLS-1$ //$NON-NLS-2$
FavouritePoint fp = findFavoriteByName(p.getName(), p.getCategory());
if(fp != null){
favoriteGroups.get(p.getCategory()).remove(fp);
cachedFavoritePoints.remove(fp);
fp.setStored(false);
}
return true;
@ -124,12 +134,74 @@ public class FavouritesDbHelper extends SQLiteOpenHelper {
return false;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(FAVOURITE_TABLE_CREATE);
public boolean addFavourite(FavouritePoint p) {
checkFavoritePoints();
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
// delete with same name before
deleteFavourite(p);
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<FavouritePoint>());
}
favoriteGroups.get(p.getCategory()).add(p);
cachedFavoritePoints.add(p);
p.setStored(true);
return true;
}
return false;
}
private void addCategoryQuery(String category, SQLiteDatabase 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<FavouritePoint> temp = new ArrayList<FavouritePoint>();
for(List<FavouritePoint> f : favoriteGroups.values()){
temp.addAll(f);
}
cachedFavoritePoints = temp;
}
private void checkFavoritePoints(){
if(favoriteGroups == null){
favoriteGroups = new LinkedHashMap<String, List<FavouritePoint>>();
SQLiteDatabase db = getReadableDatabase();
if (db != null) {
Cursor 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<FavouritePoint>());
}
if (!name.equals("")) {
FavouritePoint p = new FavouritePoint();
p.setName(name);
p.setCategory(cat);
p.setStored(true);
p.setLatitude(query.getDouble(2));
p.setLongitude(query.getDouble(3));
favoriteGroups.get(cat).add(p);
}
} while (query.moveToNext());
}
query.close();
}
recalculateCachedFavPoints();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

View file

@ -96,9 +96,6 @@ public class FavouritesActivity extends ListActivity {
super.onResume();
helper = ((OsmandApplication)getApplication()).getFavorites();
ArrayList<FavouritePoint> list = new ArrayList<FavouritePoint>(helper.getFavouritePoints());
if(helper.getFavoritePointsFromGPXFile() != null){
list.addAll(helper.getFavoritePointsFromGPXFile());
}
favouritesAdapter = new FavouritesAdapter(list);
final LatLon mapLocation = OsmandSettings.getOsmandSettings(this).getLastKnownMapLocation();
if(mapLocation != null){
@ -149,7 +146,7 @@ public class FavouritesActivity extends ListActivity {
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());
boolean editied = helper.editFavouriteName(point, editText.getText().toString(), point.getCategory());
if (editied) {
favouritesAdapter.notifyDataSetChanged();
}

View file

@ -59,23 +59,30 @@ public class MapActivityActions {
Builder b = new AlertDialog.Builder(mapActivity);
final FavouritesDbHelper helper = ((OsmandApplication)mapActivity.getApplication()).getFavorites();
final Collection<FavouritePoint> points = helper.getFavouritePoints();
final String[] ar = new String[points.size()];
final String[] names = new String[points.size()];
final FavouritePoint[] favs = new FavouritePoint[points.size()];
Iterator<FavouritePoint> it = points.iterator();
int i=0;
while(it.hasNext()){
ar[i++] = it.next().getName();
FavouritePoint fp = it.next();
// filter gpx points
if(fp.isStored()){
favs[i] = fp;
names[i] = fp.getName();
i++;
}
}
b.setItems(ar, new DialogInterface.OnClickListener(){
b.setItems(names, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
FavouritePoint fv = helper.getFavoritePointByName(ar[which]);
FavouritePoint fv = favs[which];
if(helper.editFavourite(fv, latitude, longitude)){
Toast.makeText(mapActivity, getString(R.string.fav_points_edited), Toast.LENGTH_SHORT).show();
}
mapActivity.getMapView().refreshMap();
}
});
if(ar.length == 0){
if(names.length == 0){
Toast.makeText(mapActivity, getString(R.string.fav_points_not_exist), Toast.LENGTH_SHORT).show();
helper.close();
} else {

View file

@ -101,19 +101,6 @@ public class FavoritesLayer implements OsmandMapLayer, ContextMenuLayer.IContext
canvas.drawPath(pathDst, paintBlack);
}
}
if(favorites.getFavoritePointsFromGPXFile() != null){
for (FavouritePoint o : favorites.getFavoritePointsFromGPXFile()) {
if (o.getLatitude() >= latLonBounds.bottom && o.getLatitude() <= latLonBounds.top && o.getLongitude() >= latLonBounds.left
&& o.getLongitude() <= latLonBounds.right ) {
int x = view.getMapXForPoint(o.getLongitude());
int y = view.getMapYForPoint(o.getLatitude());
matrix.setTranslate(x, y);
path.transform(matrix, pathDst);
canvas.drawPath(pathDst, paint);
canvas.drawPath(pathDst, paintBlack);
}
}
}
}
}
@ -136,17 +123,6 @@ public class FavoritesLayer implements OsmandMapLayer, ContextMenuLayer.IContext
result = n;
}
}
if (favorites.getFavoritePointsFromGPXFile() != null) {
for (int i = 0; i < favorites.getFavoritePointsFromGPXFile().size(); i++) {
FavouritePoint n = favorites.getFavoritePointsFromGPXFile().get(i);
int x = view.getRotatedMapXForPoint(n.getLatitude(), n.getLongitude());
int y = view.getRotatedMapYForPoint(n.getLatitude(), n.getLongitude());
if (Math.abs(x - ex) <= r && Math.abs(y - ey) <= r) {
r = Math.max(Math.abs(x - ex), Math.abs(y - ey));
result = n;
}
}
}
return result;
}