--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/09/28 14:58:45 1142 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/09/28 15:30:13 1143 @@ -70,13 +70,7 @@ import dk.thoerup.traininfo.util.MessageBox; public class StationList extends ListActivity { - public enum LookupStates { - GOTLOCATION, - GOTSTATIONLIST, - NOPROVIDER, - LOCATIONFIXTIMEOUT, - GPS_SAT_COUNT - } + public static final int OPTIONS_MAP = 2003; @@ -103,10 +97,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(); @@ -118,7 +112,7 @@ WelcomeScreen.ListType listType; SharedPreferences prefs; - + /////////////////////////////////////////////////////////////////////////////////////////// //Activity call backs @@ -135,7 +129,7 @@ ListView lv = getListView(); lv.setOnCreateContextMenuListener(contextMenu); - locationLookup = new LocationLookup(this, stationsFetched); + locationLookup = new LocationLookup(this); prefs = getSharedPreferences("TrainStation", 0); @@ -171,7 +165,6 @@ } else { stations = (StationBean) savedInstanceState.getSerializable("stations"); adapter.setStations(stations); - location = (GeoPair) savedInstanceState.getSerializable("location"); } } @@ -185,7 +178,6 @@ protected void onDestroy() { super.onDestroy(); - isRunning = false; if (locationLookup != null) { locationLookup.stopSearch(); @@ -224,7 +216,6 @@ if (dialog != null && dialog.isShowing()) dialog.dismiss(); outState.putSerializable("stations", (StationBean) stations); - outState.putSerializable("location", location); } @@ -391,12 +382,11 @@ // public void startLookup() { - isRunning = true; dialogMessage = getString( stationlist_waitforlocation ); showDialog(DLG_PROGRESS); locationLookup.locateStations(); - stationsFetched.sendEmptyMessageDelayed(LookupStates.LOCATIONFIXTIMEOUT.ordinal(), GPS_TIMEOUT_MS); + stationsFetched.sendEmptyMessageDelayed(0, 500); } void startNameSearch(String name) { @@ -428,7 +418,7 @@ void startLocatorTask() { dialogMessage = getString( stationlist_findingnearby ); - showDialog(DLG_PROGRESS); + showDialog(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByLocation( locationLookup.getLocation() ); @@ -443,60 +433,66 @@ @Override public void handleMessage(Message msg) { Log.e("Handler", "WHAT:" + msg.what); - - LookupStates state = LookupStates.values()[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 GPS_SAT_COUNT: - int count = msg.arg1; - String dialogMessage = getString( stationlist_waitforlocation ) + "\n" + getString( stationlist_satellitecount ) + ": " + count; - dialog.setMessage( dialogMessage ); - return; //abort immediately - case LOCATIONFIXTIMEOUT: - if (isRunning) { - locationLookup.stopSearch(); - if (locationLookup.hasLocation()) { - stationsFetched.sendEmptyMessage( LookupStates.GOTLOCATION.ordinal() ); - } 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(); + } + }); + 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 {