--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/01/28 10:34:50 566 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/05/03 13:41:04 701 @@ -18,6 +18,7 @@ import android.os.Handler; import android.os.Message; +import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; @@ -118,7 +119,7 @@ startLookup(); break; case ListSearch: - this.showDialog(DLG_STATIONNAME); + this.showDialogSafe(DLG_STATIONNAME); break; case ListFavorites: startFavoriteLookup(); @@ -151,8 +152,41 @@ } setTitle(dialogTitle); + + } + + + /* these 3 dialogs helper functions are very rude and ugly hack + * to remove these auto-reported exceptions + * - android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@436aaef8 is not valid; is your activity running? + * - java.lang.IllegalArgumentException: View not attached to window manager + */ + + public void showDialogSafe(int id) { + try { + showDialog(id); + } catch (Exception e) { + Log.e("StationList", "showDialog failed", e); + } } + public void dismissDialogSafe(int id) { + try { + dismissDialog(id); + } catch (Exception e) { + Log.e("StationList", "dismissDialog failed", e); + } + } + + public void builderShowSafe(AlertDialog.Builder builder) { + try { + builder.show(); + } catch (Exception e) { + Log.e("StationList", "builder.show() failed", e); + } + + } + /* EOF rude and ugly dialog hack */ @@ -323,7 +357,7 @@ public void startLookup() { isRunning = true; dialogMessage = getString( stationlist_waitforlocation ); - showDialog(DLG_PROGRESS); + showDialogSafe(DLG_PROGRESS); locationLookup.locateStations(); stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); @@ -331,10 +365,10 @@ void startNameSearch(String name) { dialogMessage = getString( stationlist_findbyname ); - showDialog(DLG_PROGRESS); + showDialogSafe(DLG_PROGRESS); findStationsTask = new FindStationsTask(); - findStationsTask.searchByName(name, locationLookup.getLocation()); + findStationsTask.searchByName(name); findStationsTask.execute(); } @@ -343,10 +377,10 @@ if (favorites.size() > 0) { dialogMessage = getString( stationlist_loadfavorites ); - showDialog(DLG_PROGRESS); + showDialogSafe(DLG_PROGRESS); findStationsTask = new FindStationsTask(); - findStationsTask.searchByIds(favorites.toString(), locationLookup.getLocation()); + findStationsTask.searchByIds( favorites.toString() ); findStationsTask.execute(); } else { showMessageAndClose( getString( stationlist_nofavorites ) ); @@ -358,41 +392,13 @@ void startLocatorTask() { dialogMessage = getString( stationlist_findingnearby ); - showDialog(DLG_PROGRESS); + showDialogSafe(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByLocation( locationLookup.getLocation() ); findStationsTask.execute(); } - - - /* TODO: Remove this no longer needed function - String lookupAddress(double latitude, double longitude) { - - Geocoder coder = new Geocoder(this, new Locale("da")); - StringBuilder sb = new StringBuilder(); - Log.i("lookupaddr", "" + latitude + "/" + longitude); - try { - List
addressList = coder.getFromLocation(latitude, longitude, 1); - Address addr = addressList.get(0); - - - int max = addr.getMaxAddressLineIndex(); - for (int i=0; i0) - sb.append(", "); - - sb.append(addr.getAddressLine(i)); - } - - - } catch (Exception e) { - Log.e("DepartureList", "geocoder failed", e); - } - return sb.toString(); - }*/ - //////////////////////////////////////////////////////////////////////////// // Inner classes @@ -403,7 +409,7 @@ switch (msg.what) { case GOTLOCATION: - dismissDialog(DLG_PROGRESS); + dismissDialogSafe(DLG_PROGRESS); startLocatorTask(); location = GeoPair.fromLocation( locationLookup.getLocation() ); @@ -411,7 +417,7 @@ break; case NOPROVIDER: - dismissDialog(DLG_PROGRESS); + dismissDialogSafe(DLG_PROGRESS); MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); break; case LOCATIONFIXTIMEOUT: @@ -420,7 +426,7 @@ if (locationLookup.hasLocation()) { stationsFetched.sendEmptyMessage( GOTLOCATION ); } else { - dismissDialog(DLG_PROGRESS); + dismissDialogSafe(DLG_PROGRESS); AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); builder.setMessage( getString( stationlist_gpstimeout) ); @@ -436,8 +442,8 @@ public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } - }); - builder.show(); + }); + builderShowSafe(builder); // builder.show() } } @@ -456,10 +462,9 @@ Location loc; String ids; - public void searchByName(String n, Location l) { + public void searchByName(String n) { method = LookupMethod.ByName; - loc = l; name = n; } @@ -468,10 +473,9 @@ loc = l; } - public void searchByIds(String id, Location l) { + public void searchByIds(String id) { method = LookupMethod.ByList; - loc = l; ids = id; } @@ -501,23 +505,6 @@ } - Location dummy = new Location("gps"); - List stations = stationProvider.getStations(); - - for (StationBean station : stations) { - - if (method.equals(LookupMethod.ByName) || method.equals(LookupMethod.ByList)) { - if (loc != null) { //only do the distance calc if we have a location - dummy.setLatitude(station.getLatitude()); - dummy.setLongitude(station.getLongitude()); - station.setDistance( (int)loc.distanceTo(dummy) ); - } else { - station.setDistance(0); - } - } - - } - return null; } @@ -542,20 +529,44 @@ public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); - stationsFetched.post( new Runnable() { - @Override - public void run() { - startLocatorTask(); - } - }); + Runnable runner = null; + switch (method) { + case ByLocation: + runner = new Runnable() { + @Override + public void run() { + startLocatorTask(); + } + }; + break; + case ByName: + runner = new Runnable() { + @Override + public void run() { + startNameSearch( FindStationsTask.this.name ); + } + }; + break; + case ByList: + runner = new Runnable() { + @Override + public void run() { + startFavoriteLookup(); + } + }; + break; + } + + stationsFetched.post( runner ); } }); builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } - }); - builder.show(); + }); + + builderShowSafe(builder); // builder.show() } } }