--- android/TrainInfo/src/dk/thoerup/traininfo/TrainInfoList.java 2009/09/11 11:51:57 318 +++ android/TrainInfo/src/dk/thoerup/traininfo/TrainInfoList.java 2009/09/11 12:24:53 319 @@ -10,6 +10,7 @@ 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; @@ -17,6 +18,8 @@ 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 { @@ -30,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") @@ -44,13 +49,13 @@ super.onCreate(savedInstanceState); setContentView(R.layout.main); - StationLocator.removeMockLocation(this); + LocationLookup.removeMockLocation(this); //StationLocator.injectMockLocation(this); adapter = new StationListAdapter(this); setListAdapter(adapter); - locator = new StationLocator(this, stationsFetched); + locator = new LocationLookup(this, stationsFetched); if (savedInstanceState == null) { startLookup(); } else { @@ -112,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."); @@ -194,6 +193,7 @@ } class LocatorTask extends AsyncTask { + boolean success; @Override protected void onPreExecute() { @@ -202,9 +202,11 @@ @Override protected Void doInBackground(Void... params) { - locator.findNearestStations(); + Location loc = locator.getLocation(); + success = stationProvider.lookupStations(loc); + - List stations = locator.getStations(); + List stations = stationProvider.getStations(); for (StationBean station : stations) { String addr = lookupAddress(station.getLatitude(), station.getLongitude()); station.setAddress(addr); @@ -216,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