--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2009/10/08 11:50:46 420 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/01/22 11:28:56 546 @@ -2,7 +2,7 @@ import java.util.ArrayList; import java.util.List; -import java.util.Locale; + import android.app.AlertDialog; import android.app.Dialog; @@ -10,24 +10,30 @@ import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; -import android.location.Address; -import android.location.Geocoder; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; import android.location.Location; import android.os.AsyncTask; import android.os.Bundle; 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 { @@ -36,20 +42,27 @@ public static final int NOPROVIDER = 1003; public static final int LOCATIONFIXTIMEOUT = 1004; - public static final int OPTIONS_RESCAN = 2001; - 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_GPSINFO = 2004; + + public static final int DLG_PROGRESS = 3001; public static final int DLG_STATIONNAME = 3002; - /** Called when the activity is first created. */ + static enum LookupMethod { + ByLocation, + ByName, + ByList, + MethodNone + } + + String dialogMessage = ""; ProgressDialog dialog; - LocationLookup locator = null; - LocatorTask locatorTask; + LocationLookup locationLookup = null; + FindStationsTask findStationsTask; StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); GeoPair location = new GeoPair(); @@ -61,11 +74,11 @@ StationListAdapter adapter = null; - static enum LookupMethod { - ByLocation, - ByName, - MethodNone - } + FavoritesMenu contextMenu = new FavoritesMenu(); + IntSet favorites = new IntSet(); + + WelcomeScreen.ListType listType; + SharedPreferences prefs; /////////////////////////////////////////////////////////////////////////////////////////// //Activity call backs @@ -74,21 +87,71 @@ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.main); + setContentView(R.layout.stationlist); adapter = new StationListAdapter(this); setListAdapter(adapter); - locator = new LocationLookup(this, stationsFetched); + ListView lv = getListView(); + lv.setOnCreateContextMenuListener(contextMenu); + + locationLookup = new LocationLookup(this, stationsFetched); + + + prefs = getSharedPreferences("TrainStation", 0); + String favoriteString = prefs.getString("favorites", ""); + if (! favoriteString.equals("") ) { + favorites.fromString(favoriteString); + } + + listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); + setTitle(); + if (savedInstanceState == null) { - startLookup(); + + + switch (listType) { + case ListNearest: + startLookup(); + break; + case ListSearch: + this.showDialog(DLG_STATIONNAME); + break; + case ListFavorites: + startFavoriteLookup(); + break; + default: + // Not possible !?! + } + } else { stations = (ArrayList) savedInstanceState.getSerializable("stations"); adapter.setStations(stations); location = (GeoPair) savedInstanceState.getSerializable("location"); } + } + protected void setTitle() { + String dialogTitle = getResources().getString(R.string.app_name); + switch (listType) { + case ListNearest: + dialogTitle += " - Nearby stations"; + break; + case ListSearch: + dialogTitle += " - Search"; + break; + case ListFavorites: + dialogTitle += " - Favorites"; + break; + default: + dialogTitle = "";//not possible + } + + setTitle(dialogTitle); + } + + @Override @@ -98,6 +161,7 @@ dialog.dismiss(); outState.putSerializable("stations", (ArrayList) stations); outState.putSerializable("location", location); + } @@ -105,18 +169,13 @@ @Override public boolean onCreateOptionsMenu(Menu menu) { MenuItem item; - - item = menu.add(0, OPTIONS_RESCAN, 0, "Find nearest stations"); - item.setIcon(android.R.drawable.ic_menu_mylocation); - - item = menu.add(0, OPTIONS_NAMESEARCH, 0, "Search for station"); - item.setIcon(android.R.drawable.ic_menu_search); - - item = menu.add(0, OPTIONS_MAP, 0, "Show station map"); + + item = menu.add(0, OPTIONS_MAP, 0, "Station map"); item.setIcon(android.R.drawable.ic_menu_mapmode); - item = menu.add(0, OPTIONS_ABOUT, 0, "About"); - item.setIcon(android.R.drawable.ic_menu_info_details); + item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info"); + item.setIcon(android.R.drawable.ic_menu_mapmode); + return true; } @@ -124,18 +183,11 @@ public boolean onOptionsItemSelected(MenuItem item) { boolean retval = true; - + //TODO: Cleanup switch (item.getItemId()) { - case OPTIONS_RESCAN: - startLookup(); - break; - case OPTIONS_NAMESEARCH: - showDialog(DLG_STATIONNAME); - break; case OPTIONS_MAP: Intent intent = new Intent(this,StationMapView.class); - intent.putExtra("userlocation", location ); ArrayList stationPoints = new ArrayList(); for (StationBean st : stations ) { @@ -146,18 +198,14 @@ startActivity(intent); break; - case OPTIONS_ABOUT: - String ver = this.getResources().getString(R.string.app_version); - - Location loc = locator.getLocation(); + case OPTIONS_GPSINFO: + Location loc = locationLookup.getLocation(); StringBuffer message = new StringBuffer(); - message.append("TrainInfo DK v").append(ver).append("\n"); - message.append("By Torben Nielsen\n"); - message.append("\n"); message.append("Location info:\n"); message.append("-Obtained by: ").append(loc != null ? loc.getProvider() : "-").append("\n"); message.append("-Accuracy: ").append(loc != null ? (int)loc.getAccuracy() : "-").append("m\n"); - + message.append("-Latitude: ").append(loc != null ? loc.getLatitude() : "-").append("\n"); + message.append("-Longitude: ").append(loc != null ? loc.getLongitude() : "-").append("\n"); MessageBox.showMessage(this, message.toString()); break; default: @@ -166,6 +214,19 @@ return retval; } + + + + @Override + public boolean onContextItemSelected(MenuItem item) { + contextMenu.onContextItemSelected(item); + return true; + + + } + + + @Override protected Dialog onCreateDialog(int id) { @@ -253,7 +314,7 @@ dialogMessage = "Wait for location fix"; showDialog(DLG_PROGRESS); - locator.locateStations(); + locationLookup.locateStations(); stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); } @@ -261,10 +322,24 @@ dialogMessage = "Finding stations by name"; showDialog(DLG_PROGRESS); - locatorTask = new LocatorTask(); - locatorTask.searchByName(name, locator.getLocation()); - locatorTask.execute(); + findStationsTask = new FindStationsTask(); + findStationsTask.searchByName(name, locationLookup.getLocation()); + findStationsTask.execute(); + + } + + public void startFavoriteLookup() { + if (favorites.size() > 0) { + dialogMessage = "Loading favorites"; + showDialog(DLG_PROGRESS); + + findStationsTask = new FindStationsTask(); + findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); + findStationsTask.execute(); + } else { + MessageBox.showMessage(this, "Favorite list is empty"); + } } @@ -274,12 +349,13 @@ dialogMessage = "Finding nearby stations"; showDialog(DLG_PROGRESS); - locatorTask = new LocatorTask(); - locatorTask.searchByLocation( locator.getLocation() ); - locatorTask.execute(); + findStationsTask = new FindStationsTask(); + findStationsTask.searchByLocation( locationLookup.getLocation() ); + findStationsTask.execute(); } + /* TODO: Remove this no longer needed function String lookupAddress(double latitude, double longitude) { Geocoder coder = new Geocoder(this, new Locale("da")); @@ -304,7 +380,7 @@ } return sb.toString(); - } + }*/ //////////////////////////////////////////////////////////////////////////// @@ -319,7 +395,7 @@ dismissDialog(DLG_PROGRESS); startLocatorTask(); - location = GeoPair.fromLocation( locator.getLocation() ); + location = GeoPair.fromLocation( locationLookup.getLocation() ); break; @@ -329,8 +405,8 @@ break; case LOCATIONFIXTIMEOUT: if (isRunning) { - locator.stopSearch(); - if (locator.hasLocation()) { + locationLookup.stopSearch(); + if (locationLookup.hasLocation()) { stationsFetched.sendEmptyMessage( GOTLOCATION ); } else { dismissDialog(DLG_PROGRESS); @@ -361,12 +437,13 @@ }; - class LocatorTask extends AsyncTask { + class FindStationsTask extends AsyncTask { LookupMethod method = LookupMethod.MethodNone; boolean success; String name; Location loc; + String ids; public void searchByName(String n, Location l) { @@ -380,6 +457,13 @@ loc = l; } + public void searchByIds(String id, Location l) { + + method = LookupMethod.ByList; + loc = l; + ids = id; + } + @Override protected void onPreExecute() { @@ -390,25 +474,37 @@ @Override protected Void doInBackground(Void... params) { - - if (method.equals(LookupMethod.ByLocation)) + + switch (method) { + case ByLocation: success = stationProvider.lookupStations(loc); + break; + case ByName: + success = stationProvider.lookupStationsByName(name); + break; + case ByList: + success = stationProvider.lookupStationsByIds(ids); + break; + default: + success = false; // not possible + } - if (method.equals(LookupMethod.ByName)) - success = stationProvider.lookupStations(name); Location dummy = new Location("gps"); List stations = stationProvider.getStations(); for (StationBean station : stations) { - String addr = lookupAddress(station.getLatitude(), station.getLongitude()); - station.setAddress(addr); - - if (method.equals(LookupMethod.ByName) ) { - dummy.setLatitude(station.getLatitude()); - dummy.setLongitude(station.getLongitude()); - station.setDistance( (int)loc.distanceTo(dummy) ); + + if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { + if (loc != null) { //only do the distance calc if we have a location + dummy.setLatitude(station.getLatitude()); + dummy.setLongitude(station.getLongitude()); + station.setDistance( (int)loc.distanceTo(dummy) ); + } else { + station.setDistance(0); + } } + } return null; @@ -419,6 +515,7 @@ super.onPostExecute(result); dialog.dismiss(); + if (success) { if (stationProvider.getStations().size() == 0) MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! @@ -450,4 +547,51 @@ } } } + + + 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.contains(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(); + + + if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { + stations.remove(selectedPosition); + adapter.notifyDataSetChanged(); + } + } + Editor ed = prefs.edit(); + ed.putString("favorites", favorites.toString()); + ed.commit(); + } + } } \ No newline at end of file