--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/09/28 14:58:45 1142 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2011/03/30 17:26:50 1245 @@ -23,6 +23,7 @@ import static dk.thoerup.traininfo.R.string.stationlist_nostations; import static dk.thoerup.traininfo.R.string.stationlist_obtainedby; import static dk.thoerup.traininfo.R.string.stationlist_removefavorite; +import static dk.thoerup.traininfo.R.string.stationlist_satellitecount; import static dk.thoerup.traininfo.R.string.stationlist_search; import static dk.thoerup.traininfo.R.string.stationlist_stationadded; import static dk.thoerup.traininfo.R.string.stationlist_stationmap; @@ -30,7 +31,6 @@ 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; @@ -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); @@ -156,7 +150,7 @@ switch (listType) { case ListNearest: - startLookup(); + startNearestLookup(); break; case ListSearch: showDialog(DLG_STATIONNAME); @@ -171,7 +165,6 @@ } else { stations = (StationBean) savedInstanceState.getSerializable("stations"); adapter.setStations(stations); - location = (GeoPair) savedInstanceState.getSerializable("location"); } } @@ -185,7 +178,8 @@ protected void onDestroy() { super.onDestroy(); - isRunning = false; + stationsFetched.removeMessages(0); + if (locationLookup != null) { locationLookup.stopSearch(); @@ -224,7 +218,6 @@ if (dialog != null && dialog.isShowing()) dialog.dismiss(); outState.putSerializable("stations", (StationBean) stations); - outState.putSerializable("location", location); } @@ -334,7 +327,7 @@ dialog.dismiss(); String search = et.getText().toString().trim(); if (search.length() >= 2) { - startNameSearch(search); + startNameLookup(search); } else { showMessageAndClose( getString(stationlist_twocharmin) ); } @@ -390,16 +383,15 @@ ///////////////////////////////////////////////////////////// // - public void startLookup() { - isRunning = true; + public void startNearestLookup() { 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) { + void startNameLookup(String name) { dialogMessage = getString( stationlist_findbyname ); showDialog(DLG_PROGRESS); @@ -428,7 +420,7 @@ void startLocatorTask() { dialogMessage = getString( stationlist_findingnearby ); - showDialog(DLG_PROGRESS); + showDialog(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByLocation( locationLookup.getLocation() ); @@ -442,61 +434,73 @@ class StationsFetchedHandler extends Handler { @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; + case IDLE: + Log.e("TrainInfo", "How did this happen ???"); + dismissDialog(DLG_PROGRESS); // how did we get here ?? + 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(); + startNearestLookup(); + + } + }); + 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 { @@ -536,7 +540,7 @@ switch (method) { case ByLocation: - stations = stationProvider.lookupStations(loc); + stations = stationProvider.lookupStationsByLocation(loc); break; case ByName: stations = stationProvider.lookupStationsByName(name); @@ -589,7 +593,7 @@ runner = new Runnable() { @Override public void run() { - startNameSearch( FindStationsTask.this.name ); + startNameLookup( FindStationsTask.this.name ); } }; break;