--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2009/10/10 09:56:10 432 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2009/10/10 11:30:08 433 @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.prefs.Preferences; import android.app.AlertDialog; import android.app.Dialog; @@ -10,6 +11,7 @@ import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; import android.location.Address; import android.location.Geocoder; import android.location.Location; @@ -18,16 +20,22 @@ import android.os.Handler; import android.os.Message; import android.util.Log; +import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.ContextMenu.ContextMenuInfo; +import android.view.View.OnCreateContextMenuListener; +import android.widget.AdapterView; import android.widget.EditText; import android.widget.ListView; +import android.widget.Toast; import dk.thoerup.traininfo.provider.ProviderFactory; import dk.thoerup.traininfo.provider.StationProvider; import dk.thoerup.traininfo.stationmap.GeoPair; import dk.thoerup.traininfo.stationmap.StationMapView; +import dk.thoerup.traininfo.util.IntSet; import dk.thoerup.traininfo.util.MessageBox; public class StationList extends ListActivity { @@ -40,6 +48,8 @@ public static final int OPTIONS_NAMESEARCH = 2002; public static final int OPTIONS_MAP = 2003; public static final int OPTIONS_ABOUT = 2004; + public static final int OPTIONS_FAVORITES = 2005; + public static final int DLG_PROGRESS = 3001; @@ -61,12 +71,18 @@ StationListAdapter adapter = null; + FavoritesMenu contextMenu = new FavoritesMenu(); + IntSet favorites = new IntSet(); + static enum LookupMethod { ByLocation, ByName, + ByList, MethodNone } + SharedPreferences prefs; + /////////////////////////////////////////////////////////////////////////////////////////// //Activity call backs @@ -80,7 +96,18 @@ adapter = new StationListAdapter(this); setListAdapter(adapter); + ListView lv = getListView(); + lv.setOnCreateContextMenuListener(contextMenu); + locator = new LocationLookup(this, stationsFetched); + + + prefs = getSharedPreferences("TrainStation", 0); + String favoriteString = prefs.getString("favorites", ""); + if (! favoriteString.equals("") ) { + favorites.fromString(favoriteString); + } + if (savedInstanceState == null) { startLookup(); } else { @@ -112,6 +139,9 @@ item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); item.setIcon(android.R.drawable.ic_menu_search); + item = menu.add(0, OPTIONS_FAVORITES, 0, "Favorites"); + item.setIcon(android.R.drawable.ic_menu_agenda); + item = menu.add(0, OPTIONS_MAP, 0, "Show station map"); item.setIcon(android.R.drawable.ic_menu_mapmode); @@ -132,6 +162,9 @@ case OPTIONS_NAMESEARCH: showDialog(DLG_STATIONNAME); break; + case OPTIONS_FAVORITES: + startFavoriteLookup(); + break; case OPTIONS_MAP: Intent intent = new Intent(this,StationMapView.class); @@ -166,6 +199,19 @@ return retval; } + + + + @Override + public boolean onContextItemSelected(MenuItem item) { + contextMenu.onContextItemSelected(item); + return true; + + + } + + + @Override protected Dialog onCreateDialog(int id) { @@ -266,6 +312,20 @@ locatorTask.execute(); } + + public void startFavoriteLookup() { + + if (favorites.toString().length() > 0) { + dialogMessage = "Loading favorites"; + showDialog(DLG_PROGRESS); + + locatorTask = new LocatorTask(); + locatorTask.searchByIds(favorites.toString(), locator.getLocation()); + locatorTask.execute(); + } else { + MessageBox.showMessage(this, "Favorite list is empty"); + } + } @@ -367,6 +427,7 @@ boolean success; String name; Location loc; + String ids; public void searchByName(String n, Location l) { @@ -380,6 +441,13 @@ loc = l; } + public void searchByIds(String id, Location l) { + + method = LookupMethod.ByList; + loc = l; + ids = id; + } + @Override protected void onPreExecute() { @@ -395,7 +463,10 @@ success = stationProvider.lookupStations(loc); if (method.equals(LookupMethod.ByName)) - success = stationProvider.lookupStations(name); + success = stationProvider.lookupStationsByName(name); + + if (method.equals(LookupMethod.ByList)) + success = stationProvider.lookupStationsByIds(ids); Location dummy = new Location("gps"); List stations = stationProvider.getStations(); @@ -404,7 +475,7 @@ String addr = lookupAddress(station.getLatitude(), station.getLongitude()); station.setAddress(addr); - if (method.equals(LookupMethod.ByName) ) { + if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { dummy.setLatitude(station.getLatitude()); dummy.setLongitude(station.getLongitude()); station.setDistance( (int)loc.distanceTo(dummy) ); @@ -450,4 +521,43 @@ } } } + + + class FavoritesMenu implements OnCreateContextMenuListener { + private final static int FAVORITES_ADD = 9001; + private final static int FAVORITES_REMOVE = 9002; + + private int selectedPosition; + + + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { + + AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; + selectedPosition = info.position; + int stationID = stations.get(selectedPosition).getId(); + + if (!favorites.hasInt(stationID)) { + menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); + } else { + menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); + } + + } + + public void onContextItemSelected(MenuItem item) { + StationBean sb = stations.get(selectedPosition); + + int stationID = sb.getId(); + if (item.getItemId() == FAVORITES_ADD) { + favorites.add(stationID); + Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); + } else { + favorites.remove(stationID); + Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); + } + + prefs.edit().putString("favorites", favorites.toString()); + } + } } \ No newline at end of file