fix issue with favorites and issue 101

git-svn-id: https://osmand.googlecode.com/svn/trunk@578 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-10-19 20:18:23 +00:00
parent 3c837c5b7c
commit 430cc39841
9 changed files with 339 additions and 228 deletions

View file

@ -10,7 +10,6 @@ public class ToDoConstants {
// TODO max 100 // TODO max 100
// FOR 0.4 beta RELEASE // FOR 0.4 beta RELEASE
// Profile vector rendering // Profile vector rendering
// 100. Show impoted gpx points (as favorites), sort the by distance
// Outside base 0.4 release // Outside base 0.4 release
@ -42,6 +41,7 @@ public class ToDoConstants {
/////////////////////////// DONE ////////////////////////////// /////////////////////////// DONE //////////////////////////////
// DONE ANDROID : // DONE ANDROID :
// 99. Implement better file downloader for big files // 99. Implement better file downloader for big files
// 100. Show impoted gpx points (as favorites), sort the by distance
// DONE SWING // DONE SWING

View file

@ -0,0 +1,53 @@
package net.osmand;
public class FavouritePoint {
private String name;
private double latitude;
private double longitude;
private boolean stored = false;
public FavouritePoint(){
}
public FavouritePoint(double latitude, double longitude, String name) {
this.latitude = latitude;
this.longitude = longitude;
this.name = name;
}
public double getLatitude() {
return latitude;
}
public boolean isStored() {
return stored;
}
public void setStored(boolean stored) {
this.stored = stored;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Favourite " + getName(); //$NON-NLS-1$
}
}

View file

@ -0,0 +1,133 @@
package net.osmand;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class FavouritesDbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 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_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$
private List<FavouritePoint> favoritePointsFromGPXFile = null;
private Map<String, FavouritePoint> favoritePoints = null;
public FavouritesDbHelper(Context context) {
super(context, FAVOURITE_TABLE_NAME, null, DATABASE_VERSION);
}
public List<FavouritePoint> getFavoritePointsFromGPXFile() {
return favoritePointsFromGPXFile;
}
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();
}
}
}
public Collection<FavouritePoint> getFavouritePoints() {
checkFavoritePoints();
return favoritePoints.values();
}
public FavouritePoint getFavoritePointByName(String name){
checkFavoritePoints();
return favoritePoints.get(name);
}
public boolean editFavouriteName(FavouritePoint p, String newName) {
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());
p.setName(newName);
favoritePoints.put(newName, p);
return true;
}
return false;
}
public boolean editFavourite(FavouritePoint p, double lat, double lon) {
checkFavoritePoints();
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET latitude = ?, longitude = ? WHERE name = ?", new Object[] { lat, lon, p.getName() }); //$NON-NLS-1$ //$NON-NLS-2$
p.setLatitude(lat);
p.setLongitude(lon);
return true;
}
return false;
}
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());
if(fp != null){
fp.setStored(false);
}
return true;
}
return false;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(FAVOURITE_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

View file

@ -6,10 +6,13 @@ package net.osmand.activities;
import java.io.File; import java.io.File;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import net.osmand.FavouritePoint;
import net.osmand.FavouritesDbHelper;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
import net.osmand.OsmandSettings; import net.osmand.OsmandSettings;
import net.osmand.R; import net.osmand.R;
@ -21,13 +24,9 @@ import net.osmand.osm.MapUtils;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.ListActivity; import android.app.ListActivity;
import android.app.AlertDialog.Builder; import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.view.ContextMenu; import android.view.ContextMenu;
@ -60,9 +59,8 @@ public class FavouritesActivity extends ListActivity {
public static final String FILE_TO_SAVE = "favourites.gpx"; //$NON-NLS-1$ public static final String FILE_TO_SAVE = "favourites.gpx"; //$NON-NLS-1$
private List<FavouritePoint> favouritesList;
private FavouritesDbHelper helper;
private FavouritesAdapter favouritesAdapter; private FavouritesAdapter favouritesAdapter;
private FavouritesDbHelper helper;
@Override @Override
@ -72,11 +70,8 @@ public class FavouritesActivity extends ListActivity {
lv.setId(android.R.id.list); lv.setId(android.R.id.list);
setContentView(lv); setContentView(lv);
helper = new FavouritesDbHelper(this);
favouritesList = helper.getFavouritePoints();
favouritesAdapter = new FavouritesAdapter(favouritesList);
lv.setAdapter(favouritesAdapter);
/* Add Context-Menu listener to the ListView. */ /* Add Context-Menu listener to the ListView. */
lv.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener(){ lv.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener(){
@ -84,15 +79,48 @@ public class FavouritesActivity extends ListActivity {
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.setHeaderTitle(R.string.favourites_context_menu_title); menu.setHeaderTitle(R.string.favourites_context_menu_title);
menu.add(0, NAVIGATE_TO, 0, R.string.favourites_context_menu_navigate); menu.add(0, NAVIGATE_TO, 0, R.string.favourites_context_menu_navigate);
final FavouritePoint point = (FavouritePoint) favouritesAdapter.getItem(((AdapterContextMenuInfo)menuInfo).position);
if(point.isStored()){
menu.add(0, EDIT_ITEM, 1, R.string.favourites_context_menu_edit); menu.add(0, EDIT_ITEM, 1, R.string.favourites_context_menu_edit);
menu.add(0, DELETE_ITEM, 2, R.string.favourites_context_menu_delete); menu.add(0, DELETE_ITEM, 2, R.string.favourites_context_menu_delete);
} }
}
}); });
} }
@Override
protected void onResume() {
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.getLastKnownMapLocation(OsmandSettings.getPrefs(this));
if(mapLocation != null){
favouritesAdapter.sort(new Comparator<FavouritePoint>(){
@Override
public int compare(FavouritePoint object1, FavouritePoint object2) {
double d1 = MapUtils.getDistance(mapLocation, object1.getLatitude(), object1.getLongitude());
double d2 = MapUtils.getDistance(mapLocation, object2.getLatitude(), object2.getLongitude());
if(d1 == d2){
return 0;
} else if(d1 > d2){
return 1;
}
return -1;
}
});
}
getListView().setAdapter(favouritesAdapter);
}
public void onListItemClick(ListView parent, View v, int position, long id) { public void onListItemClick(ListView parent, View v, int position, long id) {
FavouritePoint point = favouritesList.get(position); FavouritePoint point = favouritesAdapter.getItem(position);
OsmandSettings.setShowingFavorites(this, true); OsmandSettings.setShowingFavorites(this, true);
OsmandSettings.setMapLocationToShow(this, point.getLatitude(), point.getLongitude(), getString(R.string.favorite)+" : " + point.getName()); //$NON-NLS-1$ OsmandSettings.setMapLocationToShow(this, point.getLatitude(), point.getLongitude(), getString(R.string.favorite)+" : " + point.getName()); //$NON-NLS-1$
Intent newIntent = new Intent(FavouritesActivity.this, MapActivity.class); Intent newIntent = new Intent(FavouritesActivity.this, MapActivity.class);
@ -102,7 +130,7 @@ public class FavouritesActivity extends ListActivity {
@Override @Override
public boolean onContextItemSelected(MenuItem aItem) { public boolean onContextItemSelected(MenuItem aItem) {
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) aItem.getMenuInfo(); AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) aItem.getMenuInfo();
final FavouritePoint point = (FavouritePoint) favouritesList.get(menuInfo.position); final FavouritePoint point = (FavouritePoint) favouritesAdapter.getItem(menuInfo.position);
if (aItem.getItemId() == NAVIGATE_TO) { if (aItem.getItemId() == NAVIGATE_TO) {
//OsmandSettings.setMapLocationToShow(this, point.getLatitude(), point.getLongitude(), getString(R.string.favorite)+" : " + point.getName()); //$NON-NLS-1$ //OsmandSettings.setMapLocationToShow(this, point.getLatitude(), point.getLongitude(), getString(R.string.favorite)+" : " + point.getName()); //$NON-NLS-1$
OsmandSettings.setPointToNavigate(this, point.getLatitude(), point.getLongitude()); OsmandSettings.setPointToNavigate(this, point.getLatitude(), point.getLongitude());
@ -141,8 +169,7 @@ public class FavouritesActivity extends ListActivity {
Toast.makeText(FavouritesActivity.this, Toast.makeText(FavouritesActivity.this,
MessageFormat.format(resources.getString(R.string.favourites_remove_dialog_success), point.getName()), MessageFormat.format(resources.getString(R.string.favourites_remove_dialog_success), point.getName()),
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
favouritesList.remove(point); favouritesAdapter.remove(point);
favouritesAdapter.notifyDataSetChanged();
} }
} }
@ -167,18 +194,19 @@ public class FavouritesActivity extends ListActivity {
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == EXPORT_ID){ if(item.getItemId() == EXPORT_ID){
File appDir = new File(Environment.getExternalStorageDirectory(), ResourceManager.APP_DIR); File appDir = new File(Environment.getExternalStorageDirectory(), ResourceManager.APP_DIR);
if(favouritesList == null || favouritesList.isEmpty()){ if(favouritesAdapter.isEmpty()){
Toast.makeText(this, R.string.no_fav_to_save, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.no_fav_to_save, Toast.LENGTH_LONG).show();
} else if(!appDir.exists()){ } else if(!appDir.exists()){
Toast.makeText(this, R.string.sd_dir_not_accessible, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.sd_dir_not_accessible, Toast.LENGTH_LONG).show();
} else { } else {
File f = new File(appDir, FILE_TO_SAVE); File f = new File(appDir, FILE_TO_SAVE);
List<WptPt> wpt = new ArrayList<WptPt>(); List<WptPt> wpt = new ArrayList<WptPt>();
for(FavouritePoint p : favouritesList){ for (int i = 0; i < favouritesAdapter.getCount(); i++) {
FavouritePoint p = favouritesAdapter.getItem(i);
WptPt pt = new WptPt(); WptPt pt = new WptPt();
pt.lat = p.latitude; pt.lat = p.getLatitude();
pt.lon = p.longitude; pt.lon = p.getLongitude();
pt.name = p.name; pt.name = p.getName();
wpt.add(pt); wpt.add(pt);
} }
if(GPXUtilities.saveToXMLFiles(f, wpt, this)){ if(GPXUtilities.saveToXMLFiles(f, wpt, this)){
@ -193,25 +221,23 @@ public class FavouritesActivity extends ListActivity {
Toast.makeText(this, MessageFormat.format(getString(R.string.fav_file_to_load_not_found), f.getAbsolutePath()), Toast.LENGTH_LONG).show(); Toast.makeText(this, MessageFormat.format(getString(R.string.fav_file_to_load_not_found), f.getAbsolutePath()), Toast.LENGTH_LONG).show();
} else { } else {
Set<String> existedPoints = new LinkedHashSet<String>(); Set<String> existedPoints = new LinkedHashSet<String>();
if(favouritesList != null){ if(!favouritesAdapter.isEmpty()){
for(FavouritePoint fp : favouritesList){ for (int i = 0; i < favouritesAdapter.getCount(); i++) {
existedPoints.add(fp.name); FavouritePoint fp = favouritesAdapter.getItem(i);
existedPoints.add(fp.getName());
} }
} }
GPXFileResult res = GPXUtilities.loadGPXFile(this, f); GPXFileResult res = GPXUtilities.loadGPXFile(this, f);
if (res.error == null) { if (res.error == null) {
for(WptPt p : res.wayPoints){ for(WptPt p : res.wayPoints){
if(!existedPoints.contains(p.name)){ if(!existedPoints.contains(p.name)){
FavouritePoint fp = new FavouritePoint(); FavouritePoint fp = new FavouritePoint(p.lat, p.lon, p.name);
fp.name = p.name; if(helper.addFavourite(fp)){
fp.latitude = p.lat; favouritesAdapter.add(fp);
fp.longitude = p.lon; }
helper.addFavourite(fp);
favouritesList.add(fp);
} }
} }
Toast.makeText(this, R.string.fav_imported_sucessfully, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.fav_imported_sucessfully, Toast.LENGTH_SHORT).show();
favouritesAdapter.notifyDataSetChanged();
} else { } else {
Toast.makeText(this, res.error, Toast.LENGTH_LONG).show(); Toast.makeText(this, res.error, Toast.LENGTH_LONG).show();
} }
@ -222,140 +248,13 @@ public class FavouritesActivity extends ListActivity {
return true; return true;
} }
public static class FavouritesDbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 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_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$
public FavouritesDbHelper(Context context) {
super(context, FAVOURITE_TABLE_NAME, null, DATABASE_VERSION);
}
public boolean addFavourite(FavouritePoint p){
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$
return true;
}
return false;
}
public List<FavouritePoint> getFavouritePoints(){
SQLiteDatabase db = getReadableDatabase();
ArrayList<FavouritePoint> list = new ArrayList<FavouritePoint>();
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);
if(query.moveToFirst()){
do {
FavouritePoint p = new FavouritePoint();
p.setName(query.getString(0));
p.setLatitude(query.getDouble(1));
p.setLongitude(query.getDouble(2));
list.add(p);
} while(query.moveToNext());
}
query.close();
}
return list;
}
public boolean editFavouriteName(FavouritePoint p, String newName){
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$
p.setName(newName);
return true;
}
return false;
}
public boolean editFavourite(FavouritePoint p, double lat, double lon){
SQLiteDatabase db = getWritableDatabase();
if(db != null){
db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET latitude = ?, longitude = ? WHERE name = ?",new Object[]{lat, lon, p.getName()}); //$NON-NLS-1$ //$NON-NLS-2$
p.setLatitude(lat);
p.setLongitude(lon);
return true;
}
return false;
}
public boolean deleteFavourite(FavouritePoint p){
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$
return true;
}
return false;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(FAVOURITE_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public static class FavouritePoint {
private String name;
private double latitude;
private double longitude;
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Favourite " + getName(); //$NON-NLS-1$
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(helper != null){
helper.close();
}
}
class FavouritesAdapter extends ArrayAdapter<FavouritePoint> { class FavouritesAdapter extends ArrayAdapter<FavouritePoint> {
FavouritesAdapter(List<FavouritePoint> list) { FavouritesAdapter(List<FavouritePoint> list) {
super(FavouritesActivity.this, R.layout.favourites_list_item, list); super(FavouritesActivity.this, R.layout.favourites_list_item, list);
this.setNotifyOnChange(false);
} }
@ -365,11 +264,17 @@ public class FavouritesActivity extends ListActivity {
LayoutInflater inflater = getLayoutInflater(); LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.favourites_list_item, parent, false); row = inflater.inflate(R.layout.favourites_list_item, parent, false);
} }
TextView label = (TextView) row.findViewById(R.id.favourite_label); TextView label = (TextView) row.findViewById(R.id.favourite_label);
TextView distanceLabel = (TextView) row.findViewById(R.id.favouritedistance_label); TextView distanceLabel = (TextView) row.findViewById(R.id.favouritedistance_label);
ImageView icon = (ImageView) row.findViewById(R.id.favourite_icon); ImageView icon = (ImageView) row.findViewById(R.id.favourite_icon);
FavouritePoint model = (FavouritePoint) getItem(position); FavouritePoint model = (FavouritePoint) getItem(position);
row.setTag(model);
if(model.isStored()){
icon.setImageResource(R.drawable.favorites);
} else {
icon.setImageResource(R.drawable.opened_poi); icon.setImageResource(R.drawable.opened_poi);
}
LatLon lastKnownMapLocation = OsmandSettings.getLastKnownMapLocation(OsmandSettings.getPrefs(FavouritesActivity.this)); LatLon lastKnownMapLocation = OsmandSettings.getLastKnownMapLocation(OsmandSettings.getPrefs(FavouritesActivity.this));
int dist = (int) (MapUtils.getDistance(model.getLatitude(), model.getLongitude(), int dist = (int) (MapUtils.getDistance(model.getLatitude(), model.getLongitude(),
lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude())); lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude()));

View file

@ -4,14 +4,18 @@ import java.io.File;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import net.osmand.Algoritms; import net.osmand.Algoritms;
import net.osmand.AmenityIndexRepository; import net.osmand.AmenityIndexRepository;
import net.osmand.BusyIndicator; import net.osmand.BusyIndicator;
import net.osmand.FavouritePoint;
import net.osmand.FavouritesDbHelper;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
import net.osmand.LogUtil; import net.osmand.LogUtil;
import net.osmand.OsmandSettings; import net.osmand.OsmandSettings;
@ -24,8 +28,6 @@ import net.osmand.Version;
import net.osmand.GPXUtilities.GPXFileResult; import net.osmand.GPXUtilities.GPXFileResult;
import net.osmand.GPXUtilities.WptPt; import net.osmand.GPXUtilities.WptPt;
import net.osmand.OsmandSettings.ApplicationMode; import net.osmand.OsmandSettings.ApplicationMode;
import net.osmand.activities.FavouritesActivity.FavouritePoint;
import net.osmand.activities.FavouritesActivity.FavouritesDbHelper;
import net.osmand.activities.search.SearchActivity; import net.osmand.activities.search.SearchActivity;
import net.osmand.activities.search.SearchPoiFilterActivity; import net.osmand.activities.search.SearchPoiFilterActivity;
import net.osmand.activities.search.SearchTransportActivity; import net.osmand.activities.search.SearchTransportActivity;
@ -778,7 +780,6 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
updateApplicationModeSettings(); updateApplicationModeSettings();
favoritesLayer.reloadFavorites(this);
poiMapLayer.setFilter(OsmandSettings.getPoiFilterForMap(this, (OsmandApplication) getApplication())); poiMapLayer.setFilter(OsmandSettings.getPoiFilterForMap(this, (OsmandApplication) getApplication()));
backToLocation.setVisibility(View.INVISIBLE); backToLocation.setVisibility(View.INVISIBLE);
@ -1295,7 +1296,7 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
} else if(item == 5){ } else if(item == 5){
if(gpxLayer.isVisible()){ if(gpxLayer.isVisible()){
gpxLayer.clearCurrentGPX(); gpxLayer.clearCurrentGPX();
favoritesLayer.setAdditionalPoints(null); favoritesLayer.getFavorites().setFavoritePointsFromGPXFile(null);
} else { } else {
dialog.dismiss(); dialog.dismiss();
useGPXFileLayer(false, null); useGPXFileLayer(false, null);
@ -1377,7 +1378,7 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
}); });
} else { } else {
OsmandSettings.setShowingFavorites(MapActivity.this, true); OsmandSettings.setShowingFavorites(MapActivity.this, true);
List<FavouritePoint> pts = new ArrayList<FavouritePoint>(); List<net.osmand.FavouritePoint> pts = new ArrayList<FavouritePoint>();
for(WptPt p : res.wayPoints){ for(WptPt p : res.wayPoints){
FavouritePoint pt = new FavouritePoint(); FavouritePoint pt = new FavouritePoint();
pt.setLatitude(p.lat); pt.setLatitude(p.lat);
@ -1385,7 +1386,7 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
pt.setName(p.name); pt.setName(p.name);
pts.add(pt); pts.add(pt);
} }
favoritesLayer.setAdditionalPoints(pts); favoritesLayer.getFavorites().setFavoritePointsFromGPXFile(pts);
gpxLayer.setTracks(res.locations); gpxLayer.setTracks(res.locations);
} }
mapView.refreshMap(); mapView.refreshMap();
@ -1582,10 +1583,7 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
protected void addFavouritePoint(final double latitude, final double longitude){ protected void addFavouritePoint(final double latitude, final double longitude){
final Resources resources = this.getResources(); final Resources resources = this.getResources();
final FavouritePoint p = new FavouritesActivity.FavouritePoint(); final FavouritePoint p = new FavouritePoint(latitude, longitude, resources.getString(R.string.add_favorite_dialog_default_favourite_name));
p.setLatitude(latitude);
p.setLongitude(longitude);
p.setName(resources.getString(R.string.add_favorite_dialog_default_favourite_name));
Builder builder = new AlertDialog.Builder(this); Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.add_favorite_dialog_top_text); builder.setTitle(R.string.add_favorite_dialog_top_text);
@ -1597,20 +1595,21 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
Builder b = new AlertDialog.Builder(MapActivity.this); Builder b = new AlertDialog.Builder(MapActivity.this);
final FavouritesDbHelper helper = new FavouritesActivity.FavouritesDbHelper(MapActivity.this); final FavouritesDbHelper helper = ((OsmandApplication)getApplication()).getFavorites();
final List<FavouritePoint> points = helper.getFavouritePoints(); final Collection<FavouritePoint> points = helper.getFavouritePoints();
final String[] ar = new String[points.size()]; final String[] ar = new String[points.size()];
for (int i = 0; i < ar.length; i++) { Iterator<FavouritePoint> it = points.iterator();
ar[i] = points.get(i).getName(); int i=0;
while(it.hasNext()){
ar[i++] = it.next().getName();
} }
b.setItems(ar, new DialogInterface.OnClickListener(){ b.setItems(ar, new DialogInterface.OnClickListener(){
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
if(helper.editFavourite(points.get(which), latitude, longitude)){ FavouritePoint fv = helper.getFavoritePointByName(ar[which]);
if(helper.editFavourite(fv, latitude, longitude)){
Toast.makeText(MapActivity.this, getString(R.string.fav_points_edited), Toast.LENGTH_SHORT).show(); Toast.makeText(MapActivity.this, getString(R.string.fav_points_edited), Toast.LENGTH_SHORT).show();
} }
helper.close();
favoritesLayer.reloadFavorites(MapActivity.this);
mapView.refreshMap(); mapView.refreshMap();
} }
}); });
@ -1626,15 +1625,13 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
builder.setPositiveButton(R.string.default_buttons_add, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.default_buttons_add, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
FavouritesDbHelper helper = new FavouritesActivity.FavouritesDbHelper(MapActivity.this); final FavouritesDbHelper helper = ((OsmandApplication)getApplication()).getFavorites();
p.setName(editText.getText().toString()); p.setName(editText.getText().toString());
boolean added = helper.addFavourite(p); boolean added = helper.addFavourite(p);
if (added) { if (added) {
Toast.makeText(MapActivity.this, MessageFormat.format(getString(R.string.add_favorite_dialog_favourite_added_template), p.getName()), Toast.LENGTH_SHORT) Toast.makeText(MapActivity.this, MessageFormat.format(getString(R.string.add_favorite_dialog_favourite_added_template), p.getName()), Toast.LENGTH_SHORT)
.show(); .show();
} }
helper.close();
favoritesLayer.reloadFavorites(MapActivity.this);
mapView.refreshMap(); mapView.refreshMap();
} }
}); });

View file

@ -8,6 +8,7 @@ import java.io.PrintStream;
import java.lang.Thread.UncaughtExceptionHandler; import java.lang.Thread.UncaughtExceptionHandler;
import java.util.List; import java.util.List;
import net.osmand.FavouritesDbHelper;
import net.osmand.LogUtil; import net.osmand.LogUtil;
import net.osmand.OsmandSettings; import net.osmand.OsmandSettings;
import net.osmand.PoiFiltersHelper; import net.osmand.PoiFiltersHelper;
@ -30,6 +31,7 @@ public class OsmandApplication extends Application {
ResourceManager manager = null; ResourceManager manager = null;
PoiFiltersHelper poiFilters = null; PoiFiltersHelper poiFilters = null;
RoutingHelper routingHelper = null; RoutingHelper routingHelper = null;
FavouritesDbHelper favorites = null;
CommandPlayer player; CommandPlayer player;
@ -57,6 +59,13 @@ public class OsmandApplication extends Application {
return poiFilters; return poiFilters;
} }
public FavouritesDbHelper getFavorites() {
if(favorites == null) {
favorites = new FavouritesDbHelper(this);
}
return favorites;
}
public ResourceManager getResourceManager() { public ResourceManager getResourceManager() {
return manager; return manager;
} }

View file

@ -548,7 +548,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
} else if(preset == ApplicationMode.BICYCLE){ } else if(preset == ApplicationMode.BICYCLE){
// edit.putBoolean(OsmandSettings.USE_INTERNET_TO_DOWNLOAD_TILES, _); // edit.putBoolean(OsmandSettings.USE_INTERNET_TO_DOWNLOAD_TILES, _);
// edit.putBoolean(OsmandSettings.USE_INTERNET_TO_CALCULATE_ROUTE, _); // edit.putBoolean(OsmandSettings.USE_INTERNET_TO_CALCULATE_ROUTE, _);
edit.putBoolean(OsmandSettings.SHOW_POI_OVER_MAP, true); // edit.putBoolean(OsmandSettings.SHOW_POI_OVER_MAP, true);
edit.putInt(OsmandSettings.ROTATE_MAP, OsmandSettings.ROTATE_MAP_BEARING); edit.putInt(OsmandSettings.ROTATE_MAP, OsmandSettings.ROTATE_MAP_BEARING);
edit.putBoolean(OsmandSettings.SHOW_VIEW_ANGLE, true); edit.putBoolean(OsmandSettings.SHOW_VIEW_ANGLE, true);
edit.putBoolean(OsmandSettings.AUTO_ZOOM_MAP, false); edit.putBoolean(OsmandSettings.AUTO_ZOOM_MAP, false);
@ -561,7 +561,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
} else if(preset == ApplicationMode.PEDESTRIAN){ } else if(preset == ApplicationMode.PEDESTRIAN){
// edit.putBoolean(OsmandSettings.USE_INTERNET_TO_DOWNLOAD_TILES, _); // edit.putBoolean(OsmandSettings.USE_INTERNET_TO_DOWNLOAD_TILES, _);
edit.putBoolean(OsmandSettings.SHOW_POI_OVER_MAP, true); // edit.putBoolean(OsmandSettings.SHOW_POI_OVER_MAP, true);
edit.putInt(OsmandSettings.ROTATE_MAP, OsmandSettings.ROTATE_MAP_COMPASS); edit.putInt(OsmandSettings.ROTATE_MAP, OsmandSettings.ROTATE_MAP_COMPASS);
edit.putBoolean(OsmandSettings.SHOW_VIEW_ANGLE, true); edit.putBoolean(OsmandSettings.SHOW_VIEW_ANGLE, true);
edit.putBoolean(OsmandSettings.AUTO_ZOOM_MAP, false); edit.putBoolean(OsmandSettings.AUTO_ZOOM_MAP, false);
@ -576,13 +576,13 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
} else if(preset == ApplicationMode.DEFAULT){ } else if(preset == ApplicationMode.DEFAULT){
// edit.putBoolean(OsmandSettings.USE_INTERNET_TO_DOWNLOAD_TILES, _); // edit.putBoolean(OsmandSettings.USE_INTERNET_TO_DOWNLOAD_TILES, _);
edit.putBoolean(OsmandSettings.SHOW_POI_OVER_MAP, true); // edit.putBoolean(OsmandSettings.SHOW_POI_OVER_MAP, true);
edit.putInt(OsmandSettings.ROTATE_MAP, OsmandSettings.ROTATE_MAP_NONE); edit.putInt(OsmandSettings.ROTATE_MAP, OsmandSettings.ROTATE_MAP_NONE);
edit.putBoolean(OsmandSettings.SHOW_VIEW_ANGLE, false); edit.putBoolean(OsmandSettings.SHOW_VIEW_ANGLE, false);
edit.putBoolean(OsmandSettings.AUTO_ZOOM_MAP, false); edit.putBoolean(OsmandSettings.AUTO_ZOOM_MAP, false);
// edit.putBoolean(OsmandSettings.SHOW_OSM_BUGS, _); // edit.putBoolean(OsmandSettings.SHOW_OSM_BUGS, _);
// edit.putBoolean(OsmandSettings.USE_ENGLISH_NAMES, _); // edit.putBoolean(OsmandSettings.USE_ENGLISH_NAMES, _);
// edit.putBoolean(OsmandSettings.SAVE_TRACK_TO_GPX, _); edit.putBoolean(OsmandSettings.SAVE_TRACK_TO_GPX, false);
// edit.putInt(OsmandSettings.SAVE_TRACK_INTERVAL, _); // edit.putInt(OsmandSettings.SAVE_TRACK_INTERVAL, _);
edit.putInt(OsmandSettings.POSITION_ON_MAP, OsmandSettings.CENTER_CONSTANT); edit.putInt(OsmandSettings.POSITION_ON_MAP, OsmandSettings.CENTER_CONSTANT);
// edit.putString(OsmandSettings.MAP_TILE_SOURCES, _); // edit.putString(OsmandSettings.MAP_TILE_SOURCES, _);

View file

@ -20,7 +20,9 @@ import net.osmand.osm.LatLon;
import net.osmand.osm.MapUtils; import net.osmand.osm.MapUtils;
import net.osmand.osm.OpeningHoursParser; import net.osmand.osm.OpeningHoursParser;
import net.osmand.osm.OpeningHoursParser.OpeningHoursRule; import net.osmand.osm.OpeningHoursParser.OpeningHoursRule;
import android.app.AlertDialog;
import android.app.ListActivity; import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
@ -212,12 +214,39 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override @Override
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) { public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
Amenity amenity = ((AmenityAdapter) getListAdapter()).getItem(pos); final Amenity amenity = ((AmenityAdapter) getListAdapter()).getItem(pos);
String format = amenity.getSimpleFormat(OsmandSettings.usingEnglishNames(settings)); String format = amenity.getSimpleFormat(OsmandSettings.usingEnglishNames(settings));
if (amenity.getOpeningHours() != null) { if (amenity.getOpeningHours() != null) {
format += "\n"+getString(R.string.opening_hours) + " : " + amenity.getOpeningHours(); //$NON-NLS-1$ //$NON-NLS-2$ format += " "+getString(R.string.opening_hours) + " : " + amenity.getOpeningHours(); //$NON-NLS-1$ //$NON-NLS-2$
} }
Toast.makeText(v.getContext(), format, Toast.LENGTH_LONG).show(); // Toast.makeText(v.getContext(), format, Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(SearchPOIActivity.this);
builder.setTitle(format);
builder.setItems(new String[]{getString(R.string.show_poi_on_map), getString(R.string.navigate_to)}, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == 0){
int z = OsmandSettings.getLastKnownMapZoom(settings);
OsmandSettings.setMapLocationToShow(SearchPOIActivity.this,
amenity.getLocation().getLatitude(), amenity.getLocation().getLongitude(),
Math.max(16, z), getString(R.string.poi)+" : " + amenity.getSimpleFormat(OsmandSettings.usingEnglishNames(settings))); //$NON-NLS-1$
} else if(which == 1){
LatLon l = amenity.getLocation();
OsmandSettings.setPointToNavigate(SearchPOIActivity.this, l.getLatitude(), l.getLongitude());
}
if(filter != null){
OsmandSettings.setPoiFilterForMap(SearchPOIActivity.this, filter.getFilterId());
OsmandSettings.setShowPoiOverMap(SearchPOIActivity.this, true);
}
Intent newIntent = new Intent(SearchPOIActivity.this, MapActivity.class);
startActivity(newIntent);
}
});
builder.show();
return true; return true;
} }
}); });

View file

@ -2,10 +2,9 @@ package net.osmand.views;
import java.util.List; import java.util.List;
import net.osmand.FavouritePoint;
import net.osmand.FavouritesDbHelper;
import net.osmand.R; import net.osmand.R;
import net.osmand.activities.FavouritesActivity;
import net.osmand.activities.FavouritesActivity.FavouritePoint;
import net.osmand.activities.FavouritesActivity.FavouritesDbHelper;
import net.osmand.osm.LatLon; import net.osmand.osm.LatLon;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
@ -27,14 +26,13 @@ public class FavoritesLayer implements OsmandMapLayer, ContextMenuLayer.IContext
private static final int radius = 15; private static final int radius = 15;
private OsmandMapTileView view; private OsmandMapTileView view;
private List<FavouritePoint> favouritePoints;
private List<FavouritePoint> additionalPoints;
private Path path; private Path path;
private Path pathDst; private Path pathDst;
private Paint paint; private Paint paint;
private Matrix matrix; private Matrix matrix;
private Paint paintBlack; private Paint paintBlack;
private DisplayMetrics dm; private DisplayMetrics dm;
private FavouritesDbHelper favorites;
public FavoritesLayer(){ public FavoritesLayer(){
@ -72,7 +70,8 @@ public class FavoritesLayer implements OsmandMapLayer, ContextMenuLayer.IContext
paintBlack.setAntiAlias(true); paintBlack.setAntiAlias(true);
paintBlack.setStrokeWidth(2); paintBlack.setStrokeWidth(2);
reloadFavorites(view.getContext()); favorites = view.getApplication().getFavorites();
} }
@Override @Override
@ -80,19 +79,6 @@ public class FavoritesLayer implements OsmandMapLayer, ContextMenuLayer.IContext
} }
public void setAdditionalPoints(List<FavouritePoint> additionalPoints) {
this.additionalPoints = additionalPoints;
}
public List<FavouritePoint> getAdditionalPoints() {
return additionalPoints;
}
public void reloadFavorites(Context ctx){
FavouritesDbHelper helper = new FavouritesActivity.FavouritesDbHelper(ctx);
favouritePoints = helper.getFavouritePoints();
helper.close();
}
@Override @Override
public boolean drawInScreenPixels() { public boolean drawInScreenPixels() {
@ -106,7 +92,7 @@ public class FavoritesLayer implements OsmandMapLayer, ContextMenuLayer.IContext
// request to load // request to load
for (FavouritePoint o : favouritePoints) { for (FavouritePoint o : favorites.getFavouritePoints()) {
if (o.getLatitude() >= latLonBounds.bottom && o.getLatitude() <= latLonBounds.top && o.getLongitude() >= latLonBounds.left if (o.getLatitude() >= latLonBounds.bottom && o.getLatitude() <= latLonBounds.top && o.getLongitude() >= latLonBounds.left
&& o.getLongitude() <= latLonBounds.right ) { && o.getLongitude() <= latLonBounds.right ) {
int x = view.getMapXForPoint(o.getLongitude()); int x = view.getMapXForPoint(o.getLongitude());
@ -117,8 +103,8 @@ public class FavoritesLayer implements OsmandMapLayer, ContextMenuLayer.IContext
canvas.drawPath(pathDst, paintBlack); canvas.drawPath(pathDst, paintBlack);
} }
} }
if(additionalPoints != null){ if(favorites.getFavoritePointsFromGPXFile() != null){
for (FavouritePoint o : additionalPoints) { for (FavouritePoint o : favorites.getFavoritePointsFromGPXFile()) {
if (o.getLatitude() >= latLonBounds.bottom && o.getLatitude() <= latLonBounds.top && o.getLongitude() >= latLonBounds.left if (o.getLatitude() >= latLonBounds.bottom && o.getLatitude() <= latLonBounds.top && o.getLongitude() >= latLonBounds.left
&& o.getLongitude() <= latLonBounds.right ) { && o.getLongitude() <= latLonBounds.right ) {
int x = view.getMapXForPoint(o.getLongitude()); int x = view.getMapXForPoint(o.getLongitude());
@ -142,11 +128,9 @@ public class FavoritesLayer implements OsmandMapLayer, ContextMenuLayer.IContext
public FavouritePoint getFavoriteFromPoint(PointF point) { public FavouritePoint getFavoriteFromPoint(PointF point) {
FavouritePoint result = null; FavouritePoint result = null;
float r = radius * dm.density; float r = radius * dm.density;
if (favouritePoints != null) {
int ex = (int) point.x; int ex = (int) point.x;
int ey = (int) point.y; int ey = (int) point.y;
for (int i = 0; i < favouritePoints.size(); i++) { for (FavouritePoint n : favorites.getFavouritePoints()) {
FavouritePoint n = favouritePoints.get(i);
int x = view.getRotatedMapXForPoint(n.getLatitude(), n.getLongitude()); int x = view.getRotatedMapXForPoint(n.getLatitude(), n.getLongitude());
int y = view.getRotatedMapYForPoint(n.getLatitude(), n.getLongitude()); int y = view.getRotatedMapYForPoint(n.getLatitude(), n.getLongitude());
if (Math.abs(x - ex) <= r && Math.abs(y - ey) <= r) { if (Math.abs(x - ex) <= r && Math.abs(y - ey) <= r) {
@ -154,12 +138,9 @@ public class FavoritesLayer implements OsmandMapLayer, ContextMenuLayer.IContext
result = n; result = n;
} }
} }
} if (favorites.getFavoritePointsFromGPXFile() != null) {
if (additionalPoints != null) { for (int i = 0; i < favorites.getFavoritePointsFromGPXFile().size(); i++) {
int ex = (int) point.x; FavouritePoint n = favorites.getFavoritePointsFromGPXFile().get(i);
int ey = (int) point.y;
for (int i = 0; i < additionalPoints.size(); i++) {
FavouritePoint n = additionalPoints.get(i);
int x = view.getRotatedMapXForPoint(n.getLatitude(), n.getLongitude()); int x = view.getRotatedMapXForPoint(n.getLatitude(), n.getLongitude());
int y = view.getRotatedMapYForPoint(n.getLatitude(), n.getLongitude()); int y = view.getRotatedMapYForPoint(n.getLatitude(), n.getLongitude());
if (Math.abs(x - ex) <= r && Math.abs(y - ey) <= r) { if (Math.abs(x - ex) <= r && Math.abs(y - ey) <= r) {
@ -188,6 +169,10 @@ public class FavoritesLayer implements OsmandMapLayer, ContextMenuLayer.IContext
return null; return null;
} }
public FavouritesDbHelper getFavorites() {
return favorites;
}
@Override @Override
public String getObjectDescription(Object o) { public String getObjectDescription(Object o) {