--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2009/10/10 11:34:35 435 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2009/10/29 08:56:17 481 @@ -44,22 +44,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_FAVORITES = 2005; + 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; + FindStationsTask findStationsTask; StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); GeoPair location = new GeoPair(); @@ -73,14 +78,9 @@ FavoritesMenu contextMenu = new FavoritesMenu(); IntSet favorites = new IntSet(); - - static enum LookupMethod { - ByLocation, - ByName, - ByList, - MethodNone - } - + + WelcomeScreen.ListType listType; + String dialogTitle; SharedPreferences prefs; /////////////////////////////////////////////////////////////////////////////////////////// @@ -90,7 +90,7 @@ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.main); + setContentView(R.layout.stationlist); adapter = new StationListAdapter(this); @@ -109,11 +109,27 @@ } if (savedInstanceState == null) { - startLookup(); + listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); + 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"); + dialogTitle = savedInstanceState.getString("title"); + setTitle(dialogTitle); } } @@ -125,6 +141,7 @@ dialog.dismiss(); outState.putSerializable("stations", (ArrayList) stations); outState.putSerializable("location", location); + outState.putString("title", dialogTitle); } @@ -132,21 +149,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_FAVORITES, 0, "Favorites"); - item.setIcon(android.R.drawable.ic_menu_agenda); - - 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; } @@ -154,17 +163,8 @@ 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_FAVORITES: - startFavoriteLookup(); - break; case OPTIONS_MAP: Intent intent = new Intent(this,StationMapView.class); @@ -179,14 +179,9 @@ startActivity(intent); break; - case OPTIONS_ABOUT: - String ver = this.getResources().getString(R.string.app_version); - + case OPTIONS_GPSINFO: Location loc = locator.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"); @@ -307,21 +302,21 @@ dialogMessage = "Finding stations by name"; showDialog(DLG_PROGRESS); - locatorTask = new LocatorTask(); - locatorTask.searchByName(name, locator.getLocation()); - locatorTask.execute(); + findStationsTask = new FindStationsTask(); + findStationsTask.searchByName(name, locator.getLocation()); + findStationsTask.execute(); } public void startFavoriteLookup() { - if (favorites.toString().length() > 0) { + if (favorites.size() > 0) { dialogMessage = "Loading favorites"; showDialog(DLG_PROGRESS); - locatorTask = new LocatorTask(); - locatorTask.searchByIds(favorites.toString(), locator.getLocation()); - locatorTask.execute(); + findStationsTask = new FindStationsTask(); + findStationsTask.searchByIds(favorites.toString(), locator.getLocation()); + findStationsTask.execute(); } else { MessageBox.showMessage(this, "Favorite list is empty"); } @@ -334,9 +329,9 @@ dialogMessage = "Finding nearby stations"; showDialog(DLG_PROGRESS); - locatorTask = new LocatorTask(); - locatorTask.searchByLocation( locator.getLocation() ); - locatorTask.execute(); + findStationsTask = new FindStationsTask(); + findStationsTask.searchByLocation( locator.getLocation() ); + findStationsTask.execute(); } @@ -421,7 +416,7 @@ }; - class LocatorTask extends AsyncTask { + class FindStationsTask extends AsyncTask { LookupMethod method = LookupMethod.MethodNone; boolean success; @@ -458,15 +453,21 @@ @Override protected Void doInBackground(Void... params) { - - if (method.equals(LookupMethod.ByLocation)) + + switch (method) { + case ByLocation: success = stationProvider.lookupStations(loc); - - if (method.equals(LookupMethod.ByName)) + break; + case ByName: success = stationProvider.lookupStationsByName(name); - - if (method.equals(LookupMethod.ByList)) + break; + case ByList: success = stationProvider.lookupStationsByIds(ids); + break; + default: + success = false; // not possible + } + Location dummy = new Location("gps"); List stations = stationProvider.getStations(); @@ -475,11 +476,17 @@ String addr = lookupAddress(station.getLatitude(), station.getLongitude()); station.setAddress(addr); + if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { - dummy.setLatitude(station.getLatitude()); - dummy.setLongitude(station.getLongitude()); - station.setDistance( (int)loc.distanceTo(dummy) ); + 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; @@ -490,6 +497,24 @@ super.onPostExecute(result); dialog.dismiss(); + //set title + switch (method) { + case ByLocation: + dialogTitle = "Traininfo DK - Nearby stations"; + break; + case ByName: + dialogTitle = "Traininfo DK - Search"; + break; + case ByList: + dialogTitle = "Traininfo DK - Favorites"; + break; + default: + dialogTitle = "";//not possible + } + + StationList.this.setTitle(dialogTitle); + //set title end + if (success) { if (stationProvider.getStations().size() == 0) MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! @@ -537,7 +562,7 @@ selectedPosition = info.position; int stationID = stations.get(selectedPosition).getId(); - if (!favorites.hasInt(stationID)) { + if (!favorites.contains(stationID)) { menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); } else { menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); @@ -553,8 +578,15 @@ 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());