--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/05/07 15:13:07 713 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/07/11 15:15:04 983 @@ -4,6 +4,7 @@ import java.util.List; +import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ListActivity; @@ -47,13 +48,14 @@ public static final int OPTIONS_MAP = 2003; public static final int OPTIONS_GPSINFO = 2004; - - - public static final int DLG_PROGRESS = 3001; public static final int DLG_STATIONNAME = 3002; + + public static final int GPS_TIMEOUT_MS = 17500; //how long are we willing to wait for gps fix -in milliseconds + + static enum LookupMethod { ByLocation, ByName, @@ -69,7 +71,8 @@ StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); GeoPair location = new GeoPair(); - + + boolean isLaunchedforShortcut; boolean isRunning = false; List stations = new ArrayList(); @@ -111,6 +114,8 @@ listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); setTitle(); + isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); + if (savedInstanceState == null) { @@ -118,8 +123,8 @@ case ListNearest: startLookup(); break; - case ListSearch: - this.showDialogSafe(DLG_STATIONNAME); + case ListSearch: + showDialog(DLG_STATIONNAME); //TODO: this.showDialogSafe(DLG_STATIONNAME); break; case ListFavorites: startFavoriteLookup(); @@ -135,6 +140,22 @@ } } + + + @Override + protected void onDestroy() { + super.onDestroy(); + + if (findStationsTask != null) { + findStationsTask.cancel(true); + } + if (locationLookup != null) { + locationLookup.stopSearch(); + } + isRunning = false; + } + + protected void setTitle() { String dialogTitle = getResources().getString(app_name); switch (listType) { @@ -161,7 +182,8 @@ * - 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); @@ -177,6 +199,13 @@ Log.e("StationList", "dismissDialog failed", e); } } + public void dismissDialogSafe(Dialog dlg) { + try { + dlg.dismiss(); + } catch (Exception e) { + Log.e("StationList", "dismissDialog failed", e); + } + } public void builderShowSafe(AlertDialog.Builder builder) { try { @@ -185,7 +214,8 @@ Log.e("StationList", "builder.show() failed", e); } - } + }*/ + /* EOF rude and ugly dialog hack */ @@ -247,7 +277,7 @@ message.append( getString(stationlist_nolocation) ); } - MessageBox.showMessage(this, message.toString()); + MessageBox.showMessage(this, message.toString(), false); break; default: retval = super.onOptionsItemSelected(item); @@ -347,9 +377,16 @@ StationBean station = stations.get(position); - Intent intent = new Intent(this, DepartureList.class); - intent.putExtra("stationbean", station); - startActivity(intent); + if (isLaunchedforShortcut == true) { + Intent i = new Intent(); + i.putExtra("station", station); + setResult(Activity.RESULT_OK, i); + finish(); + } else { + Intent intent = new Intent(this, DepartureList.class); + intent.putExtra("stationbean", station); + startActivity(intent); + } } ///////////////////////////////////////////////////////////// @@ -358,15 +395,15 @@ public void startLookup() { isRunning = true; dialogMessage = getString( stationlist_waitforlocation ); - showDialogSafe(DLG_PROGRESS); + showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); locationLookup.locateStations(); - stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); + stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, GPS_TIMEOUT_MS); } void startNameSearch(String name) { dialogMessage = getString( stationlist_findbyname ); - showDialogSafe(DLG_PROGRESS); + showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByName(name); @@ -378,7 +415,7 @@ if (favorites.size() > 0) { dialogMessage = getString( stationlist_loadfavorites ); - showDialogSafe(DLG_PROGRESS); + showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByIds( favorites.toString() ); @@ -393,7 +430,7 @@ void startLocatorTask() { dialogMessage = getString( stationlist_findingnearby ); - showDialogSafe(DLG_PROGRESS); + showDialog(DLG_PROGRESS);//TODO:showDialogSafe(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByLocation( locationLookup.getLocation() ); @@ -410,7 +447,7 @@ switch (msg.what) { case GOTLOCATION: - dismissDialogSafe(DLG_PROGRESS); + dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS); startLocatorTask(); location = GeoPair.fromLocation( locationLookup.getLocation() ); @@ -418,8 +455,9 @@ break; case NOPROVIDER: - dismissDialogSafe(DLG_PROGRESS); - MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); + dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS); + MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); + //StationList.this.finish(); break; case LOCATIONFIXTIMEOUT: if (isRunning) { @@ -427,7 +465,7 @@ if (locationLookup.hasLocation()) { stationsFetched.sendEmptyMessage( GOTLOCATION ); } else { - dismissDialogSafe(DLG_PROGRESS); + dismissDialog(DLG_PROGRESS);//TODO:dismissDialogSafe(DLG_PROGRESS); AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); builder.setMessage( getString( stationlist_gpstimeout) ); @@ -444,7 +482,7 @@ dialog.dismiss(); } }); - builderShowSafe(builder); // builder.show() + builder.show();//TODO:builderShowSafe(builder); } } @@ -512,7 +550,7 @@ @Override protected void onPostExecute(Void result) { super.onPostExecute(result); - dialog.dismiss(); + dialog.dismiss();//TODO:dismissDialogSafe(dialog); if (success) { @@ -520,11 +558,14 @@ showMessageAndClose(getString(stationlist_nostations)); } stations = stationProvider.getStations(); + + StationList.this.getListView().invalidateViews(); adapter.setStations( stations ); + } else { //communication or parse errors AlertDialog.Builder builder = new AlertDialog.Builder(StationList.this); - builder.setMessage(getString(stationlist_nearbyerror)); + builder.setMessage(getString(stationlist_fetcherror)); builder.setCancelable(true); builder.setPositiveButton(getString(generic_retry), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { @@ -564,10 +605,11 @@ builder.setNegativeButton(getString(generic_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); + StationList.this.finish(); } }); - builderShowSafe(builder); // builder.show() + builder.show();//TODO:builderShowSafe(builder); } } }