--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2009/10/27 13:39:28 473 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/01/27 06:14:00 556 @@ -2,7 +2,7 @@ import java.util.ArrayList; import java.util.List; -import java.util.Locale; + import android.app.AlertDialog; import android.app.Dialog; @@ -12,14 +12,12 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; -import android.location.Address; -import android.location.Geocoder; 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; @@ -44,11 +42,9 @@ 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; + @@ -65,7 +61,7 @@ String dialogMessage = ""; ProgressDialog dialog; - LocationLookup locator = null; + LocationLookup locationLookup = null; FindStationsTask findStationsTask; StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); @@ -80,9 +76,8 @@ FavoritesMenu contextMenu = new FavoritesMenu(); IntSet favorites = new IntSet(); - - boolean showingFavorites = false; - + + WelcomeScreen.ListType listType; SharedPreferences prefs; /////////////////////////////////////////////////////////////////////////////////////////// @@ -92,7 +87,7 @@ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.main); + setContentView(R.layout.stationlist); adapter = new StationListAdapter(this); @@ -101,7 +96,7 @@ ListView lv = getListView(); lv.setOnCreateContextMenuListener(contextMenu); - locator = new LocationLookup(this, stationsFetched); + locationLookup = new LocationLookup(this, stationsFetched); prefs = getSharedPreferences("TrainStation", 0); @@ -110,14 +105,53 @@ 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 @@ -127,6 +161,7 @@ dialog.dismiss(); outState.putSerializable("stations", (ArrayList) stations); outState.putSerializable("location", location); + } @@ -134,21 +169,13 @@ @Override public boolean onCreateOptionsMenu(Menu menu) { MenuItem item; - - item = menu.add(0, OPTIONS_RESCAN, 0, "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, "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; } @@ -156,21 +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_FAVORITES: - startFavoriteLookup(); - break; case OPTIONS_MAP: Intent intent = new Intent(this,StationMapView.class); - intent.putExtra("userlocation", location ); ArrayList stationPoints = new ArrayList(); for (StationBean st : stations ) { @@ -181,18 +198,19 @@ 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"); - + if (loc != null) { + message.append("-Obtained by: ").append( loc.getProvider() ).append("\n"); + message.append("-Accuracy: ").append( (int)loc.getAccuracy()).append("m\n"); + message.append("-Latitude: ").append( loc.getLatitude()).append("\n"); + message.append("-Longitude: ").append( loc.getLongitude() ).append("\n"); + } else { + message.append(" - No location data!"); + } + MessageBox.showMessage(this, message.toString()); break; default: @@ -211,6 +229,19 @@ } + + public void showMessageAndClose(String message) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage(message) + .setCancelable(false) + .setPositiveButton("OK", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + StationList.this.finish(); + } + }) + .show(); + } @@ -240,13 +271,14 @@ if (et.getText().toString().length() >= 2) { startNameSearch(et.getText().toString()); } else { - MessageBox.showMessage(StationList.this, "Two characters minimum" ); + showMessageAndClose("Two characters minimum"); } } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); + StationList.this.finish(); // Close this Activity } }); return builder.create(); @@ -290,6 +322,9 @@ intent.putExtra("longitude", longitude); intent.putExtra("stationid", station.getId()); intent.putExtra("address", station.getAddress()); + intent.putExtra("isregional", station.isRegional()); + intent.putExtra("isstrain", station.isSTrain()); + intent.putExtra("ismetro", station.isMetro()); startActivity(intent); } @@ -301,7 +336,7 @@ dialogMessage = "Wait for location fix"; showDialog(DLG_PROGRESS); - locator.locateStations(); + locationLookup.locateStations(); stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); } @@ -310,7 +345,7 @@ showDialog(DLG_PROGRESS); findStationsTask = new FindStationsTask(); - findStationsTask.searchByName(name, locator.getLocation()); + findStationsTask.searchByName(name, locationLookup.getLocation()); findStationsTask.execute(); } @@ -322,10 +357,10 @@ showDialog(DLG_PROGRESS); findStationsTask = new FindStationsTask(); - findStationsTask.searchByIds(favorites.toString(), locator.getLocation()); + findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); findStationsTask.execute(); } else { - MessageBox.showMessage(this, "Favorite list is empty"); + showMessageAndClose( "Favorite list is empty"); } } @@ -337,11 +372,12 @@ showDialog(DLG_PROGRESS); findStationsTask = new FindStationsTask(); - findStationsTask.searchByLocation( locator.getLocation() ); + 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")); @@ -366,7 +402,7 @@ } return sb.toString(); - } + }*/ //////////////////////////////////////////////////////////////////////////// @@ -381,7 +417,7 @@ dismissDialog(DLG_PROGRESS); startLocatorTask(); - location = GeoPair.fromLocation( locator.getLocation() ); + location = GeoPair.fromLocation( locationLookup.getLocation() ); break; @@ -391,8 +427,8 @@ break; case LOCATIONFIXTIMEOUT: if (isRunning) { - locator.stopSearch(); - if (locator.hasLocation()) { + locationLookup.stopSearch(); + if (locationLookup.hasLocation()) { stationsFetched.sendEmptyMessage( GOTLOCATION ); } else { dismissDialog(DLG_PROGRESS); @@ -480,14 +516,17 @@ List stations = stationProvider.getStations(); for (StationBean station : stations) { - 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; @@ -498,32 +537,12 @@ super.onPostExecute(result); dialog.dismiss(); - //set title - String title; - switch (method) { - case ByLocation: - title = "Traininfo DK - Nearby stations"; - break; - case ByName: - title = "Traininfo DK - Search"; - break; - case ByList: - title = "Traininfo DK - Favorites"; - break; - default: - title = "";//not possible - } - - StationList.this.setTitle(title); - //set title end if (success) { if (stationProvider.getStations().size() == 0) MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! stations = stationProvider.getStations(); - adapter.setStations( stations ); - - showingFavorites = method.equals(LookupMethod.ByList); + adapter.setStations( stations ); } else { //communication or parse errors AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); @@ -586,7 +605,8 @@ favorites.remove(stationID); Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); - if (showingFavorites) { + + if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) { stations.remove(selectedPosition); adapter.notifyDataSetChanged(); }