--- android/TrainInfo/src/dk/thoerup/traininfo/TrainInfoList.java 2009/09/07 13:23:48 302 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2009/09/23 12:51:49 336 @@ -4,12 +4,15 @@ import java.util.List; import java.util.Locale; +import android.app.AlertDialog; import android.app.Dialog; import android.app.ListActivity; import android.app.ProgressDialog; +import android.content.DialogInterface; 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,25 +20,30 @@ 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 { - public static final int GOTLOCATION = 1; - public static final int GOTSTATIONLIST = 2; - public static final int NOPROVIDER = 3; - public static final int FIXTIMEOUT = 4; - public static final int LOOKUPSTATIONFAILED = 5; +public class StationList extends ListActivity { + public static final int GOTLOCATION = 1001; + public static final int GOTSTATIONLIST = 1002; + public static final int NOPROVIDER = 1003; + public static final int LOCATIONFIXTIMEOUT = 1004; + - public static final int DLG_PROGRESS = 1; + public static final int DLG_PROGRESS = 1001; /** Called when the activity is first created. */ + String dialogMessage = ""; ProgressDialog dialog; - StationLocator locator = null; - LocatorTask locatorTask = new LocatorTask(); + LocationLookup locator = null; + LocatorTask locatorTask; boolean isRunning = false; List stations = new ArrayList(); + StationProvider stationProvider = ProviderFactory.getStationProvider(); + StationListAdapter adapter = null; @SuppressWarnings("unchecked") @@ -44,13 +52,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 { @@ -76,7 +84,7 @@ ProgressDialog dlg = new ProgressDialog(this); dlg.setMessage("Wait for location fix"); dlg.setCancelable(false); - return dlg; + return dlg; default: return super.onCreateDialog(id); } @@ -91,6 +99,10 @@ switch (id) { case DLG_PROGRESS: this.dialog = (ProgressDialog) dialog; + if (!dialogMessage.equals("")) { + this.dialog.setMessage(dialogMessage); + dialogMessage = ""; + } break; } } @@ -100,51 +112,67 @@ showDialog(DLG_PROGRESS); locator.locateStations(); - stationsFetched.sendEmptyMessageDelayed(FIXTIMEOUT, 20000); + stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); } Handler stationsFetched = new Handler() { @Override public void handleMessage(Message msg) { + switch (msg.what) { case GOTLOCATION: - 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 ); + dismissDialog(DLG_PROGRESS); + + startLocatorTask(); + break; + case NOPROVIDER: - dialog.dismiss(); - MessageBox.showMessage(TrainInfoList.this,"No location provider enabled. Plase enable gps."); + dismissDialog(DLG_PROGRESS); + MessageBox.showMessage(StationList.this,"No location provider enabled. Plase enable gps."); break; - case FIXTIMEOUT: - dialog.dismiss(); + case LOCATIONFIXTIMEOUT: if (isRunning) { - locator.abortLocationListener(); + locator.stopSearch(); if (locator.hasLocation()) { - msg.what = GOTLOCATION; - handleMessage( msg ); // ToDo: ugly recursive call !!! - } else { - MessageBox.showMessage(TrainInfoList.this,"GPS fix timed out"); + stationsFetched.sendEmptyMessage( GOTLOCATION ); + } else { + dismissDialog(DLG_PROGRESS); + + AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); + builder.setMessage("GPS fix timed out"); + builder.setCancelable(true); + builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + startLookup(); + + } + }); + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + } + }); + builder.show(); + } } break; - case LOOKUPSTATIONFAILED: - dialog.dismiss(); - MessageBox.showMessage(TrainInfoList.this,"Error on finding nearby stations"); - break; } isRunning = false; } }; - + void startLocatorTask() + { + dialogMessage = "Finding nearby stations"; + showDialog(DLG_PROGRESS); + + locatorTask = new LocatorTask(); + locatorTask.execute(); + } @Override protected void onListItemClick(ListView l, View v, int position, long id) { @@ -154,19 +182,16 @@ double latitude = station.getLatitude(); double longitude = station.getLongitude(); - String addr = station.getAddress(); - if (addr == null || addr.trim().equals("") ) - addr = lookupAddress(latitude, longitude); Intent intent = new Intent(this, DepartureList.class); intent.putExtra("name", station.getName()); - intent.putExtra("address", addr); intent.putExtra("distance", station.getDistance()); intent.putExtra("latitude", latitude); intent.putExtra("longitude", longitude); - intent.putExtra("code", station.getCode()); + intent.putExtra("stationid", station.getId()); + intent.putExtra("address", station.getAddress()); startActivity(intent); } @@ -196,8 +221,8 @@ return sb.toString(); } - class LocatorTask extends AsyncTask { + boolean success; @Override protected void onPreExecute() { @@ -206,7 +231,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; } @@ -214,7 +247,37 @@ @Override protected void onPostExecute(Void result) { super.onPostExecute(result); - + dialog.dismiss(); + + 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 ); + + } else { //communication or parse errors + AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); + builder.setMessage("Error on finding nearby stations"); + builder.setCancelable(true); + builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + + stationsFetched.post( new Runnable() { + @Override + public void run() { + startLocatorTask(); + } + }); + } + }); + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + } + }); + builder.show(); + } } } } \ No newline at end of file