--- android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/03/17 15:27:11 630 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationList.java 2010/06/26 11:02:53 918 @@ -4,6 +4,7 @@ import java.util.List; +import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ListActivity; @@ -69,7 +70,8 @@ StationsFetchedHandler stationsFetched = new StationsFetchedHandler(); GeoPair location = new GeoPair(); - + + boolean isLaunchedforShortcut; boolean isRunning = false; List stations = new ArrayList(); @@ -111,6 +113,8 @@ listType = (WelcomeScreen.ListType) getIntent().getSerializableExtra("type"); setTitle(); + isLaunchedforShortcut = getIntent().getBooleanExtra("shortcut", false); + if (savedInstanceState == null) { @@ -119,7 +123,7 @@ startLookup(); break; case ListSearch: - this.showDialog(DLG_STATIONNAME); + this.showDialogSafe(DLG_STATIONNAME); break; case ListFavorites: startFavoriteLookup(); @@ -135,6 +139,18 @@ } } + + + @Override + protected void onDestroy() { + super.onDestroy(); + + if (findStationsTask != null) { + findStationsTask.cancel(true); + } + } + + protected void setTitle() { String dialogTitle = getResources().getString(app_name); switch (listType) { @@ -152,8 +168,48 @@ } 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 dismissDialogSafe(Dialog dlg) { + try { + dlg.dismiss(); + } 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 */ @@ -214,7 +270,7 @@ message.append( getString(stationlist_nolocation) ); } - MessageBox.showMessage(this, message.toString()); + MessageBox.showMessage(this, message.toString(), false); break; default: retval = super.onOptionsItemSelected(item); @@ -271,8 +327,9 @@ public void onClick(DialogInterface dialog, int which) { EditText et = (EditText) rootView.findViewById(R.id.EditText); dialog.dismiss(); - if (et.getText().toString().length() >= 2) { - startNameSearch(et.getText().toString()); + String search = et.getText().toString().trim(); + if (search.length() >= 2) { + startNameSearch(search); } else { showMessageAndClose( getString(stationlist_twocharmin) ); } @@ -313,9 +370,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); + } } ///////////////////////////////////////////////////////////// @@ -324,7 +388,7 @@ public void startLookup() { isRunning = true; dialogMessage = getString( stationlist_waitforlocation ); - showDialog(DLG_PROGRESS); + showDialogSafe(DLG_PROGRESS); locationLookup.locateStations(); stationsFetched.sendEmptyMessageDelayed(LOCATIONFIXTIMEOUT, 20000); @@ -332,7 +396,7 @@ void startNameSearch(String name) { dialogMessage = getString( stationlist_findbyname ); - showDialog(DLG_PROGRESS); + showDialogSafe(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByName(name); @@ -344,7 +408,7 @@ if (favorites.size() > 0) { dialogMessage = getString( stationlist_loadfavorites ); - showDialog(DLG_PROGRESS); + showDialogSafe(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByIds( favorites.toString() ); @@ -359,7 +423,7 @@ void startLocatorTask() { dialogMessage = getString( stationlist_findingnearby ); - showDialog(DLG_PROGRESS); + showDialogSafe(DLG_PROGRESS); findStationsTask = new FindStationsTask(); findStationsTask.searchByLocation( locationLookup.getLocation() ); @@ -376,7 +440,7 @@ switch (msg.what) { case GOTLOCATION: - dismissDialog(DLG_PROGRESS); + dismissDialogSafe(DLG_PROGRESS); startLocatorTask(); location = GeoPair.fromLocation( locationLookup.getLocation() ); @@ -384,8 +448,9 @@ break; case NOPROVIDER: - dismissDialog(DLG_PROGRESS); - MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider) ); + dismissDialogSafe(DLG_PROGRESS); + MessageBox.showMessage(StationList.this, getString(stationlist_nolocationprovider), true ); + //StationList.this.finish(); break; case LOCATIONFIXTIMEOUT: if (isRunning) { @@ -393,7 +458,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) ); @@ -409,8 +474,8 @@ public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } - }); - builder.show(); + }); + builderShowSafe(builder); // builder.show() } } @@ -478,7 +543,7 @@ @Override protected void onPostExecute(Void result) { super.onPostExecute(result); - dialog.dismiss(); + dismissDialogSafe(dialog); if (success) { @@ -486,34 +551,58 @@ 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) { 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(); + StationList.this.finish(); } - }); - try { - builder.show(); - } catch (android.view.WindowManager.BadTokenException e) { - Log.i("StationList", "BadTokenException"); // this can happen if the user switched away from this activity, while doInBackground was running - } + }); + + builderShowSafe(builder); // builder.show() } } }