--- android/TrainInfo/src/dk/thoerup/traininfo/TrainInfoList.java 2009/08/28 06:34:42 285 +++ android/TrainInfo/src/dk/thoerup/traininfo/TrainInfoList.java 2009/09/22 13:57:13 331 @@ -2,18 +2,24 @@ import java.util.ArrayList; import java.util.List; +import java.util.Locale; import android.app.Dialog; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Intent; +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.View; import android.widget.ListView; +import dk.thoerup.traininfo.provider.ProviderFactory; +import dk.thoerup.traininfo.provider.StationProvider; import dk.thoerup.traininfo.util.MessageBox; public class TrainInfoList extends ListActivity { @@ -27,12 +33,14 @@ /** Called when the activity is first created. */ ProgressDialog dialog; - StationLocator locator = null; + LocationLookup locator = null; LocatorTask locatorTask = new LocatorTask(); boolean isRunning = false; List stations = new ArrayList(); + StationProvider stationProvider = ProviderFactory.getStationProvider(); + StationListAdapter adapter = null; @SuppressWarnings("unchecked") @@ -41,13 +49,13 @@ super.onCreate(savedInstanceState); setContentView(R.layout.main); - StationLocator.removeMockLocation(this); - //StationLocator.injectMockLocation(this); + //LocationLookup.removeMockLocation(this); + LocationLookup.injectMockLocation(this); adapter = new StationListAdapter(this); setListAdapter(adapter); - locator = new StationLocator(this, stationsFetched); + locator = new LocationLookup(this, stationsFetched); if (savedInstanceState == null) { startLookup(); } else { @@ -109,13 +117,7 @@ dialog.setMessage("Finding nearby stations"); locatorTask.execute(); break; - case GOTSTATIONLIST: - dialog.dismiss(); - if (locator.getStations().size() == 0) - MessageBox.showMessage(TrainInfoList.this,"Error loading station list!"); - stations = locator.getStations(); - adapter.setStations( stations ); - break; + case NOPROVIDER: dialog.dismiss(); MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); @@ -146,22 +148,52 @@ @Override protected void onListItemClick(ListView l, View v, int position, long id) { 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("address", station.getAddress()); intent.putExtra("distance", station.getDistance()); - intent.putExtra("latitude", station.getLatitude()); - intent.putExtra("longitude", station.getLongitude()); + intent.putExtra("latitude", latitude); + intent.putExtra("longitude", longitude); + intent.putExtra("stationid", station.getId()); + intent.putExtra("address", station.getAddress()); startActivity(intent); } + 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(); + } class LocatorTask extends AsyncTask { + boolean success; @Override protected void onPreExecute() { @@ -170,7 +202,15 @@ @Override protected Void doInBackground(Void... params) { - locator.findNearestStations(); + Location loc = locator.getLocation(); + success = stationProvider.lookupStations(loc); + + + List stations = stationProvider.getStations(); + for (StationBean station : stations) { + String addr = lookupAddress(station.getLatitude(), station.getLongitude()); + station.setAddress(addr); + } return null; } @@ -178,7 +218,17 @@ @Override protected void onPostExecute(Void result) { super.onPostExecute(result); - + dialog.dismiss(); + + if (success) { + if (stationProvider.getStations().size() == 0) + MessageBox.showMessage(TrainInfoList.this, "No stations found!"); // this should not be possible !?! + stations = stationProvider.getStations(); + adapter.setStations( stations ); + + } else { //communication or parse errors + MessageBox.showMessage(TrainInfoList.this, "Error finding stations!"); + } } } } \ No newline at end of file