--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/09/18 07:18:59 1078 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/10/01 17:33:30 1150 @@ -30,10 +30,10 @@ import static dk.thoerup.traininfo.R.string.stationlist_stationsearch; import static dk.thoerup.traininfo.R.string.stationlist_twocharmin; import static dk.thoerup.traininfo.R.string.stationlist_waitforlocation; +import static dk.thoerup.traininfo.R.string.stationlist_satellitecount; import java.util.ArrayList; - import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; @@ -69,10 +69,8 @@ import dk.thoerup.traininfo.util.MessageBox; 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 OPTIONS_MAP = 2003; public static final int OPTIONS_GPSINFO = 2004; @@ -98,10 +96,10 @@ FindStationsTask findStationsTask; StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); - GeoPair location = new GeoPair(); + //GeoPair location = new GeoPair(); boolean isLaunchedforShortcut; - boolean isRunning = false; + StationBean stations = new StationBean(); StationProvider stationProvider = ProviderFactory.getStationProvider(); @@ -113,7 +111,7 @@ WelcomeScreen.ListType listType; SharedPreferences prefs; - + /////////////////////////////////////////////////////////////////////////////////////////// //Activity call backs @@ -130,7 +128,7 @@ ListView lv = getListView(); lv.setOnCreateContextMenuListener(contextMenu); - locationLookup = new LocationLookup(this, stationsFetched); + locationLookup = new LocationLookup(this); prefs = getSharedPreferences("TrainStation", 0); @@ -166,7 +164,6 @@ } else { stations = (StationBean) savedInstanceState.getSerializable("stations"); adapter.setStations(stations); - location = (GeoPair) savedInstanceState.getSerializable("location"); } } @@ -180,7 +177,6 @@ protected void onDestroy() { super.onDestroy(); - isRunning = false; if (locationLookup != null) { locationLookup.stopSearch(); @@ -219,7 +215,6 @@ if (dialog != null && dialog.isShowing()) dialog.dismiss(); outState.putSerializable("stations", (StationBean) stations); - outState.putSerializable("location", location); } @@ -386,12 +381,11 @@ // public void startLookup() { - isRunning = true; dialogMessage = getString( stationlist_waitforlocation ); showDialog(DLG_PROGRESS); locationLookup.locateStations(); - stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); + stationsFetched.sendEmptyMessageDelayed(0, 500); } void startNameSearch(String name) { @@ -423,7 +417,7 @@ void startLocatorTask() { dialogMessage = getString( stationlist_findingnearby ); - showDialog(DLG_PROGRESS); + showDialog(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByLocation( locationLookup.getLocation() ); @@ -438,52 +432,66 @@ @Override public void handleMessage(Message msg) { - switch (msg.what) { + LocationLookup.LookupStates state = locationLookup.getState(); + + + switch (state) { case GOTLOCATION: dismissDialog(DLG_PROGRESS); - + startLocatorTask(); - location = GeoPair.fromLocation( locationLookup.getLocation() ); - - break; + + return; case NOPROVIDER: dismissDialog(DLG_PROGRESS); MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); //StationList.this.finish(); - break; - case LOCATIONFIXTIMEOUT: - if (isRunning) { - locationLookup.stopSearch(); - if (locationLookup.hasLocation()) { - stationsFetched.sendEmptyMessage( GOTLOCATION ); - } else { - dismissDialog(DLG_PROGRESS); - - AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); - builder.setMessage( getString( stationlist_gpstimeout) ); - builder.setCancelable(true); - builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.dismiss(); - startLookup(); - - } - }); - builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.dismiss(); - } - }); - builder.show(); + return; + } + + + + if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { + dismissDialog(DLG_PROGRESS); + + locationLookup.stopSearch(); + + if (locationLookup.hasLocation()) { + startLocatorTask(); + } else { + AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); + builder.setMessage( getString( stationlist_gpstimeout) ); + builder.setCancelable(true); + builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + startLookup(); + + } + }); + builder.setNegativeButton( getString(generic_cancel), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + StationList.this.finish(); // Close this Activity + } + }); + builder.show(); - } } - break; + } else { + if (locationLookup.hasGps()) { + int count = locationLookup.getSatCount(); + String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; + dialog.setMessage( dialogMessage ); + } + this.sendEmptyMessageDelayed(0, 500); } - isRunning = false; + } - }; + } + + class FindStationsTask extends AsyncTask {