diff --git a/DataExtractionOSM/src/com/osmand/ToDoConstants.java b/DataExtractionOSM/src/com/osmand/ToDoConstants.java index c77b4fc3c2..016afb6ee5 100644 --- a/DataExtractionOSM/src/com/osmand/ToDoConstants.java +++ b/DataExtractionOSM/src/com/osmand/ToDoConstants.java @@ -10,14 +10,8 @@ public class ToDoConstants { * Write activity to show something about authors / donation .... */ public int DESCRIBE_ABOUT_AUTHORS = 8; - - // TODO ANDROID in release 0.1 -// 13. Save point as favorite & introduce favorite points dialog -// 3. Revise osmand UI. Preparing new icons (revise UI 18, 2, ). Main application icon, back to location icon. -// 14. Show zoom level on map - - // NOT in release 0.1 + // TODO ANDROID NOT in release 0.1 // 8. Enable change POI directly on map (requires OSM login) // 16. Support open street bugs api. // 20. Implement save track/route to gpx (?) @@ -36,7 +30,10 @@ public class ToDoConstants { // DONE ANDROID : +// 13. Save point as favorite & introduce favorite points dialog // 29. Show opened/closed amenities (in search poi). +// 3. Revise osmand UI. Preparing new icons (revise UI 18, 2, ). Main application icon, back to location icon. +// 14. Show zoom level on map // DONE SWING } diff --git a/OsmAnd/AndroidManifest.xml b/OsmAnd/AndroidManifest.xml index de1f974057..4e4a9da4a3 100644 --- a/OsmAnd/AndroidManifest.xml +++ b/OsmAnd/AndroidManifest.xml @@ -12,11 +12,12 @@ - - - + + + + diff --git a/OsmAnd/res/layout/favourites.xml b/OsmAnd/res/layout/favourites.xml new file mode 100644 index 0000000000..a58a456e81 --- /dev/null +++ b/OsmAnd/res/layout/favourites.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/favourites_list_item.xml b/OsmAnd/res/layout/favourites_list_item.xml new file mode 100644 index 0000000000..a1befa3489 --- /dev/null +++ b/OsmAnd/res/layout/favourites_list_item.xml @@ -0,0 +1,16 @@ + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/searchpoi_list.xml b/OsmAnd/res/layout/searchpoi_list.xml index 0d6f4d3504..217026077f 100644 --- a/OsmAnd/res/layout/searchpoi_list.xml +++ b/OsmAnd/res/layout/searchpoi_list.xml @@ -8,7 +8,7 @@ android:paddingTop="2px" android:layout_height="fill_parent" /> diff --git a/OsmAnd/res/menu/map_menu.xml b/OsmAnd/res/menu/map_menu.xml index d359e392a5..970dd94554 100644 --- a/OsmAnd/res/menu/map_menu.xml +++ b/OsmAnd/res/menu/map_menu.xml @@ -2,8 +2,10 @@ - + + + diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 2f85f0bdc9..7daec9c3af 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -1,5 +1,6 @@ + Add to Favourites Select use native or english names Use english names Application settings diff --git a/OsmAnd/src/com/osmand/activities/FavouritesActivity.java b/OsmAnd/src/com/osmand/activities/FavouritesActivity.java new file mode 100644 index 0000000000..24269342c1 --- /dev/null +++ b/OsmAnd/src/com/osmand/activities/FavouritesActivity.java @@ -0,0 +1,277 @@ +/** + * + */ +package com.osmand.activities; + +import java.util.ArrayList; +import java.util.List; + +import android.app.AlertDialog; +import android.app.ListActivity; +import android.app.AlertDialog.Builder; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.SharedPreferences; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.os.Bundle; +import android.view.ContextMenu; +import android.view.LayoutInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.view.ContextMenu.ContextMenuInfo; +import android.widget.ArrayAdapter; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.TextView; +import android.widget.Toast; +import android.widget.AdapterView.AdapterContextMenuInfo; + +import com.osmand.OsmandSettings; +import com.osmand.R; +import com.osmand.osm.LatLon; +import com.osmand.osm.MapUtils; + +/** + * @author Maxim Frolov + * + */ +public class FavouritesActivity extends ListActivity { + + public static final int DELETE_ITEM = 0; + public static final int EDIT_ITEM = 1; + + private List favouritesList; + private FavouritesDbHelper helper; + private FavouritesAdapter favouritesAdapter; + + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + setContentView(R.layout.favourites); + + helper = new FavouritesDbHelper(this); + favouritesList = helper.getFavouritePoints(); + + ListView lv = getListView(); + favouritesAdapter = new FavouritesAdapter(favouritesList); + lv.setAdapter(favouritesAdapter); + /* Add Context-Menu listener to the ListView. */ + lv.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener(){ + + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { + menu.setHeaderTitle("Context menu"); + menu.add(0, EDIT_ITEM, 0, "Edit favourite"); + menu.add(0, DELETE_ITEM, 1, "Delete favourite"); + } + + }); + } + + public void onListItemClick(ListView parent, View v, int position, long id) { + SharedPreferences prefs = getSharedPreferences(OsmandSettings.SHARED_PREFERENCES_NAME, MODE_WORLD_READABLE); + if (prefs != null) { + FavouritePoint point = favouritesList.get(position); + OsmandSettings.setLastKnownMapLocation(this, point.getLatitude(), point.getLongitude()); + Intent newIntent = new Intent(FavouritesActivity.this, MapActivity.class); + startActivity(newIntent); + } + } + + @Override + public boolean onContextItemSelected(MenuItem aItem) { + AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) aItem.getMenuInfo(); + final FavouritePoint point = (FavouritePoint) favouritesList.get(menuInfo.position); + if(aItem.getItemId() == EDIT_ITEM){ + Builder builder = new AlertDialog.Builder(this); + builder.setTitle("Input new name of favourite point"); + final EditText editText = new EditText(this); + builder.setView(editText); + editText.setText(point.getName()); + builder.setNegativeButton("Cancel", null); + builder.setPositiveButton("Apply", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + boolean editied = helper.editFavouriteName(point, editText.getText().toString()); + if (editied) { + favouritesAdapter.notifyDataSetChanged(); + } + + } + }); + builder.create().show(); + return true; + } if(aItem.getItemId() == DELETE_ITEM){ + Builder builder = new AlertDialog.Builder(this); + builder.setMessage("Are you sure about deleting favourite point?"); + builder.setNegativeButton("No", null); + builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + boolean deleted = helper.deleteFavourite(point); + if (deleted) { + Toast.makeText(FavouritesActivity.this, "Favourite point " + point.getName() + " was succesfully deleted.", + Toast.LENGTH_SHORT).show(); + favouritesList.remove(point); + favouritesAdapter.notifyDataSetChanged(); + } + + } + }); + builder.create().show(); + return true; + } + return false; + } + + public static class FavouritesDbHelper extends SQLiteOpenHelper { + + private static final int DATABASE_VERSION = 1; + private static final String FAVOURITE_TABLE_NAME = "favourite"; + private static final String FAVOURITE_COL_NAME = "name"; + private static final String FAVOURITE_COL_LAT = "latitude"; + private static final String FAVOURITE_COL_LON = "longitude"; + private static final String FAVOURITE_TABLE_CREATE = "CREATE TABLE " + FAVOURITE_TABLE_NAME + " (" + + FAVOURITE_COL_NAME + " TEXT, " + FAVOURITE_COL_LAT + " double, " + + FAVOURITE_COL_LON + " double);"; + + FavouritesDbHelper(Context context) { + super(context, FAVOURITE_TABLE_NAME, null, DATABASE_VERSION); + } + + public boolean addFavourite(FavouritePoint p){ + SQLiteDatabase db = getWritableDatabase(); + if(db != null){ + db.execSQL("INSERT INTO " + FAVOURITE_TABLE_NAME + " VALUES (?, ?, ?)",new Object[]{p.getName(), p.getLatitude(), p.getLongitude()}); + return true; + } + return false; + } + + public List getFavouritePoints(){ + SQLiteDatabase db = getReadableDatabase(); + ArrayList list = new ArrayList(); + if(db != null){ + Cursor query = db.rawQuery("SELECT " + FAVOURITE_COL_NAME +", " + FAVOURITE_COL_LAT +"," + FAVOURITE_COL_LON +" FROM " + + 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()}); + p.setName(newName); + 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()}); + 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(); + } + } + @Override + protected void onDestroy() { + super.onDestroy(); + if(helper != null){ + helper.close(); + } + } + + class FavouritesAdapter extends ArrayAdapter { + FavouritesAdapter(List list) { + super(FavouritesActivity.this, R.layout.favourites_list_item, list); + this.setNotifyOnChange(false); + } + + + public View getView(int position, View convertView, ViewGroup parent) { + View row = convertView; + if (row == null) { + LayoutInflater inflater = getLayoutInflater(); + row = inflater.inflate(R.layout.favourites_list_item, parent, false); + } + TextView label = (TextView) row.findViewById(R.id.favourite_label); + TextView distanceLabel = (TextView) row.findViewById(R.id.favouritedistance_label); + ImageView icon = (ImageView) row.findViewById(R.id.favourite_icon); + FavouritePoint model = (FavouritePoint) getItem(position); + icon.setImageResource(R.drawable.poi); + LatLon lastKnownMapLocation = OsmandSettings.getLastKnownMapLocation(FavouritesActivity.this); + int dist = (int) (MapUtils.getDistance(model.getLatitude(), model.getLongitude(), + lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude())); + distanceLabel.setText(MapUtils.getFormattedDistance(dist)); + label.setText(model.getName()); + return row; + } + } + +} diff --git a/OsmAnd/src/com/osmand/activities/MainMenuActivity.java b/OsmAnd/src/com/osmand/activities/MainMenuActivity.java index 21d316bd23..92762f539c 100644 --- a/OsmAnd/src/com/osmand/activities/MainMenuActivity.java +++ b/OsmAnd/src/com/osmand/activities/MainMenuActivity.java @@ -41,9 +41,11 @@ public class MainMenuActivity extends Activity { private Button exitButton; private Button settingsButton; private Button searchButton; + private Button favouritesButton; private NotificationManager mNotificationManager; private int APP_NOTIFICATION_ID; + public void startApplication(){ @@ -108,8 +110,7 @@ public class MainMenuActivity extends Activity { showMap.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - final Intent mapIndent = new Intent(MainMenuActivity.this, - MapActivity.class); + final Intent mapIndent = new Intent(MainMenuActivity.this, MapActivity.class); startActivityForResult(mapIndent, 0); } @@ -118,8 +119,16 @@ public class MainMenuActivity extends Activity { settingsButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - final Intent settings = new Intent(MainMenuActivity.this, - SettingsActivity.class); + final Intent settings = new Intent(MainMenuActivity.this, SettingsActivity.class); + startActivity(settings); + } + }); + + favouritesButton = (Button) findViewById(R.id.FavoritesButton); + favouritesButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + final Intent settings = new Intent(MainMenuActivity.this, FavouritesActivity.class); startActivity(settings); } }); @@ -135,7 +144,7 @@ public class MainMenuActivity extends Activity { exitButton = (Button) findViewById(R.id.ExitButton); - exitButton.setVisibility(View.INVISIBLE); +// exitButton.setVisibility(View.INVISIBLE); exitButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { diff --git a/OsmAnd/src/com/osmand/activities/MapActivity.java b/OsmAnd/src/com/osmand/activities/MapActivity.java index a0cf599231..0547b2b4b2 100644 --- a/OsmAnd/src/com/osmand/activities/MapActivity.java +++ b/OsmAnd/src/com/osmand/activities/MapActivity.java @@ -1,6 +1,9 @@ package com.osmand.activities; import android.app.Activity; +import android.app.AlertDialog; +import android.app.AlertDialog.Builder; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.hardware.Sensor; @@ -24,13 +27,17 @@ import android.view.MenuItem; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; +import android.widget.EditText; import android.widget.ImageButton; +import android.widget.Toast; import android.widget.ZoomControls; import com.osmand.LogUtil; import com.osmand.OsmandSettings; import com.osmand.R; import com.osmand.ResourceManager; +import com.osmand.activities.FavouritesActivity.FavouritePoint; +import com.osmand.activities.FavouritesActivity.FavouritesDbHelper; import com.osmand.data.preparation.MapTileDownloader; import com.osmand.map.IMapLocationListener; import com.osmand.osm.LatLon; @@ -357,6 +364,9 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat final Intent settings = new Intent(MapActivity.this, SettingsActivity.class); startActivity(settings); return true; + } else if (item.getItemId() == R.id.map_add_to_favourite) { + addFavouritePoint(); + return true; } else if (item.getItemId() == R.id.map_specify_point) { openChangeLocationDialog(); return true; @@ -375,6 +385,33 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat } return super.onOptionsItemSelected(item); } + + private void addFavouritePoint(){ + final FavouritePoint p = new FavouritesActivity.FavouritePoint(); + p.setLatitude(mapView.getLatitude()); + p.setLongitude(mapView.getLongitude()); + p.setName("Favourite"); + + Builder builder = new AlertDialog.Builder(this); + builder.setTitle("Input name of favourite point"); + final EditText editText = new EditText(this); + builder.setView(editText); + builder.setNegativeButton("Cancel", null); + builder.setPositiveButton("Add", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + FavouritesDbHelper helper = new FavouritesActivity.FavouritesDbHelper(MapActivity.this); + p.setName(editText.getText().toString()); + boolean added = helper.addFavourite(p); + if (added) { + Toast.makeText(MapActivity.this, "Favourite point " + p.getName() + " was succesfully added.", Toast.LENGTH_SHORT) + .show(); + } + helper.close(); + } + }); + builder.create().show(); + } private void openChangeLocationDialog() { NavigatePointActivity dlg = new NavigatePointActivity(this, mapView);