--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/01/27 06:07:27 555 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/02/02 08:52:45 579 @@ -36,6 +36,8 @@ import dk.thoerup.traininfo.util.IntSet; import dk.thoerup.traininfo.util.MessageBox; +import static dk.thoerup.traininfo.R.string.*; + public class StationList extends ListActivity { public static final int GOTLOCATION = 1001; public static final int GOTSTATIONLIST = 1002; @@ -133,16 +135,16 @@ } protected void setTitle() { - String dialogTitle = getResources().getString(R.string.app_name); + String dialogTitle = getResources().getString(app_name); switch (listType) { case ListNearest: - dialogTitle += " - Nearby stations"; + dialogTitle += " - " + getString(stationlist_nearbystations); break; case ListSearch: - dialogTitle += " - Search"; + dialogTitle += " - " + getString(stationlist_search); break; case ListFavorites: - dialogTitle += " - Favorites"; + dialogTitle += " - " + getString(stationlist_favorites); break; default: dialogTitle = "";//not possible @@ -169,11 +171,11 @@ @Override public boolean onCreateOptionsMenu(Menu menu) { MenuItem item; - - item = menu.add(0, OPTIONS_MAP, 0, "Station map"); + + item = menu.add(0, OPTIONS_MAP, 0, getString(stationlist_stationmap)); item.setIcon(android.R.drawable.ic_menu_mapmode); - item = menu.add(0, OPTIONS_GPSINFO, 0, "GPS Info"); + item = menu.add(0, OPTIONS_GPSINFO, 0, getString(stationlist_gpsinfo)); item.setIcon(android.R.drawable.ic_menu_mapmode); return true; @@ -201,16 +203,16 @@ case OPTIONS_GPSINFO: Location loc = locationLookup.getLocation(); StringBuffer message = new StringBuffer(); - message.append("Location info:\n"); + message.append( getString(stationlist_locationinfo) ).append(":\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"); + message.append( getString(stationlist_obtainedby) ).append( loc.getProvider() ).append("\n"); + message.append( getString(stationlist_accuracy) ).append( (int)loc.getAccuracy()).append("m\n"); + message.append( getString(stationlist_latitude) ).append( (float)loc.getLatitude()).append("\n"); + message.append( getString(stationlist_longitude) ).append( (float)loc.getLongitude() ).append("\n"); } else { - message.append(" - No location data!"); - } - + message.append( getString(stationlist_nolocation) ); + } + MessageBox.showMessage(this, message.toString()); break; default: @@ -229,6 +231,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(); + } @@ -238,7 +253,7 @@ switch (id) { case DLG_PROGRESS: ProgressDialog dlg = new ProgressDialog(this); - dlg.setMessage("Wait for location fix"); + dlg.setMessage( getString(stationlist_waitforlocation) ); dlg.setCancelable(false); return dlg; case DLG_STATIONNAME: @@ -248,21 +263,21 @@ AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle("Station search"); + builder.setTitle( getString(stationlist_stationsearch) ); builder.setView(rootView); builder.setCancelable(true); - builder.setPositiveButton("Search", new DialogInterface.OnClickListener() { + builder.setPositiveButton( getString(generic_search), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { EditText et = (EditText) rootView.findViewById(R.id.EditText); dialog.dismiss(); if (et.getText().toString().length() >= 2) { startNameSearch(et.getText().toString()); } else { - MessageBox.showMessage(StationList.this, "Two characters minimum" ); + showMessageAndClose( getString(stationlist_twocharmin) ); } } }); - builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); StationList.this.finish(); // Close this Activity @@ -296,22 +311,9 @@ super.onListItemClick(l, v, position, id); StationBean station = stations.get(position); - - double latitude = station.getLatitude(); - double longitude = station.getLongitude(); - - Intent intent = new Intent(this, DepartureList.class); - intent.putExtra("name", station.getName()); - intent.putExtra("distance", station.getDistance()); - intent.putExtra("latitude", latitude); - 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()); + intent.putExtra("stationbean", station); startActivity(intent); } @@ -319,8 +321,8 @@ // public void startLookup() { - isRunning = true; - dialogMessage = "Wait for location fix"; + isRunning = true; + dialogMessage = getString( stationlist_waitforlocation ); showDialog(DLG_PROGRESS); locationLookup.locateStations(); @@ -328,11 +330,11 @@ } void startNameSearch(String name) { - dialogMessage = "Finding stations by name"; + dialogMessage = getString( stationlist_findbyname ); showDialog(DLG_PROGRESS); findStationsTask = new FindStationsTask(); - findStationsTask.searchByName(name, locationLookup.getLocation()); + findStationsTask.searchByName(name); findStationsTask.execute(); } @@ -340,14 +342,14 @@ public void startFavoriteLookup() { if (favorites.size() > 0) { - dialogMessage = "Loading favorites"; + dialogMessage = getString( stationlist_loadfavorites ); showDialog(DLG_PROGRESS); findStationsTask = new FindStationsTask(); - findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); + findStationsTask.searchByIds( favorites.toString() ); findStationsTask.execute(); } else { - MessageBox.showMessage(this, "Favorite list is empty"); + showMessageAndClose( getString( stationlist_nofavorites ) ); } } @@ -355,42 +357,14 @@ void startLocatorTask() { - dialogMessage = "Finding nearby stations"; + dialogMessage = getString( stationlist_findingnearby ); showDialog(DLG_PROGRESS); 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")); - StringBuilder sb = new StringBuilder(); - Log.i("lookupaddr", "" + latitude + "/" + longitude); - try { - List
addressList = coder.getFromLocation(latitude, longitude, 1); - Address addr = addressList.get(0); - - - int max = addr.getMaxAddressLineIndex(); - for (int i=0; i0) - sb.append(", "); - - sb.append(addr.getAddressLine(i)); - } - - - } catch (Exception e) { - Log.e("DepartureList", "geocoder failed", e); - } - - return sb.toString(); - }*/ - //////////////////////////////////////////////////////////////////////////// // Inner classes @@ -410,7 +384,7 @@ case NOPROVIDER: dismissDialog(DLG_PROGRESS); - MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); + MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); break; case LOCATIONFIXTIMEOUT: if (isRunning) { @@ -421,16 +395,16 @@ dismissDialog(DLG_PROGRESS); AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); - builder.setMessage("Location fix timed out"); + builder.setMessage( getString( stationlist_gpstimeout) ); builder.setCancelable(true); - builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { + builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); startLookup(); } }); - builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } @@ -454,10 +428,9 @@ Location loc; String ids; - public void searchByName(String n, Location l) { + public void searchByName(String n) { method = LookupMethod.ByName; - loc = l; name = n; } @@ -466,10 +439,9 @@ loc = l; } - public void searchByIds(String id, Location l) { + public void searchByIds(String id) { method = LookupMethod.ByList; - loc = l; ids = id; } @@ -499,23 +471,6 @@ } - Location dummy = new Location("gps"); - List stations = stationProvider.getStations(); - - for (StationBean station : stations) { - - 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; } @@ -526,16 +481,17 @@ if (success) { - if (stationProvider.getStations().size() == 0) - MessageBox.showMessage(StationList.this, "No stations found!"); // this should not be possible !?! + if (stationProvider.getStations().size() == 0) { + showMessageAndClose(getString(stationlist_nostations)); + } stations = stationProvider.getStations(); adapter.setStations( stations ); } else { //communication or parse errors AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); - builder.setMessage("Error on finding nearby stations"); + builder.setMessage(getString(stationlist_nearbyerror)); builder.setCancelable(true); - builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { + builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); @@ -547,7 +503,7 @@ }); } }); - builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } @@ -573,9 +529,9 @@ int stationID = stations.get(selectedPosition).getId(); if (!favorites.contains(stationID)) { - menu.add(0, FAVORITES_ADD, 0, "Add to favorites"); + menu.add(0, FAVORITES_ADD, 0, getString(stationlist_addfavorite) ); } else { - menu.add(0, FAVORITES_REMOVE, 0, "Remove from favorites"); + menu.add(0, FAVORITES_REMOVE, 0, getString(stationlist_removefavorite) ); } } @@ -586,11 +542,11 @@ int stationID = sb.getId(); if (item.getItemId() == FAVORITES_ADD) { favorites.add(stationID); - Toast.makeText(StationList.this, "Station added", Toast.LENGTH_SHORT).show(); + Toast.makeText(StationList.this, getString(stationlist_stationadded), Toast.LENGTH_SHORT).show(); } else { favorites.remove(stationID); - Toast.makeText(StationList.this, "Station removed", Toast.LENGTH_SHORT).show(); + Toast.makeText(StationList.this, getString(stationlist_stationremoved), Toast.LENGTH_SHORT).show(); if (listType.equals( WelcomeScreen.ListType.ListFavorites) ) {