add favourites activity

git-svn-id: https://osmand.googlecode.com/svn/trunk@126 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-06-03 09:34:04 +00:00
parent 97cf6850ff
commit 20d7b06a39
10 changed files with 367 additions and 17 deletions

View file

@ -11,13 +11,7 @@ public class ToDoConstants {
*/
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
}

View file

@ -12,11 +12,12 @@
</activity>
<activity android:name=".activities.MapActivity" android:label="@string/app_name"></activity>
<activity android:name=".activities.SettingsActivity" android:label="@string/settings_activity"></activity>
<activity android:label="@string/search_activity" android:name=".activities.search.SearchActivity"></activity>
<activity android:name=".activities.search.SearchActivity" android:label="@string/search_activity" ></activity>
<activity android:name=".activities.NavigatePointActivity"></activity>
<activity android:name=".activities.FavouritesActivity"></activity>
<activity android:name=".activities.search.SearchPOIActivity" android:label="@string/searchpoi_activity"></activity>
<activity android:name=".activities.search.SearchPOIListActivity"></activity>
<activity android:name=".activities.NavigatePointActivity"></activity>
<activity android:name=".activities.search.SearchAddressActivity"></activity>
<activity android:name=".activities.search.SearchCityByNameActivity"></activity>
<activity android:name=".activities.search.SearchRegionByNameActivity"></activity>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is at /res/layout/list.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
</LinearLayout>

View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="@+id/favourite_icon" android:layout_width="25px"
android:paddingLeft="2px" android:paddingRight="2px"
android:paddingTop="2px" android:layout_height="fill_parent" />
<TextView android:id="@+id/favouritedistance_label"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center"
android:textSize="25px" android:maxWidth="150px" android:minWidth="100px"/>
<TextView android:id="@+id/favourite_label" android:layout_width="wrap_content" android:layout_marginLeft="10px"
android:layout_height="wrap_content" android:textSize="25px" />
</LinearLayout>

View file

@ -8,7 +8,7 @@
android:paddingTop="2px" android:layout_height="fill_parent" />
<TextView android:id="@+id/poidistance_label"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center"
android:textSize="25px" android:maxWidth="100px" android:minWidth="100px"/>
<TextView android:id="@+id/poi_label" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="25px" />

View file

@ -2,8 +2,10 @@
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/map_context_menu" android:menuCategory="container"><item android:id="@+id/map_show_settings" android:title="@string/settings_Button"></item>
<item android:id="@+id/map_add_to_favourite" android:title="@string/add_to_favourite"></item>
<item android:title="@string/navigate_to_point" android:id="@+id/map_navigate_to_point"></item>
<item android:title="@string/map_specify_point" android:id="@+id/map_specify_point"></item>
</group>
</menu>

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="add_to_favourite">Add to Favourites</string>
<string name="use_english_names_descr">Select use native or english names</string>
<string name="use_english_names">Use english names</string>
<string name="app_settings">Application settings</string>

View file

@ -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<FavouritePoint> 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<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 " +
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<FavouritePoint> {
FavouritesAdapter(List<FavouritePoint> 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;
}
}
}

View file

@ -41,11 +41,13 @@ 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(){
if(!applicationAlreadyStarted){
// TODO exception!!! has leaked window ?
@ -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) {

View file

@ -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;
@ -376,6 +386,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);
dlg.showDialog();