--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/09/18 07:18:59 1078 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2011/01/30 21:14:19 1220 @@ -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; @@ -33,7 +34,6 @@ import java.util.ArrayList; - import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; @@ -48,6 +48,7 @@ import android.os.Bundle; import android.os.Handler; import android.os.Message; +import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; @@ -69,10 +70,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 +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(); @@ -113,7 +112,7 @@ WelcomeScreen.ListType listType; SharedPreferences prefs; - + /////////////////////////////////////////////////////////////////////////////////////////// //Activity call backs @@ -130,7 +129,7 @@ ListView lv = getListView(); lv.setOnCreateContextMenuListener(contextMenu); - locationLookup = new LocationLookup(this, stationsFetched); + locationLookup = new LocationLookup(this); prefs = getSharedPreferences("TrainStation", 0); @@ -151,7 +150,7 @@ switch (listType) { case ListNearest: - startLookup(); + startNearestLookup(); break; case ListSearch: showDialog(DLG_STATIONNAME); @@ -166,7 +165,6 @@ } else { stations = (StationBean) savedInstanceState.getSerializable("stations"); adapter.setStations(stations); - location = (GeoPair) savedInstanceState.getSerializable("location"); } } @@ -180,7 +178,6 @@ protected void onDestroy() { super.onDestroy(); - isRunning = false; if (locationLookup != null) { locationLookup.stopSearch(); @@ -219,7 +216,6 @@ if (dialog != null && dialog.isShowing()) dialog.dismiss(); outState.putSerializable("stations", (StationBean) stations); - outState.putSerializable("location", location); } @@ -329,7 +325,7 @@ dialog.dismiss(); String search = et.getText().toString().trim(); if (search.length() >= 2) { - startNameSearch(search); + startNameLookup(search); } else { showMessageAndClose( getString(stationlist_twocharmin) ); } @@ -385,16 +381,15 @@ ///////////////////////////////////////////////////////////// // - public void startLookup() { - isRunning = true; + public void startNearestLookup() { dialogMessage = getString( stationlist_waitforlocation ); showDialog(DLG_PROGRESS); locationLookup.locateStations(); - stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); + stationsFetched.sendEmptyMessageDelayed(0, 500); } - void startNameSearch(String name) { + void startNameLookup(String name) { dialogMessage = getString( stationlist_findbyname ); showDialog(DLG_PROGRESS); @@ -423,7 +418,7 @@ void startLocatorTask() { dialogMessage = getString( stationlist_findingnearby ); - showDialog(DLG_PROGRESS); + showDialog(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByLocation( locationLookup.getLocation() ); @@ -438,52 +433,77 @@ @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; + case IDLE: + Log.e("TrainInfo", "How did this happen ???"); + dismissDialog(DLG_PROGRESS); // how did we get here ?? + return; + + } - } + + + if (locationLookup.elapsedTime() >= GPS_TIMEOUT_MS) { + try { + dismissDialog(DLG_PROGRESS); + } catch (IllegalArgumentException ex) { + // I get stacktraces that reports an exception is thrown here - + // but i can not recreate the situation that causes said exception + Log.e("TrainInfo", "Why the f*** is this exception thrown here ?? " + ex.getMessage() ); } - break; + + 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(); + + } + } 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 { @@ -523,7 +543,7 @@ switch (method) { case ByLocation: - stations = stationProvider.lookupStations(loc); + stations = stationProvider.lookupStationsByLocation(loc); break; case ByName: stations = stationProvider.lookupStationsByName(name); @@ -576,7 +596,7 @@ runner = new Runnable() { @Override public void run() { - startNameSearch( FindStationsTask.this.name ); + startNameLookup( FindStationsTask.this.name ); } }; break;